Angularjs 使用ng repeat创建包含两列的表
我有一个需要以两列布局显示的对象数组 当前某些列的数据比其他列多,因此我需要等高的行 我使用bootstrap来显示数据,但它没有等高的行。我也使用了clearfix,但不起作用Angularjs 使用ng repeat创建包含两列的表,angularjs,html,twitter-bootstrap,css,Angularjs,Html,Twitter Bootstrap,Css,我有一个需要以两列布局显示的对象数组 当前某些列的数据比其他列多,因此我需要等高的行 我使用bootstrap来显示数据,但它没有等高的行。我也使用了clearfix,但不起作用 <ng-include src="itemTemplateSrc" ng-repeat-start="tag in item.fields | filter:filterTagsByTagName" class="col-md-12"></ng-include> <div class="c
<ng-include src="itemTemplateSrc" ng-repeat-start="tag in item.fields | filter:filterTagsByTagName" class="col-md-12"></ng-include>
<div class="clearfix" ng-if="$index%2===1"></div>
<div ng-repeat-end=""></div>
现在,我想在html中显示数据,使用两列布局,而不拆分数组,因为我还有过滤器
如何在HTML表格中显示来自单个对象数组的数据。使用ng repeat在表格中显示数据非常简单,只需重复一组行即可:
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<!-- Now just ng-repeat the rows -->
<tr ng-repeat="user in $ctrl.users">
<td>{{user.name}}</td>
<td>{{user.age}}</td>
</tr>
</tbody>
</table>
名称
年龄
{{user.name}
{{user.age}
如果您需要,那么您将需要使用一些CSS来实现这一点
如果您需要它们都具有相同的高度(基于其中的内容),那么您需要使用JavaScript。看看这个小提琴手 控制器
function MyCtrl($scope) {
$scope.name = 'Superhero';
$scope.obj = ["data-1", "data-2", "data-3", "data-4"];
$scope.arr = Array.apply(null, {
length: $scope.obj.length/2
}).map(Number.call, Number);
}
function MyCtrl($scope) {
$scope.name = 'Superhero';
$scope.obj = ["data-1", "data-2", "data-3", "data-4"];
$scope.arrLength = $scope.obj.length / 2;
$scope.arr = Array.apply(null, {
length: $scope.obj.length/2
}).map(Number.call, Number);
}
HTML
<div ng-controller="MyCtrl">
<table>
<tr ng-repeat="index in arr">
<td>{{obj[index*2]}}</td>
<td> {{obj[index*2+1]}}</td>
</tr>
</table>
</div>
<div ng-controller="MyCtrl">
<input type="text" ng-model="filterText"/>
<table>
<tr ng-repeat="item in items = (obj | filter: filterText)" ng-show="$index < arrLength">
<td>{{items[$index*2]}}</td>
<td> {{items[$index*2+1]}}</td>
</tr>
</table>
</div>
请参阅更新的过滤器
它支持基本的过滤器。如果您认为问题是两个表有一列,而不是一个表有两列,那么这是非常简单的-使用$odd和$偶数属性
<div>
<div class="col-md-6">
<table class="table table-striped">
<tbody>
<tr ng-repeat="item in items">
<td ng-if="$even">...</td>
</tr>
</tbody>
</table>
</div>
<div class="col-md-6">
<table class="table table-striped">
<tbody>
<tr ng-repeat="item in items">
<td ng-if="$odd">...</td>
</tr>
</tbody>
</table>
</div>
</div>
...
...
听起来您可能想使用表格。表格将自动使行具有相同的高度
我不知道你是不是想让每一行都具有相同的高度,但使用表至少会使同一行上的列具有相同的高度
这也使用引导类。将此粘贴到div.row>div.col-###-xx中,希望它能成为您想要的样式
最后,我在第二列中添加了一个ng if=“vm.array[$index+1]”。如果数组中的对象数为奇数,这将隐藏表中的最后一个框
<div class="table-responsive">
<table class="table table-striped">
<tr ng-repeat="whatever in vm.array" ng-if="!($index % 2)">
<td>
{{vm.array[$index]}}
</td>
<td ng-if="vm.array[$index + 1]">
{{vm.array[$index + 1]}}
</td>
</tr>
</table>
</div>
{{vm.array[$index]}
{{vm.array[$index+1]}
如何使用flex获得相等的行数。我还需要对角度过滤器的支持检查我的编辑。我对算法做了一些修改,并添加了对基本过滤器的支持。使用相同的主体,还可以添加自定义过滤器。