2016-07-26 43 views
0

我想有以下3列的表格,代码看起来像这样显示表行,如果数据存在

<table class="table-striped table-bordered table-condensed"> 
    <tbody> 

    <tr ng-if="$index%3==0" ng-repeat="permission in vm.parent.getAllPermissions(category)"> 
    <td ng-repeat="i in [0,1,2]" class="col-xs-2"> 

    <span> 

    <input type="checkbox" checklist-model="vm.data.permissions" checklist-value="vm.parent.getPermission(category,$parent.$index+i)">        {{vm.parent.getPermissionTitle(category,$parent.$index+i) || " "}} 

</span> 
</td> 
</tr> 
</tbody> 
</table> 

这一工程,除了一个小问题大。

enter image description here

有根本没有足够的JSON数据,以填补3行,因此2个复选框显示空

我试过了,正在写一些东西像

vm.hasPermission = function(category, index) { 
    var permissions = vm.getAllPermissions(category); 
    return permissions && index < permissions.length 
}; 

然后像

<span> 
    <input ng-if="vm.parent.hasPermission(category,$parent.$index+i)"type="checkbox" 
checklist-model="vm.data.permissions" checklist- 
value="vm.parent.getPermission(category,$parent.$index+i)">              {{vm.parent.getPermissionTitle(category,$parent.$index+i) || " "}} 
    </span> 

它修复了问题,但不是在所有的表格上,有些表格仍然会有空的复选框。

我得到这样

vm.getPermissionTitle = function(category, index) { 
    var permissions = vm.getAllPermissions(category); 
    if (index < permissions.length) { 
    return i18n.get(permissions[index]); 
    } else { 
    return ''; 
    } 
}; 

我试图消除返回许可标题,并没有解决它。

+0

我将不胜感激修复而无需额外的依赖,就像NG-表。 – Lynob

+0

请你举个实例吗? –

+0

并用DIV替换SPAN来检查? –

回答

0

的解决方案是

<table class="table-striped table-bordered table-condensed"> 
<tbody> 
    <tr ng-if="$index%3==0" ng-repeat="permission in vm.parent.getAllPermissions(category)"> 
     <td ng-repeat="i in [0,1,2]" class="col-xs-2"> 
      <span ng-if="vm.parent.getPermission(category,$parent.$index+i)"> 
       <input type="checkbox" checklist-model="vm.data.permissions" checklist- value="vm.parent.getPermission(category,$parent.$parent.$index+i)"> 
    {{vm.parent.getPermissionTitle(category,$parent.$parent.$index+i)}} 
</span> 
</td> 
</tr> 
</tbody> 
</table> 

所以基本上{{vm.parent.getPermissionTitle(category,$parent.$parent.$index+i)}}

1

变化:

<td ng-repeat="i in [0,1,2]" class="col-xs-2"> 
    <span> 
     <input type="checkbox" checklist-model="vm.data.permissions" checklist-value="vm.parent.getPermission(category,$parent.$index+i)">        {{vm.parent.getPermissionTitle(category,$parent.$index+i) || " "}} 
    </span> 
</td> 

为:

<td ng-repeat="i in [0,1,2]" class="col-xs-2"> 
    <span ng-if="vm.parent.getPermissionTitle(category,$parent.$index+i)!=''"> 
     <input type="checkbox" checklist-model="vm.data.permissions" checklist-value="vm.parent.getPermission(category,$parent.$index+i)">        {{vm.parent.getPermissionTitle(category,$parent.$index+i)}} 
    </span> 
</td> 

如果我深知你的代码和你的问题,vm.parent.getPermission(category,$parent.$index+i)返回文本(许可)所以只是检查是不是空...它也可以这样工作:

<td ng-repeat="i in [0,1,2]" class="col-xs-2"> 
    <span ng-if="vm.parent.getPermissionTitle(category,$parent.$index+i)"> 
     <input type="checkbox" checklist-model="vm.data.permissions" checklist-value="vm.parent.getPermission(category,$parent.$index+i)">        {{vm.parent.getPermissionTitle(category,$parent.$index+i)}} 
    </span> 
</td> 

因为vm.parent.getPermission(category,$parent.$index+i)返回什么都不能评估吃了假。

我还没试试。

+0

它确实工作,但现在无处不在,如果只有两行要显示,它会显示第三个空的复选框,否则它的工作,这只是有一个小的错误剩余的 – Lynob

+0

然后该错误不在代码,但在JSON中,必须有一个'vm.parent.getPermissionTitle(category,$ parent。$ index + i)'返回一个空格或不可见字符...... –

+0

实际上你的修复会使得数据重复 – Lynob

相关问题