如何使用 angularjs 根据名称过滤和分组数据?
How to filter and group the data based on name using angularjs?
我有以下属于每个名称的描述列表。请让我知道如何分组或列出每个名称的描述。
html:
<body ng-app="app" ng-controller="MainCtrl">
<div ng-repeat="nameGroup in loopData"> <!-- this ng-repeat should be available as per my requirement -->
<div ng-repeat="loopData in nameGroup.values track by $index" ><!-- this ng-repeat should be available as per my requirement -->
<!-- {{loopData}} -->
<div class="text-center merged">{{loopData.name}}</div><!-- ng-if="$index === 0" -->
<div class="text-center">{{loopData.description}}</div>
</div>
</div>
</body>
控制器:
var app = angular.module('app', ['angular.filter']);
app.controller('MainCtrl', function($scope) {
$scope.finalLoopData = {};
$scope.loopData = [{ "country":"Abc", "id":"001", "mynumbers":[],
"values": [
{"description": "first desc", "name": "First Test Name"},
{"description": "second desc", "name": "First Test Name"},
{"description": "third desc", "name": "First Test Name"},
{"description": "fourth desc", "name": "Second Test Name"},
{"description": "fifth desc", "name": "Second Test Name"},
{"description": "sixth desc", "name": "Third Test Name"},
{"description": "seventh desc", "name": "Third Test Name"},
{"description": "eighth desc", "name": "Third Test Name"},
{"description": "ninth desc", "name": "Third Test Name"},
{"description": "tenth desc", "name": "Third Test Name"},
{"description": "eleventh desc", "name": "Fourth Test Name"},
{"description": "twelfth desc", "name": "Fourth Test Name"}
]
}];
$scope.arrayToObject = function () {
var finalLoopData = {};
angular.forEach($scope.loopData[0].values, function (value, key) {
//console.log(value);//gives all value objects
if (!finalLoopData[value.name]) {
finalLoopData[value.name] = new Array();
//console.log("name: "+value.name);//First Test Name //Second Test Name //Third Test Name //Fourth Test Name
}
finalLoopData[value.name].push(value);
});
$scope.finalLoopData = finalLoopData;
}
$scope.arrayToObject();
});
如果我 运行 这段代码,我会得到带有名称的个人描述,但不会带有组名,这是我的预期输出,类似于以下内容:
第一个测试名称
第一个描述
第二个描述
- 第三个描述
第二个测试名称
倒数第四
倒数第五
第三个测试名称
倒数第六
倒数第7
第八小节
倒数第9
- 第 10 个
第四个测试名称
第十一栏
倒数第十二
已创建 Filter:
已更新
如果您无法更改模板中的循环,请考虑以下事项:
模板
<body ng-app="app" ng-controller="MainCtrl">
<!-- Original loops -->
<div ng-repeat="nameGroup in loopData">
<div ng-repeat="loopData in nameGroup.values track by $index">
<!-- Data name -->
<strong>{{loopData.name}}</strong>
<!-- Loop data descriptions -->
<ul ng-repeat="description in loopData.descriptions">
<li>{{description}}</li>
</ul>
</div>
</div>
</body>
控制器
var app = angular.module('app', ['angular.filter']);
app.controller('MainCtrl', function($scope) {
$scope.loopData = [
{
country: "Abc",
id: "001",
mynumbers: [],
values: [
{ description: "first desc", name: "First Test Name" },
{ description: "second desc", name: "First Test Name" },
{ description: "third desc", name: "First Test Name" },
{ description: "fourth desc", name: "Second Test Name" },
{ description: "fifth desc", name: "Second Test Name" },
{ description: "sixth desc", name: "Third Test Name" },
{ description: "seventh desc", name: "Third Test Name" },
{ description: "eighth desc", name: "Third Test Name" },
{ description: "ninth desc", name: "Third Test Name" },
{ description: "tenth desc", name: "Third Test Name" },
{ description: "eleventh desc", name: "Fourth Test Name" },
{ description: "twelfth desc", name: "Fourth Test Name" }
]
}
];
this.$onInit = function() {
$scope.loopData.forEach(function(data) {
const groupedValues = {};
// group values by name
data.values.forEach(function(value) {
// initialize name group
if (!groupedValues[value.name]) {
groupedValues[value.name] = {
name: value.name,
descriptions: []
};
}
// add description to name group
groupedValues[value.name].descriptions.push(value.description);
});
// update values
data.values = Object.values(groupedValues);
});
};
});
请注意,$onInit
函数会将 $scope.loopData
转换为以下内容:
[
{
"country": "Abc",
"id": "001",
"mynumbers": [],
"values": [
{
"name": "First Test Name",
"descriptions": [
"first desc",
"second desc",
"third desc"
]
},
{
"name": "Second Test Name",
"descriptions": [
"fourth desc",
"fifth desc"
]
},
{
"name": "Third Test Name",
"descriptions": [
"sixth desc",
"seventh desc",
"eighth desc",
"ninth desc",
"tenth desc"
]
},
{
"name": "Fourth Test Name",
"descriptions": [
"eleventh desc",
"twelfth desc"
]
}
]
}
]
原回答
在您的 html 中,您没有循环处理已处理的 finalLoopData
变量。此外,要显示一次组名,您需要在第一个循环(而不是第二个)中使用它。
考虑以下代码:
模板
<body ng-app="app" ng-controller="MainCtrl">
<!-- Loop final data -->
<div ng-repeat="nameGroup in finalLoopData">
<!-- Group name -->
<strong>{{nameGroup.name}}</strong>
<!-- Loop group descriptions -->
<ul ng-repeat="description in nameGroup.descriptions track by $index">
<li>{{description}}</li>
</ul>
</div>
</body>
控制器
var app = angular.module('app', ['angular.filter']);
app.controller('MainCtrl', function($scope) {
$scope.finalLoopData = {};
$scope.loopData = [
{
country: "Abc",
id: "001",
mynumbers: [],
values: [
{ description: "first desc", name: "First Test Name" },
{ description: "second desc", name: "First Test Name" },
{ description: "third desc", name: "First Test Name" },
{ description: "fourth desc", name: "Second Test Name" },
{ description: "fifth desc", name: "Second Test Name" },
{ description: "sixth desc", name: "Third Test Name" },
{ description: "seventh desc", name: "Third Test Name" },
{ description: "eighth desc", name: "Third Test Name" },
{ description: "ninth desc", name: "Third Test Name" },
{ description: "tenth desc", name: "Third Test Name" },
{ description: "eleventh desc", name: "Fourth Test Name" },
{ description: "twelfth desc", name: "Fourth Test Name" }
]
}
];
this.$onInit = function() {
const finalLoopData = {};
$scope.loopData[0].values.forEach(function(item) {
// initialize name group
if (!finalLoopData[item.name]) {
finalLoopData[item.name] = {
name: item.name,
descriptions: []
};
}
// add description to name group
finalLoopData[item.name].descriptions.push(item.description);
});
// apply results
$scope.finalLoopData = finalLoopData;
};
});
我有以下属于每个名称的描述列表。请让我知道如何分组或列出每个名称的描述。
html:
<body ng-app="app" ng-controller="MainCtrl">
<div ng-repeat="nameGroup in loopData"> <!-- this ng-repeat should be available as per my requirement -->
<div ng-repeat="loopData in nameGroup.values track by $index" ><!-- this ng-repeat should be available as per my requirement -->
<!-- {{loopData}} -->
<div class="text-center merged">{{loopData.name}}</div><!-- ng-if="$index === 0" -->
<div class="text-center">{{loopData.description}}</div>
</div>
</div>
</body>
控制器:
var app = angular.module('app', ['angular.filter']);
app.controller('MainCtrl', function($scope) {
$scope.finalLoopData = {};
$scope.loopData = [{ "country":"Abc", "id":"001", "mynumbers":[],
"values": [
{"description": "first desc", "name": "First Test Name"},
{"description": "second desc", "name": "First Test Name"},
{"description": "third desc", "name": "First Test Name"},
{"description": "fourth desc", "name": "Second Test Name"},
{"description": "fifth desc", "name": "Second Test Name"},
{"description": "sixth desc", "name": "Third Test Name"},
{"description": "seventh desc", "name": "Third Test Name"},
{"description": "eighth desc", "name": "Third Test Name"},
{"description": "ninth desc", "name": "Third Test Name"},
{"description": "tenth desc", "name": "Third Test Name"},
{"description": "eleventh desc", "name": "Fourth Test Name"},
{"description": "twelfth desc", "name": "Fourth Test Name"}
]
}];
$scope.arrayToObject = function () {
var finalLoopData = {};
angular.forEach($scope.loopData[0].values, function (value, key) {
//console.log(value);//gives all value objects
if (!finalLoopData[value.name]) {
finalLoopData[value.name] = new Array();
//console.log("name: "+value.name);//First Test Name //Second Test Name //Third Test Name //Fourth Test Name
}
finalLoopData[value.name].push(value);
});
$scope.finalLoopData = finalLoopData;
}
$scope.arrayToObject();
});
如果我 运行 这段代码,我会得到带有名称的个人描述,但不会带有组名,这是我的预期输出,类似于以下内容:
第一个测试名称
第一个描述
第二个描述
- 第三个描述
第二个测试名称
倒数第四
倒数第五
第三个测试名称
倒数第六
倒数第7
第八小节
倒数第9
- 第 10 个
第四个测试名称
第十一栏
倒数第十二
已创建 Filter:
已更新
如果您无法更改模板中的循环,请考虑以下事项:
模板
<body ng-app="app" ng-controller="MainCtrl">
<!-- Original loops -->
<div ng-repeat="nameGroup in loopData">
<div ng-repeat="loopData in nameGroup.values track by $index">
<!-- Data name -->
<strong>{{loopData.name}}</strong>
<!-- Loop data descriptions -->
<ul ng-repeat="description in loopData.descriptions">
<li>{{description}}</li>
</ul>
</div>
</div>
</body>
控制器
var app = angular.module('app', ['angular.filter']);
app.controller('MainCtrl', function($scope) {
$scope.loopData = [
{
country: "Abc",
id: "001",
mynumbers: [],
values: [
{ description: "first desc", name: "First Test Name" },
{ description: "second desc", name: "First Test Name" },
{ description: "third desc", name: "First Test Name" },
{ description: "fourth desc", name: "Second Test Name" },
{ description: "fifth desc", name: "Second Test Name" },
{ description: "sixth desc", name: "Third Test Name" },
{ description: "seventh desc", name: "Third Test Name" },
{ description: "eighth desc", name: "Third Test Name" },
{ description: "ninth desc", name: "Third Test Name" },
{ description: "tenth desc", name: "Third Test Name" },
{ description: "eleventh desc", name: "Fourth Test Name" },
{ description: "twelfth desc", name: "Fourth Test Name" }
]
}
];
this.$onInit = function() {
$scope.loopData.forEach(function(data) {
const groupedValues = {};
// group values by name
data.values.forEach(function(value) {
// initialize name group
if (!groupedValues[value.name]) {
groupedValues[value.name] = {
name: value.name,
descriptions: []
};
}
// add description to name group
groupedValues[value.name].descriptions.push(value.description);
});
// update values
data.values = Object.values(groupedValues);
});
};
});
请注意,$onInit
函数会将 $scope.loopData
转换为以下内容:
[
{
"country": "Abc",
"id": "001",
"mynumbers": [],
"values": [
{
"name": "First Test Name",
"descriptions": [
"first desc",
"second desc",
"third desc"
]
},
{
"name": "Second Test Name",
"descriptions": [
"fourth desc",
"fifth desc"
]
},
{
"name": "Third Test Name",
"descriptions": [
"sixth desc",
"seventh desc",
"eighth desc",
"ninth desc",
"tenth desc"
]
},
{
"name": "Fourth Test Name",
"descriptions": [
"eleventh desc",
"twelfth desc"
]
}
]
}
]
原回答
在您的 html 中,您没有循环处理已处理的 finalLoopData
变量。此外,要显示一次组名,您需要在第一个循环(而不是第二个)中使用它。
考虑以下代码:
模板
<body ng-app="app" ng-controller="MainCtrl">
<!-- Loop final data -->
<div ng-repeat="nameGroup in finalLoopData">
<!-- Group name -->
<strong>{{nameGroup.name}}</strong>
<!-- Loop group descriptions -->
<ul ng-repeat="description in nameGroup.descriptions track by $index">
<li>{{description}}</li>
</ul>
</div>
</body>
控制器
var app = angular.module('app', ['angular.filter']);
app.controller('MainCtrl', function($scope) {
$scope.finalLoopData = {};
$scope.loopData = [
{
country: "Abc",
id: "001",
mynumbers: [],
values: [
{ description: "first desc", name: "First Test Name" },
{ description: "second desc", name: "First Test Name" },
{ description: "third desc", name: "First Test Name" },
{ description: "fourth desc", name: "Second Test Name" },
{ description: "fifth desc", name: "Second Test Name" },
{ description: "sixth desc", name: "Third Test Name" },
{ description: "seventh desc", name: "Third Test Name" },
{ description: "eighth desc", name: "Third Test Name" },
{ description: "ninth desc", name: "Third Test Name" },
{ description: "tenth desc", name: "Third Test Name" },
{ description: "eleventh desc", name: "Fourth Test Name" },
{ description: "twelfth desc", name: "Fourth Test Name" }
]
}
];
this.$onInit = function() {
const finalLoopData = {};
$scope.loopData[0].values.forEach(function(item) {
// initialize name group
if (!finalLoopData[item.name]) {
finalLoopData[item.name] = {
name: item.name,
descriptions: []
};
}
// add description to name group
finalLoopData[item.name].descriptions.push(item.description);
});
// apply results
$scope.finalLoopData = finalLoopData;
};
});