【问题标题】:Twitter Bootstrap Columns and Angular Order ByTwitter Bootstrap 列和角度排序依据
【发布时间】: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


【解决方案1】:
<style>
.flex-box {
        height: 100px;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        flex-wrap: wrap;
    }
    .flex-item {
            background: green;
            text-align: center;
    }
</style>
<div class="container">
    <div class="row">
        <div class="col-xs-12">
            <div class="flex-box">
                <div class="flex-item" ng-repeat="user in user.results | orderBy:'lastName'">{{user.id}}</div>
            </div>
        </div>
    </div>
</div>

使用 CSS flexbox,您可以为数据集动态添加任意数量的列https://css-tricks.com/snippets/css/a-guide-to-flexbox/

【讨论】:

    【解决方案2】:

    如果您想与bootstrap 一起使用,您可以进行如下微调。否则,您可以使用flex-boxcolumn-count

    var app = angular.module('app', []);
    app.controller('TestController', function($scope){
      $scope.fixedColumn = 3;
      
      $scope.getColumns = function(){
        return new Array($scope.fixedColumn);
      };
      
      $scope.getColumnWidth = function(){
        return Math.floor(12 / $scope.fixedColumn);
      };
      
      $scope.getRowCount = function(){
        return Math.ceil($scope.user.results.length / $scope.fixedColumn);
      };
      
      $scope.user = {
        results: [
          {
            id: 1,
            firstName: 'FirstName1',
            lastName: 'LastName1'
          },
          {
            id: 2,
            firstName: 'FirstName2',
            lastName: 'LastName2'
          },
          {
            id: 3,
            firstName: 'FirstName3',
            lastName: 'LastName3'
          },
          {
            id: 4,
            firstName: 'FirstName4',
            lastName: 'LastName4'
          },
          {
            id: 5,
            firstName: 'FirstName5',
            lastName: 'LastName5'
          },
          {
            id: 6,
            firstName: 'FirstName6',
            lastName: 'LastName6'
          },
          {
            id: 7,
            firstName: 'FirstName7',
            lastName: 'LastName7'
          },
          {
            id: 8,
            firstName: 'FirstName8',
            lastName: 'LastName8'
          }
        ]
      };
    });
    
    angular.bootstrap(document, ['app']);
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div class="container" ng-controller="TestController">
    <div class="row" ng-repeat="u in user.results | orderBy:'lastName' track by u.id" ng-init="pIndex=$index" ng-if="$index < getRowCount()">
      <div ng-repeat="col in getColumns() track by $index" ng-init="usr = user.results[pIndex + ($index * getRowCount())]" ng-class="'col-xs-'+ getColumnWidth() + ' col-sm-'+ getColumnWidth() + ' col-md-'+ getColumnWidth()" ng-if="user.results.length > (pIndex + ($index * getRowCount()))">
        <input id="{{ usr.id }}" type="checkbox">
        {{ usr.lastName }}, {{ usr.firstName }}
        <label for="{{ usr.id }}" class="pull-left checkbox-label"></label>
      </div> 
     </div>
     </div>

    【讨论】:

    • 不错!感谢您的详细回答。我一直在寻找解决这个问题的方法。我很惊讶没有一个指令来处理这个问题。感谢您的帮助!
    【解决方案3】:

    您可以在不使用列引导的情况下执行此操作。看到这个小提琴:https://jsfiddle.net/ojzdxpt1/1/

    #wrapper {
        column-count:3;
        -webkit-column-count:3;
        -moz-column-count:3;
    }
    .col {
      background:#ccc;
      border:1px solid #000;
    }
    

    #wrapper 包含每个转发器。查看更多关于 css 列的信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多