輕松學(xué)習(xí)jQuery插件EasyUI EasyUI實(shí)現(xiàn)樹形網(wǎng)絡(luò)基本操作(2)
一、EasyUI樹形網(wǎng)格動(dòng)態(tài)加載
動(dòng)態(tài)加載樹形網(wǎng)格有助于從服務(wù)器上加載部分的行數(shù)據(jù),避免加載大型數(shù)據(jù)的長(zhǎng)時(shí)間等待。本教程將向您展示如何創(chuàng)建帶有動(dò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>
服務(wù)器端代碼
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)格添加分頁
第二部分教大家如何向帶有動(dòng)態(tài)加載特性的樹形網(wǎng)格(TreeGrid)添加分頁。

創(chuàng)建樹形網(wǎng)格(TreeGrid)
啟用樹形網(wǎng)格(TreeGrid)的分頁特性,必須添加 'pagination:true' 屬性,這樣頁面加載時(shí)就會(huì)向服務(wù)器發(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>
服務(wù)器端代碼
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ā)送到服務(wù)器的參數(shù)包括:
page:要加載的當(dāng)前頁面。
rows:頁面尺寸大小。
id:父行的 id 值,從服務(wù)器返回的行將被添加。
當(dāng)展開一個(gè)行節(jié)點(diǎn)時(shí),'id' 值是大于 0 的。 當(dāng)改變頁碼時(shí),'id' 值應(yīng)該被設(shè)置為 0 來放置加載子行。
三、EasyUI樹形網(wǎng)格惰性加載節(jié)點(diǎn)
有時(shí)我們已經(jīng)得到充分的分層樹形網(wǎng)格(TreeGrid)的數(shù)據(jù)。 我們還想讓樹形網(wǎng)格(TreeGrid)按層次惰性加載節(jié)點(diǎn)。 首先,只加載頂層節(jié)點(diǎn)。 然后點(diǎn)擊節(jié)點(diǎn)的展開圖標(biāo)來加載它的子節(jié)點(diǎn)。 本教程展示如何創(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é)點(diǎn),我們需要為每個(gè)節(jié)點(diǎn)重命名 'children' 屬性。 正如下面的代碼所示,'children' 屬性重命名為 'children1'。 當(dāng)展開一個(gè)節(jié)點(diǎn)時(shí),我們調(diào)用 'append' 方法來加載它的子節(jié)點(diǎn)數(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;
}
以上就是針對(duì)樹形網(wǎng)絡(luò)的相關(guān)操作,希望對(duì)大家的學(xué)習(xí)有所幫助,大家可以結(jié)合前一篇文章進(jìn)行學(xué)習(xí),會(huì)有意想不到的收獲。
相關(guān)文章閱讀: 《輕松學(xué)習(xí)jQuery插件EasyUI EasyUI創(chuàng)建樹形網(wǎng)絡(luò)(1)》
- jQuery使用zTree插件實(shí)現(xiàn)樹形菜單和異步加載
- 輕松學(xué)習(xí)jQuery插件EasyUI EasyUI創(chuàng)建樹形菜單
- 輕松學(xué)習(xí)jQuery插件EasyUI EasyUI創(chuàng)建樹形網(wǎng)絡(luò)(1)
- 推薦8款jQuery輕量級(jí)樹形Tree插件
- jquery.simple.tree插件 更簡(jiǎn)單,兼容性更好的無限樹插件
- jQuery ztree實(shí)現(xiàn)動(dòng)態(tài)樹形多選菜單
- jQuery樹形控件zTree使用小結(jié)
- jQuery zTree加載樹形菜單功能
- jquery zTree異步加載簡(jiǎn)單實(shí)例講解
- jQuery樹形插件jquery.simpleTree.js用法分析
相關(guān)文章
精選的10款用于構(gòu)建良好易用性網(wǎng)站的jQuery插件
這篇隨筆收集了10款非常給力的jquery 插件,幫助你構(gòu)建易用性良好的網(wǎng)站,希望對(duì)你有用!2011-01-01
JQuery 動(dòng)態(tài)擴(kuò)展對(duì)象之另類視角
大家都知道javascript是動(dòng)態(tài)語言,它對(duì)動(dòng)態(tài)的支持是與身俱來的。2010-05-05
ASP.NET jQuery 實(shí)例13 原創(chuàng)jQuery文本框字符限制插件-TextArea Counter
這節(jié)介紹一個(gè)自己寫的jQuery文本框字符限制插件,至于如何寫插件,我這里就不多講了,可以查看相關(guān)介紹,這里主要介紹下該插件的功能2012-02-02
Jquery焦點(diǎn)與失去焦點(diǎn)示例應(yīng)用
這篇文章主要介紹了Jquery焦點(diǎn)與失去焦點(diǎn)示例應(yīng)用,需要的朋友可以參考下2014-06-06
用簡(jiǎn)潔的jQuery方法toggleClass實(shí)現(xiàn)隔行換色
這篇文章主要介紹了用簡(jiǎn)潔的jQuery方法toggleClass實(shí)現(xiàn)隔行換色,很簡(jiǎn)單,但很實(shí)用,需要的朋友可以看看2014-10-10
很棒的學(xué)習(xí)jQuery的12個(gè)網(wǎng)站推薦
jQuery是目前最流行的 JavaScript 庫。對(duì)于初學(xué)者來說,有的時(shí)候很難找到一個(gè)好的學(xué)習(xí)jQuery的網(wǎng)站,今天本文收集了12個(gè)很棒的 jQuery 學(xué)習(xí)網(wǎng)站推薦給大家。2011-04-04
jQuery使用toggleClass方法動(dòng)態(tài)添加刪除Class樣式的方法
這篇文章主要介紹了jQuery使用toggleClass方法動(dòng)態(tài)添加刪除Class樣式的方法,實(shí)例分析了jQuery中toggleClass方法操作class樣式的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03

