Home  >  Article  >  Web Front-end  >  Easily learn jQuery plug-in EasyUI EasyUI to implement basic operations of tree network (2)_jquery

Easily learn jQuery plug-in EasyUI EasyUI to implement basic operations of tree network (2)_jquery

WBOY
WBOYOriginal
2016-05-16 15:28:401072browse

1. Dynamic loading of EasyUI tree grid
Dynamically loading tree grids helps load partial rows of data from the server, avoiding long waits for loading large data. This tutorial will show you how to create a TreeGrid with dynamic loading features.

Create a tree grid (TreeGrid)

 <table title="Products" class="easyui-treegrid" style="width:700px;height:300px"
  url="treegrid3_getdata.php"
  rownumbers="true"
  idField="id" treeField="name">
 <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>

Server side code
treegrid3_getdata.php

$id = isset($_POST['id']) &#63; intval($_POST['id']) : 0;
 
include 'conn.php';
$result = array();
$rs = mysql_query("select * from products where parentId=$id");
while($row = mysql_fetch_array($rs)){
 $row['state'] = has_child($row['id']) &#63; '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 &#63; true : false;
}

2. Add paging to EasyUI tree grid
The second part teaches you how to add pagination to a tree grid (TreeGrid) with dynamic loading features.

Create a tree grid (TreeGrid)

To enable the pagination feature of TreeGrid, you must add the 'pagination:true' attribute, so that the 'page' and 'rows' parameters will be sent to the server when the page is loaded.

 <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>

Server side code

treegrid4_getdata.php

$page = isset($_POST['page']) &#63; intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) &#63; intval($_POST['rows']) : 10;
$offset = ($page-1)*$rows;
 
$id = isset($_POST['id']) &#63; 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']) &#63; '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']) &#63; '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 &#63; true : false;
}

Parameters sent to the server include:
page: The current page to load.
rows: Page size.
id: The id value of the parent row to which the row returned from the server will be added.
When expanding a row node, the 'id' value is greater than 0. When changing the page number, the 'id' value should be set to 0 to place the loading subrow.
3. EasyUI tree grid lazy loading nodes
Sometimes we already have full hierarchical TreeGrid data. We also want the TreeGrid to lazily load nodes hierarchically. First, only the top-level nodes are loaded. Then click the node's expand icon to load its child nodes. This tutorial shows how to create a TreeGrid with lazy loading.

Create a tree grid (TreeGrid)

 <table id="test" title="Folder Browser" class="easyui-treegrid" style="width:700px;height:300px"
  data-options="
  url: 'data/treegrid_data.json',
  method: 'get',
  rownumbers: true,
  idField: 'id',
  treeField: 'name',
  loadFilter: myLoadFilter
  ">
 <thead>
  <tr>
  <th field="name" width="220">Name</th>
  <th field="size" width="100" align="right">Size</th>
  <th field="date" width="150">Modified Date</th>
  </tr>
 </thead>
 </table>

In order to place loading child nodes, we need to rename the 'children' attribute for each node. As shown in the code below, the 'children' property is renamed to 'children1'. When a node is expanded, we call the 'append' method to load its child node data.
'loadFilter' code

 function myLoadFilter(data,parentId){
 function setData(){
  var todo = [];
  for(var i=0; i<data.length; i++){
  todo.push(data[i]);
  }
  while(todo.length){
  var node = todo.shift();
  if (node.children){
   node.state = 'closed';
   node.children1 = node.children;
   node.children = undefined;
   todo = todo.concat(node.children1);
  }
  }
 }
 
 setData(data);
 var tg = $(this);
 var opts = tg.treegrid('options');
 opts.onBeforeExpand = function(row){
  if (row.children1){
  tg.treegrid('append',{
   parent: row[opts.idField],
   data: row.children1
  });
  row.children1 = undefined;
  tg.treegrid('expand', row[opts.idField]);
  }
  return row.children1 == undefined;
 };
 return data;
 }

The above are related operations for tree networks. I hope it will be helpful to everyone's learning. You can study in conjunction with the previous article, and you will have unexpected gains.

Read related articles: "Easy to learn the jQuery plug-in EasyUI EasyUI to create a tree network (1)"

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn