jQuery EasyUI 數(shù)據(jù)網(wǎng)格與樹插件 – Tree 樹

2018-09-07 19:42 更新

jQuery EasyUI 數(shù)據(jù)網(wǎng)格與樹插件 - Tree 樹


jQuery EasyUI 插件 jQuery EasyUI 插件

通過 $.fn.tree.defaults 重寫默認(rèn)的 defaults。

樹(tree)在網(wǎng)頁中以樹形結(jié)構(gòu)顯示分層數(shù)據(jù)。它向用戶提供展開、折疊、拖拽、編輯和異步加載功能。

依賴

  • draggable
  • droppable

用法

樹(tree)定義在 <ul> 元素中。該標(biāo)記可定義葉節(jié)點和子節(jié)點。節(jié)點將是 ul 列表內(nèi)的 <li> 元素。下面演示了將被用于制作嵌套在 ul 元素內(nèi)的樹節(jié)點的元素。

<ul id="tt" class="easyui-tree">
    <li>
		<span>Folder</span>
		<ul>
			<li>
				<span>Sub Folder 1</span>
				<ul>
					<li><span><a href="#">File 11</a></span></li>
					<li><span>File 12</span></li>
					<li><span>File 13</span></li>
				</ul>
			</li>
			<li><span>File 2</span></li>
			<li><span>File 3</span></li>
		</ul>
	</li>
    <li><span>File21</span></li>
</ul>

樹(Tree)也可以在一個空的 <ul> 元素中定義,可使用 javascript 加載數(shù)據(jù)。

<ul id="tt"></ul>
$('#tt').tree({
    url:'tree_data.json'
});

使用 loadFilter 來處理來自 ASP.NET web 服務(wù)的 json 數(shù)據(jù)。

$('#tt').tree({
    url: ...,
    loadFilter: function(data){
		if (data.d){
			return data.d;
		} else {
			return data;
		}
    }
});

樹的數(shù)據(jù)格式(Tree Data Format)

每個節(jié)點可以包括下列屬性:

  • id:節(jié)點的 id,它對于加載遠(yuǎn)程數(shù)據(jù)很重要。
  • text:要顯示的節(jié)點文本。
  • state:節(jié)點狀態(tài),'open' 或 'closed',默認(rèn)是 'open'。當(dāng)設(shè)置為 'closed' 時,該節(jié)點有子節(jié)點,并且將從遠(yuǎn)程站點加載它們。
  • checked:指示節(jié)點是否被選中。
  • attributes:給一個節(jié)點添加的自定義屬性。
  • children:定義了一些子節(jié)點的節(jié)點數(shù)組。

實例:

[{
    "id":1,
    "text":"Folder1",
    "iconCls":"icon-save",
    "children":[{
		"text":"File1",
		"checked":true
    },{
		"text":"Books",
		"state":"open",
		"attributes":{
			"url":"/demo/book/abc",
			"price":100
		},
		"children":[{
			"text":"PhotoShop",
			"checked":true
		},{
			"id": 8,
			"text":"Sub Bookds",
			"state":"closed"
		}]
    }]
},{
    "text":"Languages",
    "state":"closed",
    "children":[{
		"text":"Java"
    },{
		"text":"C#"
    }]
}]

異步樹

樹支持內(nèi)置的異步加載模式,因此用戶可以創(chuàng)建一個空的樹,然后指定一個動態(tài)返回 JSON 數(shù)據(jù)的服務(wù)器端,用于根據(jù)需求異步填充樹。下面是一個實例:

<ul class="easyui-tree" data-options="url:'get_data.php'"></ul>

樹是通過 URL 'get_data.php' 加載的。子節(jié)點依賴于父節(jié)點狀態(tài)被加載。當(dāng)展開一個關(guān)閉的節(jié)點時,如果該節(jié)點沒有子節(jié)點加載,它將通過上面定義的 URL 向服務(wù)器發(fā)送節(jié)點的 id 值作為名為 'id' 的 http 請求參數(shù),以便檢索子節(jié)點。

請看從服務(wù)器返回的數(shù)據(jù):

[{
    "id": 1,
    "text": "Node 1",
    "state": "closed",
    "children": [{
		"id": 11,
		"text": "Node 11"
    },{
		"id": 12,
		"text": "Node 12"
    }]
},{
    "id": 2,
    "text": "Node 2",
    "state": "closed"
}]

節(jié)點 1 和節(jié)點 2 是關(guān)閉的,當(dāng)展開節(jié)點 1 時,將直接展示它的子節(jié)點。當(dāng)展開節(jié)點 2 時,將向服務(wù)器發(fā)送 value(2) 以便檢索子節(jié)點。

本教程中的 創(chuàng)建異步樹 演示了如何編寫服務(wù)器代碼來根據(jù)需求返回樹的數(shù)據(jù)。

屬性

名稱 類型 描述 默認(rèn)值
url string 獲取遠(yuǎn)程數(shù)據(jù)的 URL 。 null
method string 檢索數(shù)據(jù)的 http 方法(method)。 post
animate boolean 定義當(dāng)節(jié)點展開折疊時是否顯示動畫效果。 false
checkbox boolean 定義是否在每個節(jié)點前邊顯示復(fù)選框。 false
cascadeCheck boolean 定義是否級聯(lián)檢查。 true
onlyLeafCheck boolean 定義是否只在葉節(jié)點前顯示復(fù)選框。 false
lines boolean 定義是否顯示樹線條。 false
dnd boolean 定義是否啟用拖放。 false
data array 要加載的節(jié)點數(shù)據(jù)。
$('#tt').tree({
	data: [{
		text: 'Item1',
		state: 'closed',
		children: [{
			text: 'Item11'
		},{
			text: 'Item12'
		}]
	},{
		text: 'Item2'
	}]
});
null
formatter function(node) 定義如何呈現(xiàn)節(jié)點文本。
代碼實例:
$('#tt').tree({
	formatter:function(node){
		return node.text;
	}
});
loader function(param,success,error) 定義如何從遠(yuǎn)程服務(wù)器加載數(shù)據(jù)。返回 false 則取消該動作。該函數(shù)有下列參數(shù):
param:要傳遞到遠(yuǎn)程服務(wù)器的參數(shù)對象。
success(data):當(dāng)檢索數(shù)據(jù)成功時調(diào)用的回調(diào)函數(shù)。
error():當(dāng)檢索數(shù)據(jù)失敗時調(diào)用的回調(diào)函數(shù)。
json loader
loadFilter function(data,parent) 返回要顯示的過濾數(shù)據(jù)。返回數(shù)據(jù)時以標(biāo)準(zhǔn)樹格式返回的。該函數(shù)有下列參數(shù):
data:要加載的原始數(shù)據(jù)。
parent:DOM 對象,表示父節(jié)點。

事件

很多事件的回調(diào)函數(shù)需要 'node' 參數(shù),它包括下列屬性:

  • id:綁定到節(jié)點的標(biāo)識值。
  • text:要顯示的文本。
  • iconCls:用來顯示圖標(biāo)的 css class。
  • checked:節(jié)點是否被選中。
  • state:節(jié)點狀態(tài),'open' 或 'closed'。
  • attributes:綁定到節(jié)點的自定義屬性。
  • target:目標(biāo)的 DOM 對象。
名稱 參數(shù) 描述
onClick node 當(dāng)用戶點擊一個節(jié)點時觸發(fā)。代碼實例:
$('#tt').tree({
	onClick: function(node){
		alert(node.text);  // alert node text property when clicked
	}
});
onDblClick node 當(dāng)用戶雙擊一個節(jié)點時觸發(fā)。
onBeforeLoad node, param 當(dāng)加載數(shù)據(jù)的請求發(fā)出前觸發(fā),返回 false 則取消加載動作。
onLoadSuccess node, data 當(dāng)數(shù)據(jù)加載成功時觸發(fā)。
onLoadError arguments 當(dāng)數(shù)據(jù)加載失敗時觸發(fā),arguments 參數(shù)與 jQuery.ajax 的 'error' 函數(shù)一樣。
onBeforeExpand node 節(jié)點展開前觸發(fā),返回 false 則取消展開動作。
onExpand node 當(dāng)節(jié)點展開時觸發(fā)。
onBeforeCollapse node 節(jié)點折疊前觸發(fā),返回 false 則取消折疊動作。
onCollapse node 當(dāng)節(jié)點折疊時觸發(fā)。
onBeforeCheck node, checked 當(dāng)用戶點擊復(fù)選框前觸發(fā),返回 false 則取消該選中動作。該事件自版本 1.3.1 起可用。
onCheck node, checked 當(dāng)用戶點擊復(fù)選框時觸發(fā)。
onBeforeSelect node 節(jié)點被選中前觸發(fā),返回 false 則取消選擇動作。
onSelect node 當(dāng)節(jié)點被選中時觸發(fā)。
onContextMenu e, node 當(dāng)右鍵點擊節(jié)點時觸發(fā)。代碼實例:
// right click node and then display the context menu
$('#tt').tree({
	onContextMenu: function(e, node){
		e.preventDefault();
		// select the node
		$('#tt').tree('select', node.target);
		// display context menu
		$('#mm').menu('show', {
			left: e.pageX,
			top: e.pageY
		});
	}
});

// the context menu is defined as below:
<div id="mm" class="easyui-menu" style="width:120px;">
	<div onclick="append()" data-options="iconCls:'icon-add'">Append</div>
	<div onclick="remove()" data-options="iconCls:'icon-remove'">Remove</div>
</div>
onBeforeDrag node 當(dāng)節(jié)點的拖拽開始時觸發(fā),返回 false 則禁止拖拽。該事件自版本 1.3.2 起可用。
onStartDrag node 當(dāng)開始拖拽節(jié)點時觸發(fā)。該事件自版本 1.3.2 起可用。
onStopDrag node 當(dāng)停止拖拽節(jié)點時觸發(fā)。該事件自版本 1.3.2 起可用。
onDragEnter target, source 當(dāng)節(jié)點被拖拽進(jìn)入某個允許放置的目標(biāo)節(jié)點時觸發(fā),返回 false 則禁止放置。
target:被放置的目標(biāo)節(jié)點元素。
source:被拖拽的源節(jié)點。
該事件自版本 1.3.2 起可用。
onDragOver target, source 當(dāng)節(jié)點被拖拽到允許放置的目標(biāo)節(jié)點上時觸發(fā),返回 false 則禁止放置。
target:被放置的目標(biāo)節(jié)點元素。
source:被拖拽的源節(jié)點。
該事件自版本 1.3.2 起可用。
onDragLeave target, source 當(dāng)節(jié)點被拖拽離開允許放置的目標(biāo)節(jié)點時觸發(fā)。
target:被放置的目標(biāo)節(jié)點元素。
source:被拖拽的源節(jié)點。
該事件自版本 1.3.2 起可用。
onBeforeDrop target,source,point 節(jié)點被放置之前觸發(fā),返回 false 則禁止放置。
target:DOM 對象,放置的目標(biāo)節(jié)點。
source:源節(jié)點。
point:表示放置操作,可能的值是:'append'、'top' 或 'bottom'。
該事件自版本 1.3.2 起可用。
onDrop target,source,point 當(dāng)節(jié)點被放置時觸發(fā)。 target:DOM 對象,放置的目標(biāo)節(jié)點。
source:源節(jié)點。
point:表示放置操作,可能的值是:'append'、'top' 或 'bottom'。
onBeforeEdit node 編輯節(jié)點前觸發(fā)。
onAfterEdit node 編輯節(jié)點后觸發(fā)。
onCancelEdit node 當(dāng)取消編輯動作時觸發(fā)。

方法

名稱 參數(shù) 描述
options none 返回樹的選項(options)。
loadData data 加載樹的數(shù)據(jù)。
getNode target 獲取指定的節(jié)點對象。
getData target 獲取指定的節(jié)點數(shù)據(jù),包括它的子節(jié)點。
reload target 重新加載樹的數(shù)據(jù)。
getRoot none 獲取根節(jié)點,返回節(jié)點對象。
getRoots none 獲取根節(jié)點,返回節(jié)點數(shù)組。
getParent target 獲取父節(jié)點,target 參數(shù)表示節(jié)點的 DOM 對象。
getChildren target 獲取子節(jié)點, target 參數(shù)表示節(jié)點的 DOM 對象。
getChecked state 獲取選中的節(jié)點。狀態(tài)可用值有:'checked'、'unchecked'、'indeterminate'。如果狀態(tài)未分配,則返回 'checked' 節(jié)點。
代碼實例:
var nodes = $('#tt').tree('getChecked');	// get checked nodes
var nodes = $('#tt').tree('getChecked', 'unchecked');	// get unchecked nodes
var nodes = $('#tt').tree('getChecked', 'indeterminate');	// get indeterminate nodes
var nodes = $('#tt').tree('getChecked', ['checked','indeterminate']);	// get checked and indeterminate nodes
getSelected none 獲取選中的節(jié)點并返回它,如果沒有選中節(jié)點,則返回 null。
isLeaf target 把指定的節(jié)點定義成葉節(jié)點,target 參數(shù)表示節(jié)點的 DOM 對象。
find id 找到指定的節(jié)點并返回該節(jié)點對象。代碼實例:
// find a node and then select it
var node = $('#tt').tree('find', 12);
$('#tt').tree('select', node.target);
select target 選中一個節(jié)點,target 參數(shù)表示節(jié)點的 DOM 對象。
check target 把指定節(jié)點設(shè)置為勾選。
uncheck target 把指定節(jié)點設(shè)置為未勾選。
collapse target 折疊一個節(jié)點,target 參數(shù)表示節(jié)點的 DOM 對象。
expand target 展開一個節(jié)點,target 參數(shù)表示節(jié)點的 DOM 對象。當(dāng)節(jié)點關(guān)閉且沒有子節(jié)點時,節(jié)點的 id 值(名為 'id' 參數(shù))將被發(fā)送至服務(wù)器以請求子節(jié)點數(shù)據(jù)。
collapseAll target 折疊所有的節(jié)點。
expandAll target 展開所有的節(jié)點。
expandTo target 從根部展開一個指定的節(jié)點。
scrollTo target 滾動到指定節(jié)點。該方法自版本 1.3.4 起可用。
append param 追加一些子節(jié)點到一個父節(jié)點,param 參數(shù)有兩個屬性:
parent:DOM 對象,要追加到的父節(jié)點,如果沒有分配,則追加為根節(jié)點。
data:數(shù)組,節(jié)點的數(shù)據(jù)。

代碼實例:
// append some nodes to the selected node
var selected = $('#tt').tree('getSelected');
$('#tt').tree('append', {
	parent: selected.target,
	data: [{
		id: 23,
		text: 'node23'
	},{
		text: 'node24',
		state: 'closed',
		children: [{
			text: 'node241'
		},{
			text: 'node242'
		}]
	}]
});
toggle target 切換節(jié)點的展開/折疊狀態(tài),target 參數(shù)表示節(jié)點的 DOM 對象。
insert param 在指定節(jié)點的前邊或后邊插入一個節(jié)點,param 參數(shù)包括下列屬性:
before:DOM 對象,前邊插入的節(jié)點。
after:DOM 對象,后邊插入的節(jié)點。
data:對象,節(jié)點數(shù)據(jù)。

下面的代碼演示了如何在選中節(jié)點之前插入一個新的節(jié)點:
var node = $('#tt').tree('getSelected');
if (node){
	$('#tt').tree('insert', {
		before: node.target,
		data: {
			id: 21,
			text: 'node text'
		}
	});
}
remove target 移除一個節(jié)點和它的子節(jié)點,target 參數(shù)表示節(jié)點的 DOM 對象。
pop target 彈出一個節(jié)點和它的子節(jié)點,該方法和 remove 一樣,但是返回了移除的節(jié)點數(shù)據(jù)。
update param 更新指定的節(jié)點,'param' 參數(shù)有下列屬性:
target(DOM 對象,要被更新的節(jié)點)、id、text、iconCls、checked,等等。

代碼實例:
// update the selected node text
var node = $('#tt').tree('getSelected');
if (node){
	$('#tt').tree('update', {
		target: node.target,
		text: 'new text'
	});
}
enableDnd none 啟用拖放功能。
disableDnd none 禁用拖放功能。
beginEdit target 開始編輯節(jié)點。
endEdit target 結(jié)束編輯節(jié)點。
cancelEdit target 取消編輯節(jié)點。

jQuery EasyUI 插件 jQuery EasyUI 插件
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號