【发布时间】:2017-03-22 18:13:41
【问题描述】:
我一直遇到这个问题,但没有找到一个不会导致布局问题的好解决方案。在列中显示按字母顺序排序的项目数组的最佳方式是什么?我正在使用 ng-repeat 遍历数组并为每个项目显示一个复选框。我希望数据按字母顺序显示在 n 列中,即不按字母顺序排列。
按字母顺序排列
|item a| |item d| |item g|
|item b| |item e| ...
|item c| |item f| ...
当前实现 - 按字母顺序排列
<div class="checkbox col-xs-12 col-sm-12 col-md-3 col-lg-3" ng-repeat="user in user.results | orderBy:'lastName' track by user.id">
<input id="{{ user.id }}" type="checkbox">
{{ user.lastName }}, {{ user.firstName }}
<label for="{{ user.id }}" class="pull-left checkbox-label"></label>
</div>
编辑
我最初使用动态引导方法,但这实际上搞砸了复选框行为,即单击复选框导致选中了不正确的复选框。我正在尝试使用 flexbox 解决此问题,但我之前没有使用过它,并且不明白如何动态更改列数而无需在 flex 容器上设置固定高度。我想在小/超小屏幕上设置一列,在中/大屏幕上设置三列。
.flex-box {
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.flex-item {
background: green;
width: 33%;
}
/* Small screens */
@media all and (max-width: @container-tablet) {
.flex-item {
width: 100%;
}
}
<div class="flex-box">
<div class="flex-item" ng-repeat="country in region.countries | orderBy:'name' track by $index">
<input id="{{ country.isoCode }}" checklist-model="vm.selectedCountries.value[region.name]" checklist-value="country" type="checkbox" ng-change="vm.setRegionCountry(region, country, checked)">
<label for="{{ country.isoCode }}" class="pull-left checkbox-label"></label>
<span>{{ country.name | limitTo:17 }}{{country.name.length > 17 ? '...' : ''}}</span>
</div>
</div>
解决方案
.flex-box {
display: flex;
flex-flow: column wrap;
}
/* Small screens */
@media all and (min-width: @screen-sm-min) {
.flex-box {
max-height: 375px;
}
}
/* Medium screens */
@media all and (min-width: @screen-md-min) {
.flex-box {
max-height: 550px;
}
}
/* Large screens */
@media all and (min-width: @screen-lg-min) {
.flex-box {
max-height: 375px;
}
}
【问题讨论】:
-
你考虑过使用 flexbox 吗?
-
你想要 n 列还是 n 行?
|item a| |item d| |item g|在列中看起来不是按字母顺序排列的。它们在行中按字母顺序排列。请同时确认。 -
我试图在列中按字母顺序实现,即从上到下阅读,然后到下一列,所以,项目 a,项目 b,项目 c,-> 下一列项目 d,项目 e , 项目 f...
-
@bob,flexbox 看起来很有可能,将进一步研究。
-
我的答案中包含了一些示例代码。查看 css-tricks 链接了解更多信息
标签: angularjs twitter-bootstrap flexbox angularjs-orderby