我有以下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
问题与您的NG重复有关。您正在使用错误的变量语法更改此
<tr ng-repeat="d in $data" >
to
<tr ng-repeat="d in data" >
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});
});
和voila
编辑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>
希望此帮助
cols:
$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>
尝试这个。