使用挖空时没有可见行时隐藏表

时间:2015-11-10 15:11:06

标签: knockout.js html-table hide rows

我有一个表,其行可见性由下拉列表中的项类型选择处理。以下是我的代码(我使用了knockout.js)。

<table class="table table-bordered table-condensed">
  <tbody data-bind="foreach: $root.elements">
     <tr data-bind="visible:$root.selectedType==elementType">
       <td>
         <span data-bind="text:elementValue"></span>
      </td>
     <tr>
  </tbody>    
</table>

此处下拉列表中的$root.selectedType值已更改。有时会出现所选类型没有元素的情况,在这种情况下我有一个空表(只显示标题)。我的要求是在没有所选类型的元素(行)时隐藏表。

1 个答案:

答案 0 :(得分:0)

您可以使用visible绑定:

<table class="table table-bordered table-condensed" 
       data-bind="visible: $root.elements().length > 0">
  <tbody data-bind="foreach: $root.elements">
     <tr data-bind="visible:$root.selectedType==elementType">
       <td>
         <span data-bind="text:elementValue"></span>
      </td>
     <tr>
  </tbody>    
</table>

如果$root.elements长度为非零

,则会显示该表格
相关问题