树形网格动态加载


动态加载树形网格有助于从服务器上加载部分的行数据,避免加载大型数据的长时间等待。本教程将向您展示如何创建带有动态加载特性的树形网格(TreeGrid)。

创建树形网格(TreeGrid)

  1. <table title="Products" class="easyui-treegrid" style="width:700px;height:300px"
  2. url="treegrid3_getdata.php"
  3. rownumbers="true"
  4. idField="id" treeField="name">
  5. <thead>
  6. <tr>
  7. <th field="name" width="250">Name</th>
  8. <th field="quantity" width="100" align="right">Quantity</th>
  9. <th field="price" width="150" align="right" formatter="formatDollar">Price</th>
  10. <th field="total" width="150" align="right" formatter="formatDollar">Total</th>
  11. </tr>
  12. </thead>
  13. </table>

服务器端代码

treegrid3_getdata.php

  1. $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
  2.  
  3. include 'conn.php';
  4. $result = array();
  5. $rs = mysql_query("select * from products where parentId=$id");
  6. while($row = mysql_fetch_array($rs)){
  7. $row['state'] = has_child($row['id']) ? 'closed' : 'open';
  8. $row['total'] = $row['price']*$row['quantity'];
  9. array_push($result, $row);
  10. }
  11.  
  12. echo json_encode($result);
  13.  
  14. function has_child($id){
  15. $rs = mysql_query("select count(*) from products where parentId=$id");
  16. $row = mysql_fetch_array($rs);
  17. return $row[0] > 0 ? true : false;
  18. }

下载 jQuery EasyUI 实例

下载地址