Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html ngRepeat将同一属性分组,不带其他库_Html_Angularjs_Angularjs Ng Repeat - Fatal编程技术网

Html ngRepeat将同一属性分组,不带其他库

Html ngRepeat将同一属性分组,不带其他库,html,angularjs,angularjs-ng-repeat,Html,Angularjs,Angularjs Ng Repeat,我有一系列具有属性的对象,其中一些对象在名为“region”的属性中具有相同的值。我希望按区域属性对这些对象进行分组(即,如果它们位于同一区域,则将它们分组在一起),并在不同的表中显示来自这些对象的区域数量。我不知道如何在ng repeat中执行此操作。以下是对象列表的示例: [{"name": 000, "region": cus, "weight": 1}, {"name": 001, "region": cus, "weight": 2}, {"name": 003, "region":

我有一系列具有属性的对象,其中一些对象在名为“region”的属性中具有相同的值。我希望按区域属性对这些对象进行分组(即,如果它们位于同一区域,则将它们分组在一起),并在不同的表中显示来自这些对象的区域数量。我不知道如何在ng repeat中执行此操作。以下是对象列表的示例:

[{"name": 000, "region": cus, "weight": 1}, {"name": 001, "region": cus, "weight": 2}, {"name": 003, "region": wus, "weight": 2}]
目标是在一个表标签上进行ng重复,这样表由区域“cus”和“wus”生成,这两个表在cus表中有000和001,在wus表中有002

出于我在这里不会解释的原因,我不希望使用AngularJS的另一个库来实现这个期望的结果。很多人都建议可以添加groupBy属性,但有没有办法只使用本机Angular代码就可以做到这一点

非常感谢您的帮助。

angular.module('app',['angular.filter']))
主要功能($scope){
变量a=[{“名称”:000,地区:'cus',“权重”:1},{“名称”:001,地区:'cus',“权重”:2},{“名称”:003,地区:'wus',“权重”:2}]
$scope.a=a;
}

组名:{{key}
名称
重量
{{item.name}
{{item.weight}}

您将需要转换初始数组以创建它的分组版本。这很容易做到。例如:

$scope.regions = [{"name": "000", "region": "cus", "weight": 1}, {"name": "001", "region": "cus", "weight": 2}, {"name": "003", "region": "wus", "weight": 2}];

$scope.groupedRegions = $scope.regions.reduce(function(prev, curr) {
    if (!prev[curr.region]) prev[curr.region] = [];
    prev[curr.region].push(curr);
    return prev;
}, {});
然后渲染它:

<table ng-repeat="(key, value) in groupedRegions" class="table table-bordered">
    <tbody>
        <tr>
            <td>{{key}}</td>
            <td>
                <table>
                    <tbody>
                        <tr ng-repeat="region in value">
                            <td>{{region.name}}</td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>

{{key}}
{{region.name}
演示:

ng repeat=“data.weight as data.name group by data.region for data in data”ng repeat=“item.setInValue as item.label group by item.whather for items in items”可能重复: