欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

輕松學習jQuery插件EasyUI EasyUI實現(xiàn)樹形網絡基本操作(2)

 更新時間:2022年05月06日 10:31:40   投稿:lijiao  
這篇文章主要幫助大家輕松學習jQuery插件EasyUI,針對EasyUI實現(xiàn)樹形網絡基本操作,分為三大方面:動態(tài)加載、添加分頁、以及惰性加載節(jié)點,感興趣的小伙伴們可以參考一下

一、EasyUI樹形網格動態(tài)加載

動態(tài)加載樹形網格有助于從服務器上加載部分的行數(shù)據(jù),避免加載大型數(shù)據(jù)的長時間等待。本教程將向您展示如何創(chuàng)建帶有動態(tài)加載特性的樹形網格(TreeGrid)。

創(chuàng)建樹形網格(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>

服務器端代碼
treegrid3_getdata.php

$id = isset($_POST['id']) ? 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']) ? '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;
}

二、EasyUI樹形網格添加分頁

第二部分教大家如何向帶有動態(tài)加載特性的樹形網格(TreeGrid)添加分頁。

 

創(chuàng)建樹形網格(TreeGrid)

啟用樹形網格(TreeGrid)的分頁特性,必須添加 'pagination:true' 屬性,這樣頁面加載時就會向服務器發(fā)送 'page' 和 'rows' 參數(shù)。

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

發(fā)送到服務器的參數(shù)包括:

page:要加載的當前頁面。
rows:頁面尺寸大小。
id:父行的 id 值,從服務器返回的行將被添加。
當展開一個行節(jié)點時,'id' 值是大于 0 的。 當改變頁碼時,'id' 值應該被設置為 0 來放置加載子行。

三、EasyUI樹形網格惰性加載節(jié)點

有時我們已經得到充分的分層樹形網格(TreeGrid)的數(shù)據(jù)。 我們還想讓樹形網格(TreeGrid)按層次惰性加載節(jié)點。 首先,只加載頂層節(jié)點。 然后點擊節(jié)點的展開圖標來加載它的子節(jié)點。 本教程展示如何創(chuàng)建帶有惰性加載特性的樹形網格(TreeGrid)。

 

創(chuàng)建樹形網格(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>

為了放置加載子節(jié)點,我們需要為每個節(jié)點重命名 'children' 屬性。 正如下面的代碼所示,'children' 屬性重命名為 'children1'。 當展開一個節(jié)點時,我們調用 'append' 方法來加載它的子節(jié)點數(shù)據(jù)。
'loadFilter' 代碼

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

以上就是針對樹形網絡的相關操作,希望對大家的學習有所幫助,大家可以結合前一篇文章進行學習,會有意想不到的收獲。

相關文章閱讀: 《輕松學習jQuery插件EasyUI EasyUI創(chuàng)建樹形網絡(1)》

相關文章

最新評論