【问题标题】:Alternation classes with Knockout.JS带有 Knockout.JS 的交替类
【发布时间】:2012-09-27 19:10:13
【问题描述】:

我有下一张桌子:

  <table class="table" id="calls_table">
        <thead>
        <tr>
            <th class="round-bg header">Val1</th>
            <th class="rep-bg header">Val2</th>
            <th class="rep-bg header">Val3</th>

        </tr>
        </thead>
        <tbody data-bind="template: {name: 'call-template', foreach: calls}">
        </tbody>

    </table> 

    <script type="text/html" id="call-template">
        <tr class="alt">
            <td><a data-bind="attr: {href: url}, text: v1" class="simp" href=""></a></td>
            <td><a data-bind="attr: {href: url}, text: v2" class="simp" href=""></a></td>
            <td data-bind="text: v3"></td>

        </tr>
    </script>

问题是如何为 TR 添加类的交替?喜欢:class='alt'、class=''——请指教。

【问题讨论】:

    标签: knockout.js


    【解决方案1】:

    这个问题在alternate row style with $index binding的SO上得到了回答

    我在jsfiddle 整理了一个工作示例

    【讨论】:

      【解决方案2】:

      您还可以使用 jQuery :odd 或 :even 选择器。

      $(".table tr:even").addClass('alt');
      

      这并没有使用淘汰赛来解决答案,我知道,但有时简单就是好的。

      【讨论】:

      • 使用 KO 构建表时,这在第一次呈现表时有效。但是如果你做了任何导致表格重新渲染的事情(例如,改变任何值),你必须确保你再次调用它。
      • photo_tom,是的,这也是真的。感谢您了解这一点。
      猜你喜欢
      • 2012-06-30
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      • 1970-01-01
      • 2013-05-25
      • 2014-03-15
      • 1970-01-01
      • 2012-03-31
      相关资源
      最近更新 更多