【问题标题】:creating table with two columns using ng-repeat使用 ng-repeat 创建具有两列的表
【发布时间】:2016-08-31 11:05:16
【问题描述】:

我有一个对象数组,需要以两列布局显示 目前有些列的数据比其他列多,所以我需要等高的行。

我使用 bootstrap 来显示数据,但它没有等高的行。我也用过clearfix,但是没用。

<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 表格中显示单个对象数组中的数据。

【问题讨论】:

  • 如何使用 flex 来获得相等的行数。

标签: angularjs html twitter-bootstrap css


【解决方案1】:

听起来您可能想使用一张桌子。表格将自动使行具有相同的高度。

我不知道您的意思是不是希望每一行都具有相同的高度,但是使用表格至少会使同一行的列具有相同的高度。

这也使用引导类。把它放在一个 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>

【讨论】:

    【解决方案2】:

    如果您将问题视为具有单列的 2 个表而不是具有 2 列的 1 个表,这很简单——使用 $odd 和 $even 属性。

    <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>
    

    【讨论】:

      【解决方案3】:

      看看这个提琴手here。

      控制器

      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);
      }
      

      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>
      

      基本上我已经创建了一个索引数组(长度为实际数组的一半),然后使用它创建了一个包含 2 行的表并从您的对象中提取项目。

      编辑 支持过滤器

      HTML

      <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>
      

      控制器

      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);  
      }
      

      查看更新的过滤器here。

      它支持基本过滤器。

      【讨论】:

      • 我还需要角度滤镜支持
      • 检查我的编辑。我稍微修改了算法并添加了对基本过滤器的支持。使用相同的主体,您也可以添加自定义过滤器。
      【解决方案4】:

      使用 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>
      

      如果您需要all the row heights be equal,那么您需要使用一些 CSS 来完成。

      如果您需要它们都具有相同的高度,基于其中的内容,那么您将需要使用 JavaScript。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-12
        • 1970-01-01
        • 2014-09-14
        • 2015-12-23
        • 2014-11-03
        • 1970-01-01
        • 1970-01-01
        • 2015-02-23
        相关资源
        最近更新 更多