角度选择/下拉分层数据

时间:2015-12-15 15:12:22

标签: javascript angularjs

我有一个文件夹结构,我想用一个html选择使用angular。

我在下面创建了一个示例,显示了结构并将其输出到嵌套列表结构中。我也把它放到了选择中,但它只显示了顶级文件夹。有没有办法让select显示所有带缩进的文件夹,这样你就可以直观地看到select中的结构?

我可以使用文件夹的深度向文件夹添加另一个属性以帮助缩进。这些文件夹是用户在系统中定义的,因此我不知道在没有查看数据的情况下会有多少级别。

由于

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css" />
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  <script src="script.js"></script>
</head>

<body ng-app="selectFolderApp">

  <div ng-controller="selectFolderController as ctrl">

    <br>{{ ctrl.folderId }}
    <br>

    <br>

    <select class="form-control" ng-model="ctrl.folderId" ng-options="item.id as item.name for item in ctrl.folders">
      <option></option>
    </select>

    <br>

    <script type="text/ng-template" id="folderTree">
      {{item.name}}
      <ul>
        <li ng-repeat="item in item.children" ng-include="'folderTree'"></li>
      </ul>
    </script>

    <ul>
      <li ng-repeat="item in ctrl.folders" ng-include="'folderTree'"></li>
    </ul>

  </div>
</body>

</html>
SELECT Team, Count(distinct Date_Played) AS Count 
FROM NBAGameLog 
GROUP BY Team

1 个答案:

答案 0 :(得分:4)

将文件夹结构转换为平面对象列表,每个对象都有一个level属性。然后,您可以使用此属性在视觉上缩进select元素中的值。

var app = angular.module("selectFolderApp", []);

app.controller("selectFolderController", ['$http', '$scope',
  function($http, $scope) {

    var ctrl = this;

    ctrl.folders = [{
      "name": "Housing",
      "id": "1",
      "children": [{
        "name": "Datasheets",
        "id": "2",
        "children": [{
          "name": "Folder 1",
          "id": "3",
          "children": []
        }, {
          "name": "Folder 2",
          "id": "4",
          "children": []
        }]
      }, {
        "name": "Charts",
        "id": "5",
        "children": [{
          "name": "Folder A",
          "id": "6",
          "children": []
        }, {
          "name": "Folder B",
          "id": "7",
          "children": []
        }, {
          "name": "Folder C",
          "id": "8",
          "children": [{
              "name": "Sub Folder 1",
              "id": "9",
              "children": []
            }, {
              "name": "Sub Folder 2",
              "id": "10",
              "children": []
            }

          ]
        }, {
          "name": "Folder D",
          "id": "11",
          "children": []
        }, {
          "name": "Folder E",
          "id": "12",
          "children": []
        }]
      }]
    }, {
      "name": "Road Works",
      "id": "13",
      "children": [{
        "name": "Datasheets",
        "id": "14",
        "children": [{
          "name": "Folder 1",
          "id": "15",
          "children": [{
            "name": "Sub Folder 1",
            "id": "16",
            "children": []
          }]
        }, {
          "name": "Folder 2",
          "id": "17",
          "children": []
        }, {
          "name": "Folder 3",
          "id": "18",
          "children": []
        }, {
          "name": "Folder 4",
          "id": "19",
          "children": []
        }]
      }, {
        "name": "Charts",
        "id": "20",
        "children": [{
          "name": "Folder A",
          "id": "21",
          "children": []
        }, {
          "name": "Folder B",
          "id": "22",
          "children": []
        }, {
          "name": "Folder C",
          "id": "23",
          "children": []
        }]
      }]
    }];

    var times = function (n, str) {
        var result = '';
        for (var i = 0; i < n; i++) {
            result += str;
        }
        return result;
    };

    var recur = function (item, level, arr) {
        arr.push({
            name: item.name,
            id: item.id,
            level: level,
            indent: times(level, '–')
        });

        if (item.children) {
            item.children.forEach(function (item) {
                recur(item, level + 1, arr);
            });
        }
    };

    ctrl.flatFolders = [];
    ctrl.folders.forEach(function (item) {
        recur(item, 0, ctrl.flatFolders);
    });
  }
]);
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css" />

  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  <script src="script.js"></script>
</head>

<body ng-app="selectFolderApp">

  <div ng-controller="selectFolderController as ctrl">

    <select class="form-control" ng-model="ctrl.folderId">
      <option ng-repeat="item in ctrl.flatFolders" ng-value="{{ item.id }}">
          {{ item.indent }} {{ item.name }}
      </option>
    </select>

  </div>
</body>

</html>