首頁  >  文章  >  web前端  >  bootstrap中外掛程式treeview的介紹與使用

bootstrap中外掛程式treeview的介紹與使用

零下一度
零下一度原創
2018-05-14 14:56:464202瀏覽

專案需要實作權限管理,使用前端框架bootstrap,所以就直接選用了bootstrap的treeview擴充插件。先上圖:

選取父節點時,父節點下所有子節點也全部選中,看程式碼

1 、HTML代碼

<h2>TreeView Checkable</h2>
<p id="treeview-checkable"></p>

2、Json資料

function getTvStateData() {
   var defaultData = [
    {
     text: &#39;Parent 1&#39;,
     href: &#39;#parent1&#39;,
     tags: [&#39;4&#39;],
     state: {
      checked: true
     },
     nodes: [
      {
       text: &#39;Child 1&#39;,
       href: &#39;#child1&#39;,
       tags: [&#39;2&#39;],
       nodes: [
        {
         text: &#39;Grandchild 1&#39;,
         href: &#39;#grandchild1&#39;,
         tags: [&#39;0&#39;]
        },
        {
         text: &#39;Grandchild 2&#39;,
         href: &#39;#grandchild2&#39;,
         tags: [&#39;0&#39;]
        }
       ]
      },
      {
       text: &#39;Child 2&#39;,
       href: &#39;#child2&#39;,
       tags: [&#39;0&#39;]
      }
     ]
    },
    {
     text: &#39;Parent 2&#39;,
     href: &#39;#parent2&#39;,
     tags: [&#39;0&#39;],
     nodes: [
      {
       text: &#39;Child 1&#39;,
       href: &#39;#child1&#39;,
       tags: [&#39;2&#39;],
       nodes: [
        {
         text: &#39;Grandchild 1&#39;,
         href: &#39;#grandchild1&#39;,
         tags: [&#39;0&#39;]
        },
        {
         text: &#39;Grandchild 2&#39;,
         href: &#39;#grandchild2&#39;,
         tags: [&#39;0&#39;]
        }
       ]
      },
      {
       text: &#39;Child 2&#39;,
       href: &#39;#child2&#39;,
       tags: [&#39;0&#39;]
      }
     ]
    },
    {
     text: &#39;Parent 3&#39;,
     href: &#39;#parent3&#39;
    },
    {
     text: &#39;Parent 4&#39;,
     href: &#39;#parent4&#39;,
     tags: [&#39;0&#39;]
    },
    {
     text: &#39;Parent 5&#39;,
     href: &#39;#parent5&#39;,
     tags: [&#39;0&#39;]
    }
   ];

   return defaultData;
  }

3、JS資料綁定,載入TreeView

$(function() {
 var $checkableTree = $(&#39;#treeview-checkable&#39;)
    .treeview({
     data: getTvStateData(), //数据
     showIcon: false,
     showCheckbox: true,
     levels: 1,
     onNodeChecked: function(event, node) { //选中节点
      var selectNodes = getChildNodeIdArr(node); //获取所有子节点
      if (selectNodes) { //子节点不为空,则选中所有子节点
       $(&#39;#treeview-checkable&#39;).treeview(&#39;checkNode&#39;, [selectNodes, { silent: true }]);
      }
      var parentNode = $("#treeview-checkable").treeview("getNode", node.parentId);
      setParentNodeCheck(node);
     },
     onNodeUnchecked: function(event, node) { //取消选中节点
      var selectNodes = getChildNodeIdArr(node); //获取所有子节点
      if (selectNodes) { //子节点不为空,则取消选中所有子节点
       $(&#39;#treeview-checkable&#39;).treeview(&#39;uncheckNode&#39;, [selectNodes, { silent: true }]);
      }
     },
     onNodeExpanded:
      function(event, data) {
       if (data.nodes === undefined || data.nodes === null) {
        
       } else if (data.tags[0] === "2") {
        alert("Tags 2");
       } else {
        alert("1111");
       }
      }
    });
 });

4、選取/取消選取父節點時,選取/取消選取所有子節點,以及選取所有子節點時,選取父節點

function getChildNodeIdArr(node) {
   var ts = [];
   if (node.nodes) {
    for (x in node.nodes) {
     ts.push(node.nodes[x].nodeId);
     if (node.nodes[x].nodes) {
      var getNodeDieDai = getChildNodeIdArr(node.nodes[x]);
      for (j in getNodeDieDai) {
       ts.push(getNodeDieDai[j]);
      }
     }
    }
   } else {
    ts.push(node.nodeId);
   }
   return ts;
  }

  function setParentNodeCheck(node) {
   var parentNode = $("#treeview-checkable").treeview("getNode", node.parentId);
   if (parentNode.nodes) {
    var checkedCount = 0;
    for (x in parentNode.nodes) {
     if (parentNode.nodes[x].state.checked) {
      checkedCount ++;
     } else {
      break;
     }
    }
    if (checkedCount === parentNode.nodes.length) {
     $("#treeview-checkable").treeview("checkNode", parentNode.nodeId);
     setParentNodeCheck(parentNode);
    }
   }
  }

以上是bootstrap中外掛程式treeview的介紹與使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn