0

I want to have a table consisting of 3 columns, the code looks like this

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

That works great except for a small issue.

enter image description here

There's simply not enough json data to fill 3 rows, so 2 checkboxes are displayed empty

What I tried, is writing something like

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

And then something like

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

It fixes the problem but not on all tables, some tables would still have empty checkboxes.

I get the permission titles like this

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

I tried removing the return, didn't fix it.

8
  • I would appreciate a fix without extra dependencies, like ng-tables. Commented Jul 26, 2016 at 13:14
  • would you please make live example ? Commented Jul 26, 2016 at 13:15
  • and check by replacing SPAN with DIV ? Commented Jul 26, 2016 at 13:16
  • @IsmailFarooq ok thanks, I'll try that first, if didn't work then I'll try making a live example, the problem with that is I have to get json from somewhere, if I were to run in online Commented Jul 26, 2016 at 13:18
  • give me the live url of your app if you can ? Commented Jul 26, 2016 at 13:19

2 Answers 2

1

change:

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

for:

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

if I understood well your code and your problem, vm.parent.getPermission(category,$parent.$index+i) returns the text (permission) so just check that is not empty... it may also work like:

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

because vm.parent.getPermission(category,$parent.$index+i) returning nothing could evaluate false.

I haven't try it.

Sign up to request clarification or add additional context in comments.

4 Comments

it did work but now everywhere, if there are only two rows to be displayed, it will display a third empty checkbox, but otherwise it's working, it's just there's a small bug remaining
then the bug is not in the code, but in the json, there must be a vm.parent.getPermissionTitle(category,$parent.$index+i) returning a space or invisible characters...
actually your fix will make the data get repeated
Doing this: <span ng-if="vm.parent.getPermissionTitle(category,$parent.$index+i)!=''"> cause the data to repeat? it should have something to do with the $index variable, but with the code you provided is hard to tell.
0

The solution is

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

So basically {{vm.parent.getPermissionTitle(category,$parent.$parent.$index+i)}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.