Cozy Web上でエンティティのモデル定義によって設定されたリソースの一覧アクセスをノンプログラミングで行う方法についてみています。
前回はリソースの一覧表示の機能機能について説明します。
今回はページング機能について説明します。
HelloResource
リソースの作成・更新処理の題材としてHelloResourceを定義しました。
このHelloResourceを使用して、リソースの一覧表示の方法についてみています。
準備
cozyを起動するディレクトリのwebappsディレクトリに、アプリケーションのホームディレクトリとなるHelloResourceが作成されています。これをベースに開発を進めます。
モデル
WEB-INF/models/model.orgにアプリケーションで使用するモデルが定義されています。
- * entity
- ** product
- *** features
- table=product
- *** attributes
- | Name | Type | Multiplicity |
- |-------+--------+--------------|
- | id | token | 1 |
- | name | string | 1 |
- | price | int | 1 |
エンティティproductを定義しています。
これがWebのリソースproductとなります。
トップページ
トップページとしてホームディレクトリに以下のindex.htmlを配備しています。これは前回と同様です。
- <html>
- <head>
- <title>HelloResource</title>
- </head>
- <body>
- <ul>
- <li><a href="product">List</a></li>
- <li><a href="product/_create_">Create</a></li>
- <li><a href="product/_update_">Update</a></li>
- <li><a href="product/_delete_">Delete</a></li>
- </ul>
- </body>
- </html>
一覧表示
まず、前回紹介したパラメタなしの場合です。
以下のようにリソース名を指定してアクセスします。
- $ curl http://localhost:8080/web/HelloResource/product
その結果、以下のページが表示されます。(この結果はxmllintで整形しています。他のHTML出力も同様です。)
- <?xml version="1.0"?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- <meta name="keywords" content="TBD"/>
- <meta name="description" content="TBD"/>
- <meta name="robots" content="noindex,nofollow"/>
- <meta name="author" content="TBD"/>
- <title>product</title>
- </head>
- <body>
- <div>
- <table class="">
- <caption class="">product</caption>
- <thead class="">
- <tr class="">
- <th scope="col" class="">Id</th>
- <th scope="col" class="">Name</th>
- <th scope="col" class="">Price</th>
- </tr>
- </thead>
- <tbody class="">
- <tr data-href="product/1.html">
- <td class="">1</td>
- <td class="">Apple</td>
- <td class="">300</td>
- </tr>
- </tbody>
- </table>
- <table>
- <tr>
- <td>Prev</td>
- <td>
- <a href="#?query.offset=0&query.page.size=1">1</a>
- </td>
- <td>
- <a href="#?query.offset=1&query.page.size=1">2</a>
- </td>
- <td>
- <a href="#?query.offset=2&query.page.size=1">3</a>
- </td>
- <td>
- <a href="#?query.offset=3&query.page.size=1">4</a>
- </td>
- <td>Next</td>
- </tr>
- </table>
- </div>
- </body>
- </html>
ページング
次はページング機能を使用してみます。
ページング機能を使用する場合、query.page.sizeパラメタで1ページ数あたりの行数を指定します。
以下のようにリソース名のページを指定して、queyr.page.sizeに「1」を指定して実行してみます。
- $ curl "http://localhost:8080/web/HelloResource/product?query.page.size=1"
この結果、以下のページが表示されました。
- <?xml version="1.0"?>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
- <meta name="keywords" content="TBD"/>
- <meta name="description" content="TBD"/>
- <meta name="robots" content="noindex,nofollow"/>
- <meta name="author" content="TBD"/>
- <title>product</title>
- </head>
- <body>
- <div>
- <table class="">
- <caption class="">product</caption>
- <thead class="">
- <tr class="">
- <th scope="col" class="">Id</th>
- <th scope="col" class="">Name</th>
- <th scope="col" class="">Price</th>
- </tr>
- </thead>
- <tbody class="">
- <tr data-href="product/1.html">
- <td class="">1</td>
- <td class="">Apple</td>
- <td class="">300</td>
- </tr>
- </tbody>
- </table>
- <table>
- <tr>
- <td>Prev</td>
- <td>
- <a href="#?query.offset=0&query.page.size=1">1</a>
- </td>
- <td>
- <a href="#?query.offset=1&query.page.size=1">2</a>
- </td>
- <td>
- <a href="#?query.offset=2&query.page.size=1">3</a>
- </td>
- <td>
- <a href="#?query.offset=3&query.page.size=1">4</a>
- </td>
- <td>Next</td>
- </tr>
- </table>
- </div>
- </body>
- </html>
query.page.sizeに「1」を指定しているので1ページあたり1行表示のページングが行われています。データ表示のテーブルの下部にページング移動用のテーブルが表示されています。全レコード数が4なので、4ページの構成となっています。
まとめ
今回はリソースの一覧表示のページングについて説明しました。
次回はカスタム画面などの機能について説明する予定です。
諸元
- Cozy
- 0.0.15