1. 程式人生 > >Bootstrap樹形菜單插件TreeView js使用方法詳解

Bootstrap樹形菜單插件TreeView js使用方法詳解

對象實例 零基礎 結構 spec net table images get num

jQuery多級列表樹插件基於Twitter Bootstrap,以簡單和優雅的方式來顯示一些繼承樹結構,如視圖樹、列表樹等等。

實用Bootstrap樹形菜單特效插件Bootstrap Tree View,非常不錯的Bootstrap插件,現在很多Bootstrap制作的頁面都需要此功能,此插件需要Bootstrap3版本以及jQuery 2.0極以上版本支持,支持眾多參數自定義功能,顏色、背景色、圖標、鏈接等,還是很不錯的。

效果圖:

技術分享圖片


具體使用方法:

插件依賴

Bootstrap v3.0.3
jQuery v2.0.3

以上兩個外部依賴文件已經經過測試可以正常使用,其他版本的Bootstrap需要另行測試。該插件不支持bootstrap 2。

使用方法

首先要在頁面中引入依賴文件和 bootstrap-treeview.js文件。

?
1 2 3 4 5 6 <!-- Required Stylesheets --> <link href="./css/bootstrap.css" rel="stylesheet"> <!-- Required Javascript --> <script src="./js/jquery.js"></script> <script src="./js/bootstrap-treeview.js"></script
>

HTML結構

可以使用任何HTML DOM元素來作為該列表樹的容器:
<div id="tree"></div>

調用插件

該列表樹插件最基本的調用方法如下:

?
1 2 3 4 5 6 function getTree() { // Some logic to retrieve, or generate tree structure return data; } $(‘#tree‘).treeview({data: getTree()});

數據結構

為了創建樹的繼承結構,需要為該列表樹插件提供一個嵌套結構的js對象。例如:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 var tree = [ { text: "Parent 1", nodes: [ { text: "Child 1", nodes: [ { text: "Grandchild 1" }, { text: "Grandchild 2" } ] }, { text: "Child 2" } ] }, { text: "Parent 2" }, { text: "Parent 3" }, { text: "Parent 4" }, { text: "Parent 5" } ];

最簡單的樹結構可以只有一個節點,使用一個帶text屬性的js對象來實現即可:

?
1 2 3 { text: "Node 1" }

如果你需要自定義更多的內容,可以參考下面:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 { text: "Node 1", icon: "glyphicon glyphicon-stop", selectedIcon: "glyphicon glyphicon-stop", color: "#000000", backColor: "#FFFFFF", href: "#node-1", selectable: true, state: { checked: true, disabled: true, expanded: true, selected: true }, tags: [‘available‘], nodes: [ {}, ... ] }

全局參數

參數可以定制treeview的默認外觀和行為。參數使用一個對象來在插件初始化時傳入:

?
1 2 3 4 5 6 7 8 // Example: initializing the treeview // expanded to 5 levels // with a background color of green $(‘#tree‘).treeview({ data: data, // data is not optional levels: 5, backColor: ‘green‘ });

可用方法

你可以通過兩種方式來調用方法:
1、插件包裝器:插件的包裝器可以作為訪問底層方法的代理。
$(‘#tree‘).treeview(‘methodName‘, args)
多個參數必須使用數組對象來傳入。

2、直接使用treeview:你可以通過下面兩種方法中的一種來獲取treeview對象實例。

?
1 2 3 4 5 6 7 8 //該方法返回一個treeview的對象實例 $(‘#tree‘).treeview(true) .methodName(args); //對象實例也保存在DOM元素的data中, //可以使用‘treeview‘的id來訪問它。 $(‘#tree‘).data(‘treeview‘) .methodName(args);

如果大家還想深入學習,可以點擊這裏進行學習,再為大家附兩個精彩的專題:Bootstrap學習教程 Bootstrap實戰教程 Bootstrap插件使用教程

源碼下載:http://xiazai.jb51.net/201611/yuanma/bootstraptreeview(jb51.net).rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://www.cnblogs.com/captainbed

Bootstrap樹形菜單插件TreeView js使用方法詳解