jQuery EasyUI ツリーメニュー -ツリーグリッドにページングを追加
このチュートリアルでは、動的読み込み機能を備えたツリーグリッド(TreeGrid)にページングを追加する方法を説明します。
ツリーグリッド(TreeGrid)を作成する
ツリーグリッド(TreeGrid)のページング機能を有効にするには、'pagination:true' プロパティを追加する必要があります。これにより、ページの読み込み時に 'page' と 'rows' パラメータがサーバーに送信されます。
<table title="Products" class="easyui-treegrid" style="width:700px;height:300px"
data-options="
url: 'treegrid4_getdata.php',
rownumbers: true,
pagination: true,
pageSize: 2,
pageList: [2,10,20],
idField: 'id',
treeField: 'name',
onBeforeLoad: function(row,param){
if (!row) { // load top level rows
param.id = 0; // set id=0, indicate to load new page rows
}
}
">
<thead>
<tr>
<th field="name" width="250">Name</th>
<th field="quantity" width="100" align="right">Quantity</th>
<th field="price" width="150" align="right" formatter="formatDollar">Price</th>
<th field="total" width="150" align="right" formatter="formatDollar">Total</th>
</tr>
</thead>
</table>
サーバー側のコード
treegrid4_getdata.php
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$offset = ($page-1)*$rows;
$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
include 'conn.php';
$result = array();
if ($id == 0){
$rs = mysql_query("select count(*) from products where parentId=0");
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];
$rs = mysql_query("select * from products where parentId=0 limit $offset,$rows");
$items = array();
while($row = mysql_fetch_array($rs)){
$row['state'] = has_child($row['id']) ? 'closed' : 'open';
array_push($items, $row);
}
$result["rows"] = $items;
} else {
$rs = mysql_query("select * from products where parentId=$id");
while($row = mysql_fetch_array($rs)){
$row['state'] = has_child($row['id']) ? 'closed' : 'open';
$row['total'] = $row['price']*$row['quantity'];
array_push($result, $row);
}
}
echo json_encode($result);
function has_child($id){
$rs = mysql_query("select count(*) from products where parentId=$id");
$row = mysql_fetch_array($rs);
return $row[0] > 0 ? true : false;
}
サーバーに送信されるパラメータは次のとおりです:
- page:読み込む現在のページ。
- rows:ページサイズ。
- id:親行の id 値。サーバーから返された行が追加されます。
行ノードを展開すると、'id' 値は 0 より大きくなります。 ページ番号を変更するときは、子行の読み込みを防ぐために 'id' 値を 0 に設定する必要があります。
jQuery EasyUI インスタンスをダウンロード
jeasyui-tree-treegrid4.zip
その他の拡張機能