【问题标题】:How to speed up rendering table of checkboxes in Angular.js如何加快 Angular.js 中复选框的渲染表
【发布时间】:2014-07-31 13:47:46
【问题描述】:

我正在制作一个系列图表工具。对于每个系列,我有 4 个类别。我希望用户能够选择要绘制的系列和类别的任意组合。

为了实现这一点,我使用 Angular.js 创建了一个引导模式,其中包含一个复选框表,用于每个系列/类别组合。它可以工作,但是当系列太多时,我发现模态的渲染太慢了:

  <table class="table table-striped table-bordered table-hover">
    <thead>
      <tr>
        <th>Series Name</th>
        <th ng-repeat="col in cols">
          {{ col }}
        </th>
        <th>select all</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="(name, insts) in items"
          ng-show="([name] | filter:query).length > 0">
        <td>{{ name }}</td>
        <td ng-repeat="inst in insts">
          <input type="checkbox" ng-model="inst.isSelected">
        </td>
        <td><ui-select-all items="insts" prop="isSelected"></ui-select-all></td>
      </tr>
  </table>

我所拥有的 Plunker 演示:http://plnkr.co/edit/1zmZMpDsGwaKdyL7dFty?p=preview

我只是在学习 Angular,所以我不太确定如何加快速度,或者是否有比使用一大堆复选框更智能的方法。

【问题讨论】:

    标签: javascript angularjs twitter-bootstrap


    【解决方案1】:

    加速任何 ng-repeat 的最佳方法是使用 track by $index

      <tr ng-repeat="(name, insts) in items track by $index"
          ng-show="([name] | filter:query).length > 0">
        <td>{{ name }}</td>
        <td ng-repeat="inst in insts track by $index">
          <input type="checkbox" ng-model="inst.isSelected">
        </td>
        <td><ui-select-all items="insts" prop="isSelected"></ui-select-all></td>
      </tr>
    

    【讨论】:

    • 我在 plunker 中尝试过,但没有明显的加速
    • 您也可以将 {{}} 替换为 ng-bind,或查看 bindonce
    • 我使用 bindonce 更新了 plunker,但模态仍然需要几秒钟才能显示出来。作为比较,如果您将 num rows 设置为一个较小的数字,它几乎会立即加载。
    • @HarishR 你能详细说明“track by $index”吗?但无论如何它似乎没用,因为它确实根本不会改变 plunker 样本中的速度
    • 请阅读this article
    猜你喜欢
    • 2019-04-12
    • 2015-10-28
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2022-01-05
    • 2017-10-16
    相关资源
    最近更新 更多