-2

I have the following ngTable, and I want to add columns dynamically:

    <table ng-table="tableParams" show-filter="showFilter" class="table table-bordered">
        <tbody>
            <tr ng-repeat="d in $data" >
                <td ng-repeat="col in cols" title="'col.nm'" 
                      filter="{ col.nm: 'text' }">{{ col.nm }}</td>
            </tr>
        </tbody>
    </table>

$data contains the data itself, but I have the columns definition in a different array:

$scope.cols = [ {nm:'col0'}, {nm:'col1'}, {nm:'col2'} ];

and $data:

$scope.data = [ {col0: "0", col1: "1", col2: "2"} ] ...

When I run the code the table is empty without any columns. I tried to look online to see if this is possible but couldn't find an example. Any ideas?

Added PLUNK

ps0604
  • 1,227
  • 23
  • 133
  • 330

3 Answers3

0

Problem is with your ng-repeat. You are using Wrong variable syntax Change this

 <tr ng-repeat="d in $data" >

to

 <tr ng-repeat="d in data" >
Keyur Shah
  • 536
  • 6
  • 20
  • 1
    'When I run the code the table is empty without any columns' By changing $data to data will show table. and u need to ng-repeat again in td for dynamic columns. – Keyur Shah Jun 27 '16 at 11:40
0

Lel... i check PLUNK and i see you no declare cols in SCOPE! and how u want take it from html?! xD check change this:

    var app = angular.module('app', ['ngTable']);

app.controller('myCtl', function($scope,NgTableParams) {
          $scope.cols = [ {nm:'col0'}, {nm:'col1'}, {nm:'col2'} ];

          $scope.data = [ 
            { col0: 'User 1', col1: 'Name 1', col2: 'Group 1'},
            { col0: 'User 2', col1: 'Name 2', col2: 'Group 2'}
          ];

          $scope.tableParams = new NgTableParams({dataset: $scope.data});

});

And voila


EDIT V2:

Check this html, i added columns name with one ng-repeat and value in another.

 <table ng-table="tableParams" class="table table-bordered table-hover">
    <tbody>
        <td ng-repeat="col in cols">{{ col.nm }}</td>
        <tr ng-repeat="u in data">
            <td ng-repeat="(key, value) in u">
              {{value}}
            </td>
        </tr>
    </tbody>
</table>

Hope this help

Javierif
  • 632
  • 1
  • 6
  • 18
-1

Cols:

$scope.cols = [ {nm:'col0'}, {nm:'col1'}, {nm:'col2'} ];

Data:

$scope.data = [ {col0: "0", col1: "1", col2: "2"} ] ...

Template:

<div ng-controller="myCtl" ng-app="app">
    <table ng-table="tableParams" class="table table-bordered table-hover">
      <tbody>
        <tr><td ng-repeat="name in names">{{ name.nm }}</td></tr>
        <tr ng-repeat="u in data">
          <td ng-repeat="value in u">
            {{value}}
          </td>
        </tr>
      </tbody>
    </table>
</div>

Try this.

RaV
  • 1,029
  • 1
  • 9
  • 25