动态添加列到ngTable

时间:2016-06-27 11:18:27

标签: angularjs ngtable

我有以下ngTable,我想动态添加列:

    <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包含数据本身,但我将列定义在不同的数组中:

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

和$ data:

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

当我运行代码时,表是空的,没有任何列。我试着在网上查看这是否可行,但无法找到一个例子。有什么想法吗?

添加了PLUNK

3 个答案:

答案 0 :(得分:0)

问题出在你的ng-repeat上。您正在使用错误的变量语法 改变这个

myRealm.beginTransaction();

// Create an object
Country country1 = myRealm.createObject(Country.class); 
country1.setName("Norway");
country1.setPopulation(5165800);
country1.setCode("NO");
myRealm.commitTransaction();

RealmResults<Country> results1 =
    myRealm.where(Country.class).findAll();
for(Country c:results1) {
Log.d("results1", c.getName());
}

答案 1 :(得分:0)

Lel ...我检查PLUNK,我发现你没有在SCOPE中声明cols!以及如何从html中获取它?! xD检查改变了这个:

    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});

});

编辑V2:

检查这个html,我添加了一个ng-repeat的列名和另一个的值。

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

希望这个帮助

答案 2 :(得分:-1)

列数:

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

数据:

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

模板:

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

试试这个。