輕松學(xué)習(xí)jQuery插件EasyUI EasyUI創(chuàng)建樹形菜單
一、EasyUI使用標(biāo)記創(chuàng)建樹形菜單
一個樹形菜單(Tree)可以從標(biāo)記創(chuàng)建。easyui 樹形菜單(Tree)也可以定義在 <ul> 元素中。無序列表的 <ul> 元素提供一個基礎(chǔ)的樹(Tree)結(jié)構(gòu)。每一個 <li> 元素將產(chǎn)生一個樹節(jié)點(diǎn),子 <ul> 元素將產(chǎn)生一個父樹節(jié)點(diǎn)。
創(chuàng)建樹形菜單(Tree)
<ul class="easyui-tree"> <li> <span>Folder</span> <ul> <li> <span>Sub Folder 1</span> <ul> <li><span>File 11</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>
二、EasyUI創(chuàng)建異步樹形菜單
為了創(chuàng)建異步的樹形菜單(Tree),每一個樹節(jié)點(diǎn)必須要有一個 'id' 屬性,這個將提交回服務(wù)器去檢索子節(jié)點(diǎn)數(shù)據(jù)。
創(chuàng)建樹形菜單(Tree)
<ul id="tt" class="easyui-tree" url="tree2_getdata.php"> </ul>
服務(wù)器端代碼
$id = isset($_POST['id']) ? intval($_POST['id']) : 0; include 'conn.php'; $result = array(); $rs = mysql_query("select * from nodes where parentId=$id"); while($row = mysql_fetch_array($rs)){ $node = array(); $node['id'] = $row['id']; $node['text'] = $row['name']; $node['state'] = has_child($row['id']) ? 'closed' : 'open'; array_push($result,$node); } echo json_encode($result); function has_child($id){ $rs = mysql_query("select count(*) from nodes where parentId=$id"); $row = mysql_fetch_array($rs); return $row[0] > 0 ? true : false; }
三、EasyUI樹形菜單添加節(jié)點(diǎn)
本節(jié)向您展示如何附加節(jié)點(diǎn)到樹形菜單(Tree)。我們將創(chuàng)建一個包含水果和蔬菜節(jié)點(diǎn)的食品樹,然后添加一些其他水果到已存在的水果節(jié)點(diǎn)。
創(chuàng)建食品樹
首先,我們創(chuàng)建食品樹,代碼如下所示:
<div style="width:200px;height:auto;border:1px solid #ccc;"> <ul id="tt" class="easyui-tree" url="tree_data.json"></ul> </div>
請注意,樹(Tree)組件是定義在 <ul> 標(biāo)記中,樹節(jié)點(diǎn)數(shù)據(jù)從 URL "tree_data.json" 加載。
得到父節(jié)點(diǎn)
然后我們通過點(diǎn)擊節(jié)點(diǎn)選擇水果節(jié)點(diǎn),我們將添加一些其他的水果數(shù)據(jù)。執(zhí)行 getSelected 方法得到處理節(jié)點(diǎn):
var node = $('#tt').tree('getSelected');
getSelected 方法的返回結(jié)果是一個 javascript 對象,它有一個 id、text、target 屬性。target 屬性是一個 DOM 對象,引用選中節(jié)點(diǎn),它的 append 方法將用于附加子節(jié)點(diǎn)。
附加節(jié)點(diǎn)
var node = $('#tt').tree('getSelected'); if (node){ var nodes = [{ "id":13, "text":"Raspberry" },{ "id":14, "text":"Cantaloupe" }]; $('#tt').tree('append', { parent:node.target, data:nodes }); }
當(dāng)添加一些水果,您將看見:
正如您所看到的,使用 easyui 的樹(Tree)插件去附加節(jié)點(diǎn)不是那么的難。
四、EasyUI創(chuàng)建帶復(fù)選框的樹形菜單
easyui 的樹(Tree)插件允許您創(chuàng)建一個復(fù)選框樹。如果您點(diǎn)擊一個節(jié)點(diǎn)的復(fù)選框,這個點(diǎn)擊的節(jié)點(diǎn)信息將向上和向下繼承。例如:點(diǎn)擊 'tomato' 節(jié)點(diǎn)的復(fù)選框,您將會看見 'Vegetables' 節(jié)點(diǎn)現(xiàn)在僅僅選中部分。
創(chuàng)建復(fù)選框樹
<ul id="tt" class="easyui-tree" url="data/tree_data.json" checkbox="true"> </ul>
五、EasyUI樹形菜單拖放控制
當(dāng)在一個應(yīng)用中使用樹(Tree)插件,拖拽(drag)和放置(drop)功能要求允許用戶改變節(jié)點(diǎn)位置。啟用拖拽(drag)和放置(drop)操作,所有您需要做的就是把樹(Tree)插件的 'dnd' 屬性設(shè)置為 true。
創(chuàng)建樹形菜單(Tree)
$('#tt').tree({ dnd: true, url: 'tree_data.json' });
當(dāng)在一個樹節(jié)點(diǎn)上發(fā)生放置操作,'onDrop' 事件將被觸發(fā),您應(yīng)該做一些或更多的操作,例如保存節(jié)點(diǎn)狀態(tài)到遠(yuǎn)程服務(wù)器端,等等。
onDrop: function(targetNode, source, point){ var targetId = $(target).tree('getNode', targetNode).id; $.ajax({ url: '...', type: 'post', dataType: 'json', data: { id: source.id, targetId: targetId, point: point } }); }
六、EasyUI樹形菜單加載父/子節(jié)點(diǎn)
通常表示一個樹節(jié)點(diǎn)的方式就是在每一個節(jié)點(diǎn)存儲一個 parentid。 這個也被稱為鄰接列表模型。 直接加載這些數(shù)據(jù)到樹形菜單(Tree)是不允許的。 但是我們可以在加載樹形菜單之前,把它轉(zhuǎn)換為標(biāo)準(zhǔn)標(biāo)準(zhǔn)的樹形菜單(Tree)數(shù)據(jù)格式。 樹(Tree)插件提供一個 'loadFilter' 選項(xiàng)函數(shù),它可以實(shí)現(xiàn)這個功能。 它提供一個機(jī)會來改變?nèi)魏我粋€進(jìn)入數(shù)據(jù)。 本教程向您展示如何使用 'loadFilter' 函數(shù)加載父/子節(jié)點(diǎn)到樹形菜單(Tree)。
父/子節(jié)點(diǎn)數(shù)據(jù)
[ {"id":1,"parendId":0,"name":"Foods"}, {"id":2,"parentId":1,"name":"Fruits"}, {"id":3,"parentId":1,"name":"Vegetables"}, {"id":4,"parentId":2,"name":"apple"}, {"id":5,"parentId":2,"name":"orange"}, {"id":6,"parentId":3,"name":"tomato"}, {"id":7,"parentId":3,"name":"carrot"}, {"id":8,"parentId":3,"name":"cabbage"}, {"id":9,"parentId":3,"name":"potato"}, {"id":10,"parentId":3,"name":"lettuce"} ] 使用 'loadFilter' 創(chuàng)建樹形菜單(Tree) $('#tt').tree({ url: 'data/tree6_data.json', loadFilter: function(rows){ return convert(rows); } });
轉(zhuǎn)換的實(shí)現(xiàn)
function convert(rows){ function exists(rows, parentId){ for(var i=0; i<rows.length; i++){ if (rows[i].id == parentId) return true; } return false; } var nodes = []; // get the top level nodes for(var i=0; i<rows.length; i++){ var row = rows[i]; if (!exists(rows, row.parentId)){ nodes.push({ id:row.id, text:row.name }); } } var toDo = []; for(var i=0; i<nodes.length; i++){ toDo.push(nodes[i]); } while(toDo.length){ var node = toDo.shift(); // the parent node // get the children nodes for(var i=0; i<rows.length; i++){ var row = rows[i]; if (row.parentId == node.id){ var child = {id:row.id,text:row.name}; if (node.children){ node.children.push(child); } else { node.children = [child]; } toDo.push(child); } } } return nodes; }
以上就是關(guān)于EasyUI創(chuàng)建樹形菜單的基本操作方法,希望大家可以學(xué)以致用,真正的掌握其中的技巧。
- jquery實(shí)現(xiàn)自定義樹形表格的方法【自定義樹形結(jié)構(gòu)table】
- jQuery 利用ztree實(shí)現(xiàn)樹形表格的實(shí)例代碼
- 推薦8款jQuery輕量級樹形Tree插件
- jQuery樹形控件zTree使用小結(jié)
- jquery ztree實(shí)現(xiàn)下拉樹形框使用到了json數(shù)據(jù)
- json+jQuery實(shí)現(xiàn)的無限級樹形菜單效果代碼
- jquery實(shí)現(xiàn)樹形菜單完整代碼
- jQuery ztree實(shí)現(xiàn)動態(tài)樹形多選菜單
- jQuery 樹形結(jié)構(gòu)的選擇器
- jQuery實(shí)現(xiàn)樹形員工信息表
相關(guān)文章
jQuery實(shí)現(xiàn)鼠標(biāo)經(jīng)過顯示動畫邊框特效
本文主要介紹了jQuery鼠標(biāo)經(jīng)過顯示動畫邊框特效的實(shí)例代碼。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03jQuery實(shí)現(xiàn)定時隱藏對話框的方法分析
這篇文章主要介紹了jQuery實(shí)現(xiàn)定時隱藏對話框的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了jQuery定時隱藏對話框的相關(guān)函數(shù)、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2018-02-02jQuery動態(tài)添加.active 實(shí)現(xiàn)導(dǎo)航效果代碼思路詳解
這篇文章主要介紹了jQuery動態(tài)添加.active 實(shí)現(xiàn)導(dǎo)航效果代碼思路詳解,需要的朋友可以參考下2017-08-08jQuery中parents()和parent()的區(qū)別分析
這篇文章主要介紹了jQuery中parents()和parent()的區(qū)別,具體分析了parents()和parent()的原理與用法區(qū)別,非常具有實(shí)用價值,需要的朋友可以參考下2014-10-10