輕松學習jQuery插件EasyUI EasyUI實現(xiàn)樹形網(wǎng)絡基本操作(2)
一、EasyUI樹形網(wǎng)格動態(tài)加載
動態(tài)加載樹形網(wǎng)格有助于從服務器上加載部分的行數(shù)據(jù),避免加載大型數(shù)據(jù)的長時間等待。本教程將向您展示如何創(chuàng)建帶有動態(tài)加載特性的樹形網(wǎng)格(TreeGrid)。

創(chuàng)建樹形網(wǎ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樹形網(wǎng)格添加分頁
第二部分教大家如何向帶有動態(tài)加載特性的樹形網(wǎng)格(TreeGrid)添加分頁。

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

創(chuàng)建樹形網(wǎ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;
}
以上就是針對樹形網(wǎng)絡的相關操作,希望對大家的學習有所幫助,大家可以結合前一篇文章進行學習,會有意想不到的收獲。
相關文章閱讀: 《輕松學習jQuery插件EasyUI EasyUI創(chuàng)建樹形網(wǎng)絡(1)》
- jQuery使用zTree插件實現(xiàn)樹形菜單和異步加載
- 輕松學習jQuery插件EasyUI EasyUI創(chuàng)建樹形菜單
- 輕松學習jQuery插件EasyUI EasyUI創(chuàng)建樹形網(wǎng)絡(1)
- 推薦8款jQuery輕量級樹形Tree插件
- jquery.simple.tree插件 更簡單,兼容性更好的無限樹插件
- jQuery ztree實現(xiàn)動態(tài)樹形多選菜單
- jQuery樹形控件zTree使用小結
- jQuery zTree加載樹形菜單功能
- jquery zTree異步加載簡單實例講解
- jQuery樹形插件jquery.simpleTree.js用法分析
相關文章
Jquery實現(xiàn)仿京東商城省市聯(lián)動菜單
這篇文章主要介紹了Jquery實現(xiàn)仿京東商城省市聯(lián)動菜單的簡單實例演示,可以選擇對應省、市、縣,希望大家可以喜歡。2015-11-11
jquery中防刷IP流量軟件影響統(tǒng)計的一點對策
由于公司項目需要增加推廣獎勵功能,網(wǎng)站用戶通過邀請地址發(fā)給QQ好友、論壇、個人網(wǎng)站之類,只要有通過邀請地址點擊進來就算推廣成功(當天同一個IP僅算一次),推廣人會獲得相應的獎勵、積分等。2011-07-07
基于JQuery和原生JavaScript實現(xiàn)網(wǎng)頁定位導航特效
本文通過實例代碼給大家介紹了基于JQuery和原生JavaScript實現(xiàn)網(wǎng)頁定位導航特效,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-04-04
jquery實現(xiàn)文本框數(shù)量加減功能的例子分享
在做商城項目的時候,需要在購物車中增加一個商品加減功能,并在加減時同時修改總價格的顯示,并且保證文本框text只能輸入純數(shù)字2014-05-05
通過jquery獲取上傳文件名稱、類型和大小的實現(xiàn)代碼
這篇文章主要介紹了在文件上傳到服務器之前,我們可以通過jquery來獲取上傳文件的名稱,類型和尺寸大小,需要的朋友可以參考下2018-04-04

