創建非同步樹形菜單 <ul class="easyui-tree" id="treeMenu"> </ul> $(function(){ $('#treeMenu').tree({ url:'tree_data.json' //url的值是非同步獲取數據的頁面地址 }); }); $result = [ ...
創建非同步樹形菜單
- 創建樹形菜單的ul標簽
<ul class="easyui-tree" id="treeMenu"> </ul>
View Code - 寫js代碼,對菜單的ul標簽元素使用tree函數
$(function(){ $('#treeMenu').tree({ url:'tree_data.json' //url的值是非同步獲取數據的頁面地址 }); });
View Code - 寫用來非同步獲取數據的php頁面(tree_data.json頁面)。
返回的需是Json值(此處用數組代替從資料庫獲取的數據,以省略連接資料庫過程)$result = []; //節點一 $prodArr = [ "id" => 1, "text" => "商品管理", "state" => "open", "children" => [ [ "id" => 2, "text" => "添加商品", "state" => "open", "attributes" => [ "url" => "http://abc.com/test.php" ] ], [ "id" => 3, "text" => "修改商品", "state" => "open", "attributes" => [ "url" => "http://abc.com/test2.php" ] ] ] ]; //節點二 $newsArr = [ "id" => 10, "text" => "新聞管理", "state" => "open", "children" => [ [ "id" => 12, "text" => "添加新聞", "state" => "open" ], [ "id" => 13, "text" => "修改新聞", "state" => "open" ] ] ]; //節點三 $userArr = [ "id" => 20, "text" => "用戶管理", "state" => "open", "children" => [ [ "id" => 22, "text" => "添加用戶", "state" => "open" ], [ "id" => 23, "text" => "修改用戶", "state" => "open" ] ] ]; Array_push($result, $prodArr, $newsArr, $userArr); echo json_encode($result);
View Code說明:
節點的屬性:
id:節點ID,可以作為參數來非同步向頁面地址獲取子節點數據
text:節點文本
state:節點狀態。取值為open(預設預設值)或close。當設置為close時,會自動非同步獲取子節點的數據
checked:指明節點是否被選中。
attributes:自定義屬性
children:以數組的形式包含子節點 (更多細節知識可查看easyui官網中tree知識點)
到這,非同步樹形菜單就完成了。
動態添加標簽頁tab
- 創建用來包裹標簽頁tab的外層標簽
<div id="contentTabs" class="easyui-tabs" style="width:100%;height:100%;"> </div>
View Code - 在js中定義addTab函數
function addTab(title, url){ if ($('#contentTabs').tabs('exists', title)){ $('#contentTabs').tabs('select', title); } else { var content = '<iframe scrolling="auto" frameborder="0" src="'+url+'" style="width:100%;height:100%;"></iframe>'; $('#contentTabs').tabs('add',{ title:title, content:content, closable:true }); } }
View Code - 在樹形菜單的點擊事件函數中調用addTab函數
$(function(){ $("#treeMenu").tree({ onClick:function(node){ if (node.attributes !== undefined && node.attributes.url !== undefined) { addTab(node.text,node.attributes.url); } } }); });
View Code
最後,如圖顯示