首頁 > web前端 > js教程 > 主體

輕鬆學習jQuery外掛程式EasyUI EasyUI實現樹狀網路基本操作(2)_jquery

WBOY
發布: 2016-05-16 15:28:40
原創
1028 人瀏覽過

一、EasyUI樹形網格動態載入
動態載入樹形網格有助於從伺服器上載入部分的行數據,避免載入大型數據的長時間等待。本教學將向您展示如何建立具有動態載入特性的樹形網格(TreeGrid)。

建立樹狀網格(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']) &#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;
}
登入後複製

二、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']) &#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;
}
登入後複製

傳送到伺服器的參數包括:
page:要載入的目前頁面。
rows:頁面尺寸大小。
id:父行的 id 值,從伺服器傳回的行將會被加入。
當展開一個行節點時,'id' 值是大於 0 的。 當改變頁碼時,'id' 值應該被設定為 0 來放置載入子行。
三、EasyUI樹形網格惰性載入節點
有時我們已經得到充分的分層樹形網格(TreeGrid)的資料。 我們也想讓樹形網格(TreeGrid)以層次惰性載入節點。 首先,只載入頂層節點。 然後點擊節點的展開圖示來載入它的子節點。 本教學展示如何建立具有惰性載入特性的樹形網格(TreeGrid)。

 

建立樹狀網格(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>
登入後複製

為了放置載入子節點,我們需要為每個節點重新命名 'children' 屬性。 正如下面的程式碼所示,'children' 屬性重命名為 'children1'。 當展開一個節點時,我們呼叫 'append' 方法來載入它的子節點資料。
'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建立樹形網路(1)》

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!