用ng-repeat填充表并检查匹配的标头数据 [英] Populate table with ng-repeat and check matching header data
问题描述
我有年份数组,可用于填充表标题行,还有一个对象,可用于填充表.我需要在相应的Year标题下填充正确的Year数据,所以我需要检查对象中的year(Y)是否对应于header数组中的Year,否则,我需要添加一个空细胞.该对象按年份排序.最好的方法是什么?这是小提琴
I have array of years which I use to populate table header row and I have an object that I use to populate the table. I need to populate the correct year data under the corresponding year header, so I'd need to check if the year(Y) in the object corresponds to the year in the header array, if not then I'd need to add an empty cell. The object is sorted by year. What would be the best way to do that? Here is the fiddle
控制器
var app = angular.module("testModule", []);
app.controller('testController', function($scope) {
$scope.headerYears = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020];
$scope.rows = [{
"Name": "Name1",
"Col": [{
"Y": 2013,
"M": 25711
}, {
"Y": 2014,
"M": 26095
}, {
"Y": 2015,
"M": 23641
}, {
"Y": 2016,
"M": 22224
}, {
"Y": 2017,
"M": 21968
}, {
"Y": 2018,
"M": 23820
}, {
"Y": 2019,
"M": 26673
}, {
"Y": 2020,
"M": 29329.5
}]
}, {
"Name": "Name2",
"Col": [{
"Y": 2013,
"M": 83
}, {
"Y": 2014,
"M": 461
}, {
"Y": 2015,
"M": 1067
}, {
"Y": 2016,
"M": 1120
}, {
"Y": 2017,
"M": 1050
}, {
"Y": 2018,
"M": 600
}, {
"Y": 2019,
"M": 475
}, {
"Y": 2020,
"M": 481
}]
}, {
"Name": "Name3",
"Col": [{
"Y": 2013,
"M": 25794
}, {
"Y": 2014,
"M": 26556
}, {
"Y": 2015,
"M": 24708
}, {
"Y": 2016,
"M": 23424
}, {
"Y": 2017,
"M": 23297
}, {
"Y": 2018,
"M": 24412.5
}, {
"Y": 2019,
"M": 27090.5
}, {
"Y": 2020,
"M": 29754.5
}]
}]
});
HTML
<table border="1" data-ng-app="testModule" data-ng-controller="testController">
<thead>
<tr>
<th ng-repeat="i in headerYears">{{i}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in rows">
<td ng-repeat="item in row.Col">{{item.M}}{{item.Y}}</td>
</tr>
</tbody>
推荐答案
我会在DOM中执行ng-if
I would do an ng-if in the DOM
<table border="1" data-ng-app="testModule" data-ng-controller="testController">
<thead>
<tr>
<th ng-repeat="i in headerYears">{{i}}</th>
</tr>
</thead>
<tbody>
<tr >
<td ng-repeat="i in headerYears" >{{i}}
<span ng-repeat="row in rows"></span>
<span ng-repeat="item in row.Col">{{item.Y}} </span>
</td>
</tr>
</tbody>
</table>
您可以向跨度中添加NG-IF,如span ng-if ="item.Y == i"和另一个span ng-if ="item.Y!= i"
you can add an NG-IF to the span like span ng-if="item.Y==i" and another span ng-if="item.Y != i"
这篇关于用ng-repeat填充表并检查匹配的标头数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!