我在angularjs中动态生成表数据和表列。我的桌子是
Name Hobby
XXXX Music
XXXX Cricket
XXXX Books
YYYY Tennis
YYYY Books
YYYY Drawing
但我希望我的表格显示如下:
Name Hobby
XXXX
Music
Cricket
Books
YYYY
Tennis
Books
Drawing
我使用以下代码生成表:
<tr>
<th ng-repeat="th in keys">{{th}}</th>
</tr>
<tr ng-repeat="x in data ">
<td ng-repeat="th in keys">
{{ x[th]}}
</td>
</tr>
我的json看起来像这样
[{"Name:"XXXX", "Hobby":"Music"},
{"Name:"XXXX", "Hobby":"Cricket"},
{"Name:"XXXX", "Hobby":"Books"},
{"Name:"YYYY", "Hobby":"Tennis"},
{"Name:"YYYY", "Hobby":"Books"},
{"Name:"YYYY", "Hobby":"Drawing"}]
我不能使用与此相似的
<ng-repeat="(key, value) in players | groupBy: 'team'">
因为我的表头是动态创建的
我怎么能在angularjs中做到这一点?
答案 0 :(得分:3)
这里是简单的JavaScript解决方案:
var array = [{"Name":"XXXX", "Hobby":"Music"},
{"Name":"XXXX", "Hobby":"Cricket"},
{"Name":"XXXX", "Hobby":"Books"},
{"Name":"YYYY", "Hobby":"Tennis"},
{"Name":"YYYY", "Hobby":"Books"},
{"Name":"YYYY", "Hobby":"Drawing"}];
var distinctNames = []
for (i = 0; i < array.length; i++) {
if(distinctNames.indexOf(array[i].Name) === -1){
distinctNames.push(array[i].Name);
}
else{
array[i].Name = "";
}
}
在此过滤器之后,您可以在表格中呈现数组
答案 1 :(得分:1)
你可以在标记本身做这件事,不需要对数组进行排序或过滤。
<强>标记强>
<table>
<thead>
<tr>
<th ng-repeat="th in keys">{{th}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="x in data">
<td ng-repeat="th in keys">
<span ng-show="th != 'Name' || (th == 'Name' && data[$parent.$index - 1]['Name'] != x['Name'])">
{{ x[th]}}
</span>
</td>
</tr>
</tbody>
</table>
答案 2 :(得分:0)
我会使用SKCurrentPositionListener 您将能够轻松地对表格进行排序,过滤和分组。