【发布时间】:2018-05-16 07:07:55
【问题描述】:
我有一个使用 AngularJS 制作的带有列过滤器的数据表。 这是 HTML:
<body ng-app="myApp" ng-controller="appController as Ctrl">
<table class="table table-bordered table-striped table-hover dataTable js-exportable" datatable="ng" dt-options="Ctrl.dtOptions" dt-columns="Ctrl.dtColumns">
<thead>
<tr>
<th></th>
<th>Name</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>Name</th>
</tr>
</tfoot>
<tbody>
<tr ng-repeat="user in userList">
<td>
<input type="checkbox" id="user-{{ $index }}" ng-model="Ctrl.checkboxValue[$index]" ng-click="Ctrl.checkValue(user.id)" ng-true-value="{{user.id}}" />
<label for="user-{{ $index }}"></label>
</td>
<td>
<a href="#">
{{ ::user.name }}
</a>
</td>
</tr>
</tbody>
</table>
这是脚本:
angular.module('myApp', ['ngAnimate', 'ngSanitize', 'datatables', 'datatables.columnfilter'])
.controller('appController', function($scope, $compile, DTOptionsBuilder, DTColumnBuilder){
$scope.userList = [
{
id: '1',
name: 'hello'
},
{
id: '2',
name: 'hi'
}
];
var vm = this;
vm.dtOptions = DTOptionsBuilder.newOptions()
.withPaginationType('full_numbers')
.withOption('createdRow', function (row, data, dataIndex) {
$compile(angular.element(row).contents())($scope);
})
.withColumnFilter({
aoColumns: [{
}, {
type: 'text',
bRegex: true,
bSmart: true
}]
});
vm.dtColumns = [
DTColumnBuilder.newColumn('').withTitle(''),
DTColumnBuilder.newColumn('name').withTitle('Name'),
];
vm.checkboxValue = [];
vm.checkValue = function(id){
console.log(id);
}
});
问题:
用户的 ID 没有传递给
checkValue函数。因此,console.log 是undefined。假设如果第一个用户的复选框被选中,
checkboxValue数组的值为[undefined: '1']。如果选中第二个用户的复选框,则checkboxValue数组的值变为[undefined: '2']。 只有一个复选框被选中。这是为什么呢?
【问题讨论】:
-
第一个问题是正常的,应该使用
ng-change。它的input字段,ng-click不会工作。你能创建一个第二期的 plunkr 并分享演示网址 -
plnkr.co/edit/A3PJfBuwtpUQFAIz8hW7?p=preview 。抱歉,拖了这么久。
标签: angularjs datatables angular-datatables