Razor
Razor

Reputation: 23

Ng-table, hide-columns feature, can't hide columns from controller manually

I have set up ng-table with checkbox buttons to hide columns and it works great. here is an html

// switchers to hide/show columns
<div style="margin-bottom: 20px">
    <label class="checkbox-inline" ng-repeat="col in demo.cols">
      <input type="checkbox" ng-model-options="{ getterSetter: true }" ng-model="col.show"/> {{col.title()}}
    </label>
  </div>
// ng table self
  <table ng-table="demo.tableParams" class="table table-condensed table-bordered table-striped" ng-table-columns-binding="demo.cols">
    <tr ng-repeat="row in $data">
      <td title="'Name'" ng-if="true">{{row.name}}</td>
      <td title="'Age'" ng-if="true">{{row.age}}</td>
      <td title="'Money'" ng-if="true">{{row.money}}</td>
    </tr>
  </table>

and here is controller code

    // NG-TABLE PARAMS
  self.cols = [
    { field: "name", title: "Name", show: true },
    { field: "age", title: "Age", show: true },
    { field: "money", title: "Money", show: true}
  ];

  var simpleList = [{"id":1,"name":"Nissim","age":41,"money":454},{"id":2,"name":"Mariko","age":10,"money":-100},{"id":3,"name":"Mark","age":39,"money":291},{"id":4,"name":"Allen","age":85,"money":871},{"id":5,"name":"Dustin","age":10,"money":378},{"id":6,"name":"Macon","age":9,"money":128},{"id":7,"name":"Ezra","age":78,"money":11},{"id":8,"name":"Fiona","age":87,"money":285},{"id":9,"name":"Ira","age":7,"money":816},{"id":10,"name":"Barbara","age":46,"money":44},{"id":11,"name":"Lydia","age":56,"money":494},{"id":12,"name":"Carlos","age":80,"money":193}];

  self.tableParams = new NgTableParams({}, {
    filterDelay: 0,
    dataset: simpleList,
    counts: []
  });

Image

Problem

I wanted to create my own button to hide necessarry columns at once, instead of clicking to each column. To do that i created a button with function. Here the button code

<button  class="btn btn-default btn-sm" ng:click="demo.hideColumns()">Hide columns</button>

and here is the function inside controller

self.hideColumns = function(){
    self.cols[1].show = false;
    self.cols[2].show = false;
}

the code above works, the checkboxes change to unchecked, but the table does not hide columns, I guess the problem somehow related to angular's getter/setter features, but I couldn't solve it.

Dear SO community please help me to solve this problem, Thank you

Links

Codepen page, where I copied the code

Upvotes: 2

Views: 3194

Answers (1)

miensol
miensol

Reputation: 41598

The col.show is a getter/setter function. To toggle the value change the hideColumns function to i.e.:

self.hideColumns = function(){
    self.cols[1].show(false);
    self.cols[2].show(false);
}

A working plunker

Upvotes: 2

Related Questions