gpt4 book ai didi

html - 使用 ng-repeat 包装列

转载 作者:行者123 更新时间:2023-11-28 06:05:05 27 4
gpt4 key购买 nike

我正在使用 ng-repeat 在我的 Controller 中打印一个数组。我的问题是我希望我的重复在第 n:th 项之后从顶部开始列出,如下所示:

 Item 1    Item 6    Item 11
Item 2 Item 7 Item 12
Item 3 Item 8 Item 13
Item 4 Item 9 Item 14
Item 5 Item 10 Item 15

我的代码:

HTML

<div class="participant-list" >
<div class="participant" ng-repeat="speaker in participants | orderBy:'name'" ng-click="addSpeaker(speaker.id)">{{speaker.name}}, {{speaker.city}}</div>
</div>

CSS

.participant-list {
height: 800px;
}

.participant {
margin: 5px 0px;
padding: 2px 0px;
cursor: pointer;
}

有什么好的方法吗?

最佳答案

这不是最优雅的解决方案,但它确实有效。如果元素数不是三的倍数(或您想要的任何列数),则它不起作用。要解决这个问题,您可以将空白项添加到数组中。您还必须在 Controller 中手动对其进行排序。

index.html

<!DOCTYPE html>
<html ng-app="app">
<head>
<title>NG Repeat Column Wrapping</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<script src="./ctrl.js"></script>
</head>
<body ng-controller="ctrl">
<div class="container">
<div class="row" ng-repeat="x in rowsCount">
<div class="col-sm-4">
<span>{{ names[$index] }}</span>
</div>
<div class="col-sm-4">
<span>{{ names[$index + 5] }}</span>
</div>
<div class="col-sm-4">
<span>{{ names[$index + 10] }}</span>
</div>
</div>
</div>
</body>
</html>

控制键

var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
$scope.names = ["Bob", "Susan", "Mark", "John", "Billy", "Ted", "Marcy", "Wilma", "Jeff", "Yasmin", "Pete", "Taylor", "Matthew", "Laney", "Jesus"];
$scope.rowsCount = [];
$scope.createRows = function() {
for (var i = 0; i < ($scope.names.length / 3); i++) {
$scope.rowsCount.push(i);
}
};
$scope.createRows();
});

关于html - 使用 ng-repeat 包装列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36508404/

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