【问题标题】:AngularJS displaying hierarchical dataAngularJS 显示分层数据
【发布时间】:2015-10-11 18:03:35
【问题描述】:

我正在尝试在分层视图中显示数据列表。 我的数据如下所示:

items:[
  {
    "model_id": "1",
    "model_type_id": "1",
    "name": "Parent 1",
    "model_parent_id": ""
  },
  {
    "model_id": "2",
    "model_type_id": "1",
    "name": "Parent 2",
    "model_parent_id": ""
  },
  {
    "model_id": "3",
    "model_type_id": "2",
    "name": "Child 1",
    "model_parent_id": "1"
  },
  {
    "model_id": "4",
    "model_type_id": "2",
    "name": "Child 2",
    "model_parent_id": "2"
  }
]

我的控制器看起来像:

myApp.controller('ModelController', ['$scope', 'ModelFactory',
    function ($scope, ModelFactory) {

        $scope.init = function (id) {
            $scope.brandId = id;
            getModels($scope.brandId);
        };

        function getModels(brandId) {

            ModelFactory.GetModels(brandId)
                .success(function (mdls) {
                    $scope.models = mdls;
                    console.log($scope.mdls);
                })
                .error(function (error) {
                    $scope.status = 'Unable to load model data: ' + error.message;
                    console.log($scope.status);
                });
        };
    }
]);

我的 HTML 看起来像:

<div ng-controller="ModelController" ng-init="init(brand.ID)">
    <ul ng-sortable class="block__list block__list_words">
        <li ng-repeat="model in models | filter: {model_type_id:1} ">{{model.model_name}} - {{model.model_id}}

            <div ng-controller="ModelController" ng-init="init(brand.ID)">
                <ul ng-sortable class="block__list block__list_words">
                    <li ng-repeat="m in models | filter: {model_type_id:2} | filter:{model_parent_id:model.model_id}">
                        {{m.model_name}} - {{m.model_parent_id}}
                    </li>
                </ul>
            </div>

        </li>
    </ul>
</div>

在我尝试使用外部控制器过滤内部控制器时,过滤器不起作用。我让两个孩子都显示在每个父母下方。我怎样才能得到它以便显示父母,并且只显示孩子的model_parent_id等于父母的model_id的孩子?

【问题讨论】:

    标签: angularjs filtering


    【解决方案1】:

    虽然我不确定是否有办法使用过滤器来实现这一点,但显示嵌套数据的正常方法是重新组织数据结构以反映您想要显示的内容。

    items:[
      {
        "model_id": "1",
        "model_type_id": "1",
        "name": "Parent 1",
        "children": [{
          "model_id": "3",
          "model_type_id": "2",
          "name": "Child 1"
        }]
      },
      {
        "model_id": "2",
        "model_type_id": "1",
        "name": "Parent 2",
        "children": [{
          "model_id": "3",
          "model_type_id": "2",
          "name": "Child 2"
        }]
      }
    ]
    

    然后使用嵌套的 ng-repeat 显示它们

    <ul>
      <li ng-repeat="parent in items">
        {{parent.name}} - {{parent.model_id}}
        <ul>
          <li ng-repeat="child in parent.children">
            {{child.name}} - {{child.model_id}}
          </li>
        </ul>
      </li>
    </ul>
    

    注意:不需要使用嵌套控制器,在顶层一个就足够了。如果你需要递归使用一些共享逻辑,使用自定义指令来替换 li。


    要重新组织数据,您可以在服务器端或客户端进行。 下面展示了如何在客户端进行,因为我们可能没有权限更改服务器端 API。

    $scope.data = [];
    angular.forEach(items, function(item) {
        if (item.model_parent_id == "") {
            $scope.data.push(item);
        }  
    });
    
    // add all parents first in case some children comes before parent
    angular.forEach(items, function(item) {
        if (item.model_parent_id == "") continue;
    
        // search for parent
        angular.forEach($scope.data, function(parent) {
            if (parent.model_id == item.model_parent_id) {
                if (!parent.children) parent.children = [];
                parent.children.push(item);
            }
        }
    });
    

    【讨论】:

    • 这在我的情况下可能会很困难。数据存储在数据库中的一个表中。我有一个 .net 应用程序并创建一个函数来检索数据,然后将其正确存储在 JSON 对象中,这比我想要的过滤方式更耗时。
    • 获取数据后可以在javascript中进行数据处理,后面我会加一个例子。
    猜你喜欢
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    相关资源
    最近更新 更多