角树控件无法正常工作

时间:2018-08-09 03:59:27

标签: angularjs treecontrol

我正在使用https://wix.github.io/angular-tree-control/#multi-selection来处理树结构示例。我没有获得正确格式的预期结果。下面是代码,并在其上附加了plunkr。

 <html>
 <head>
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <script src="https://wix.github.io/angular-tree-control/angular-tree-control.js"></script>
 <link href="https://wix.github.io/angular-tree-control/css/tree-control.css" />
 <link href="https://wix.github.io/angular-tree-control/css/tree-control-attribute.css" />
 </head>
 <div ng-app="app">
 <div ng-controller="myController">
 <treecontrol class="tree-classic"
   tree-model="dataForTheTree"
   options="treeOptions"
   on-selection="showSelected(node)"
   selected-node="node1">
   employee: {{node.name}} age {{node.age}}
 </treecontrol>
 </div>
 </div>

  angular.module('app', ['treeControl'])
 .controller('myController', function() {
 $scope.treeOptions = {
 nodeChildren: "children",
 dirSelectable: true,
 injectClasses: {
    ul: "a1",
    li: "a2",
    liSelected: "a7",
    iExpanded: "a3",
    iCollapsed: "a4",
    iLeaf: "a5",
    label: "a6",
    labelSelected: "a8"
  }
 }
$scope.dataForTheTree =
[
    { "name" : "Joe", "age" : "21", "children" : [
    { "name" : "Smith", "age" : "42", "children" : [] },
    { "name" : "Gary", "age" : "21", "children" : [
        { "name" : "Jenifer", "age" : "23", "children" : [
            { "name" : "Dani", "age" : "32", "children" : [] },
            { "name" : "Max", "age" : "34", "children" : [] }
        ]}
    ]}
]},
{ "name" : "Albert", "age" : "33", "children" : [] },
{ "name" : "Ron", "age" : "29", "children" : [] }
];
});
Here is the attached plunkr https://plnkr.co/edit/bQHOIQ2HDPr4WJaukB8I?p=preview

1 个答案:

答案 0 :(得分:2)

我注意到缺少两件事:

  1. 上下文菜单模块缺失。
  2. 添加的CSS链接中没有rel="stylesheet"

即使angular-tree-control readme说:

  

如果要使用menu-id属性,请包含上下文菜单模块

我发现他们的GitHub页面上报告了诸如this one之类的问题,这是由于缺少上下文菜单模块所致。

添加以下行以包含上下文菜单模块:

<script type="text/javascript" src="https://wix.github.io/angular-tree-control/context-menu.js"></script>

还请更新链接元素,使其包含rel属性,如下所示:

<link rel="stylesheet" href="https://wix.github.io/angular-tree-control/css/tree-control.css" />
<link rel="stylesheet" href="https://wix.github.io/angular-tree-control/css/tree-control-attribute.css" />

不需要像在Plunker中那样添加CSS,它是上述链接的重复部分。

希望这可以解决您的问题。这是一个工作示例的完整代码:

angular.module('app', ['treeControl'])
  .controller('myController', ['$scope', function($scope) {
    $scope.treeOptions = {
      nodeChildren: "children",
      dirSelectable: true,
      injectClasses: {
        ul: "a1",
        li: "a2",
        liSelected: "a7",
        iExpanded: "a3",
        iCollapsed: "a4",
        iLeaf: "a5",
        label: "a6",
        labelSelected: "a8"
      }
    }
    $scope.dataForTheTree = [{
        "name": "Joe",
        "age": "21",
        "children": [{
            "name": "Smith",
            "age": "42",
            "children": []
          },
          {
            "name": "Gary",
            "age": "21",
            "children": [{
              "name": "Jenifer",
              "age": "23",
              "children": [{
                  "name": "Dani",
                  "age": "32",
                  "children": []
                },
                {
                  "name": "Max",
                  "age": "34",
                  "children": []
                }
              ]
            }]
          }
        ]
      },
      {
        "name": "Albert",
        "age": "33",
        "children": []
      },
      {
        "name": "Ron",
        "age": "29",
        "children": []
      }
    ];
  }]);
treecontrol.tree-classic li .full {
  display: none;
}

treecontrol.tree-classic li .empty {
  display: inline;
}

treecontrol.tree-classic li .tree-selected .full {
  display: inline;
}

treecontrol.tree-classic li .tree-selected .empty {
  display: none;
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script type="text/javascript" src="https://wix.github.io/angular-tree-control/angular-tree-control.js"></script>
  <script type="text/javascript" src="https://wix.github.io/angular-tree-control/context-menu.js"></script>
  <link rel="stylesheet" href="https://wix.github.io/angular-tree-control/css/tree-control.css" />
  <link rel="stylesheet" href="https://wix.github.io/angular-tree-control/css/tree-control-attribute.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
</head>

<body ng-app="app">
  <div ng-controller="myController">
    <treecontrol class="tree-classic" tree-model="dataForTheTree" options="treeOptions" on-selection="showSelected(node)" selected-node="node1">
      <span class="fa fa-square-o empty"></span>
      <span class="fa fa-square full"></span> employee: {{node.name}} age {{node.age}}
    </treecontrol>
  </div>

</body>

</html>

2018年10月8日更新:编辑了代码段以使用FontAwsome添加复选框。