gpt4 book ai didi

javascript - 使用 ng-repeat angularjs 在表中应用 rowspan

转载 作者:行者123 更新时间:2023-11-30 19:44:04 27 4
gpt4 key购买 nike

我有这样的数组

$scope.orderno=[
{"orderno":1254,"weight":25875,"group":5},
{"orderno":56787,"weight":25875,"group":5},
{"orderno":567,"weight":25875,"group":3},
{"orderno":123254,"weight":25875,"group":3}
];

现在我想在 html 中显示如下
expected output
我试过了,但我不能。我在下面附上了我试过的代码。

<div ng-app>
<table ng-controller="MainCtrl">
<thead>
<div>
<tr>
<td>orderno</td>
<td>weight</td>
<td rowspan={{orderwt}}>group</td>
</tr>
</div>
</thead>
<tbody ng-repeat="item in orderno">
<tr>
<td></td>
<td></td>
<td rowspan="{{orderno.length}}">{{item.group}}</td>
</tr>
<tr>
<td>{{item.orderno}}</td>
<td>{{item.weight}}</td>
</tr>
</tbody>
</table>
</div>

我试过了,但我找不到正确的答案

最佳答案

您应该做的第一件事是将数据转换为更易于迭代的格式。例如,您可以使用 array.reduce() 来帮助您创建一个以组号为键的新对象。然后您可以迭代此对象以创建您的表。

请参阅下面带有评论的示例片段:

// This is your original data array
let arr = [{
"orderno": 1254,
"weight": 25875,
"group": 5
},
{
"orderno": 56787,
"weight": 25875,
"group": 5
},
{
"orderno": 567,
"weight": 25875,
"group": 3
},
{
"orderno": 123254,
"weight": 25875,
"group": 3
}
], // Use reduce and Object.create to make a new object keyed by group number
result = arr.reduce(function(r, a) {
r[a.group] = r[a.group] || [];
r[a.group].push(a);
return r;
}, Object.create(null));

let app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$scope.groups = result;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<table border="1">
<thead>
<td>Group</td>
<td>Order No</td>
<td>Weight</td>
</thead>
<tbody ng-repeat="(key, value) in groups"> <!-- Outer loop -->
<tr ng-repeat="group in value"> <!-- Inner loop -->
<td ng-if="$index == 0" rowspan="{{ value.length }}">{{ group.group }}</td>
<!-- the above is so we only add the rowspan once -->
<td>{{ group.orderno }}</td>
<td>{{ group.weight }}</td>
</tr>
</tbody>
</table>

</div>

关于javascript - 使用 ng-repeat angularjs 在表中应用 rowspan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55098672/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com