gpt4 book ai didi

javascript - 如果 tr 没有子项,则删除它 - ng-repeat - Angular

转载 作者:行者123 更新时间:2023-12-03 11:46:15 25 4
gpt4 key购买 nike

我正在尝试使用 ng-repeat 在表中构建 tr,并且我正在基于某些逻辑使用行作为值来构建它。这引入了空 tr,我如何删除它?

此代码在 tbody 末尾添加 2 个空 tr。

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

$scope.data = [
{
"name" : "John",
"row" : 1
},
{
"name" : "Mike",
"row" : 2
},
{
"name" : "Bill",
"row" : 1
},
{
"name" : "Alice",
"row" : 3
},
{
"name" : "David",
"row" : 2
}
]
});



app.directive('specrow', ['$compile', function($compile) {
return {
restrict: 'A',
scope : {
'specobj' : '=',
'specitemindex' : '='
},
link: function (scope, elem, attrs){
var template = "<td><span>{{specobj.name}}</span></td>";
var linkFn = $compile(template);
var content = linkFn(scope);
var trEl = angular.element(elem[0].parentElement.children).eq(scope.specobj.row-1);
if(trEl.length === 0) {
elem.append(content);
}else{
angular.element(elem[0].parentElement.children).eq(scope.specobj.row-1).append(content);
}
}
}
}]);

Plnkr:http://plnkr.co/edit/I3kl9U41n3VzQvqFfG4G?p=preview

最佳答案

所以有答案:)

var app = angular.module('app', ['angular.filter']);

app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.data = [
{
"name" : "John",
"row" : 1
},
{
"name" : "Mike",
"row" : 2
},
{
"name" : "Bill",
"row" : 1
},
{
"name" : "Alice",
"row" : 3
},
{
"name" : "David",
"row" : 2
}
]
});
table, th, td {
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.1.1/angular-filter.min.js"></script>

<body ng-app="app">
<table ng-controller="MainCtrl">
<tbody>
<tr ng-repeat="row in data |groupBy: 'row' | orderBy : 'row'">
<td ng-repeat="person in row">{{person.row}}.{{person.name}}</td>

</tr>
</tbody>
</table>
</body>

关于javascript - 如果 tr 没有子项,则删除它 - ng-repeat - Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26035341/

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