這篇文章主要介紹了bootstrap中treeview擴展addNode方法動態添加子節點的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
bootstrap-treeview是一款效果非??岬幕赽ootstrap的jQuery多級列表樹插件。該jQuery插件基于Twitter Bootstrap,以簡單和優雅的方式來顯示一些繼承樹結構,如視圖樹、列表樹等等。
本文只是詳細說明對bootstrap-treeview添加子節點的擴展方法(addNode),如了解bootstrap-treeview所有用法請看官方API
官方api https://www.npmjs.com/package/bootstrap-treeview (點擊新窗口打開)
使用過程中,需要動態添加子節點。發現api中沒有此功能。找了很多資料也沒有發現有相關的方法。
又不想放棄使用它,看來只能自己寫的。先讀他們的源代碼,看他們的邏輯關系,然后就下手自己寫一下。不多說,直接上代碼
第一步:在Tree主函數return {/*在這里添加addNode的入口*/}
看圖比較直觀
附上代碼:
addNode: $.proxy(this.addNode, this),
第二步:添加Tree的prototype方法
/** 給節點添加子節點 @param {Object|Number} identifiers - A valid node, node id or array of node identifiers @param {optional Object} options.node; */ Tree.prototype.addNode = function (identifiers, options) { this.forEachIdentifier(identifiers, options, $.proxy(function (node, options) { this.setAddNode(node, options); }, this)); this.setInitialStates({ nodes: this.tree }, 0); this.render(); } /** * 添加子節點 */ Tree.prototype.setAddNode = function (node, options) { if (node.nodes == null) node.nodes = []; if (options.node) { node.nodes.push(options.node); }; };
看圖:
第三步:就是如何使用了。
$("#Treeview01").treeview("addNode", [2, { node: { text: "新加的菜單", href: "001005" } }]);
注意 $("#Treeview01")使用data已初始化過的
下面看個實例TreeView動態添加節點
遍歷某路徑下的文件夾添加父節點,遍歷文件夾下的文件添加子節點
private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog fd = new FolderBrowserDialog(); if (fd.ShowDialog() == DialogResult.OK) { // 在此添加代碼,選擇的路徑為 folderBrowserDialog1.SelectedPath if (Directory.Exists(fd.SelectedPath)) { DirectoryInfo thisOne = new DirectoryInfo(fd.SelectedPath); DirectoryInfo[] directoryInfo = thisOne.GetDirectories(); for (int i = 0; i < directoryInfo.Length; i++) { TreeNode root = new TreeNode(directoryInfo[i].ToString());//創建節點 root.Name = directoryInfo[i].ToString(); //為節點取個名字,這兒創建的是根節點 root.Tag = 0; treeView1.Nodes.Add(root); //將節點添加到treeView1上 DirectoryInfo thisTwo = new DirectoryInfo(fd.SelectedPath + "\\" + directoryInfo[i].ToString()); FileInfo[] fileInfo = thisTwo.GetFiles(); for (int j = 0; j < fileInfo.Length; j++) { TreeNode node = new TreeNode(fileInfo[j].ToString()); node.Tag = 1; node.Name = fileInfo[j].ToString(); if (!root.Nodes.ContainsKey(node.Name)) { root.Nodes.Add(node); } } } } } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“bootstrap中treeview擴展addNode方法動態添加子節點的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。