当我将AngularJS(1.6.x)与jQuery数据表结合使用时,我收到警告错误:
无法重新初始化DataTable
首先我使用AngularJS来绑定和填充数据表, 然后我尝试添加Individual column searching (text inputs) feature。
Individual column searching (select inputs)
AngularJS初始化数据表,但没有给我一个句柄。
这是我的代码:
var app1=angular.module('formvalid', ['ui.bootstrap','ui.utils']);
app1.controller('validationCtrl',function($scope){
angular.element(document).ready(function () {
// Setup - add a text input to each footer cell
$('#example tfoot th').each( function () {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
console.log(' document ready function, add search by column feature ');
var table = $('#example').DataTable();
// Apply the search
table.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
});// document ready
$scope.data=[[
"Tiger Nixon",
"System Architect",
"Edinburgh",
"5421",
"2011\/04\/25",
"$320,800"
]];
$scope.dataTableOpt = {
//custom datatable options
// or load data through ajax call also
// "data": $scope.data00, // this is not real binding, the real binding is ui-jq="dataTable" ui-options="dataTableOpt", fill $scope.data
"aLengthMenu": [[10, 50, 100,-1], [10, 50, 100,'All']],
};
});
答案 0 :(得分:0)
demo on codepen.io
demo on jsFiddle
早期的angularjs初始化datatable(必须添加“retrieve”:true,否则,将得到错误retrieve existing table handle),但是没有得到表句柄,
稍后,$('#id')。DataTable();将1)如果存在,将检索表句柄。 2)如果不存在,将创建一个新表。
所以解决方案是
$scope.dataTableOpt = {
//custom datatable options
// or load data through ajax call also
// "data": $scope.data00, // this is not real binding, the real binding is ui-jq="dataTable" ui-options="dataTableOpt", fill $scope.data
"retrieve": true, // angularjs at begining initialize datatable, but don't get a handle to the table, later you want to add search column, you need to get the table handle.
"aLengthMenu": [[10, 50, 100,-1], [10, 50, 100,'All']],
};
答案 1 :(得分:0)
codepen : ui-grid (angularjs 1.x) demo
jsFiddle : ui-grid (angularjs 1.x) demo
var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid', 'ui.grid.exporter', 'ui.grid.selection']);
app.controller('MainCtrl', ['$scope', '$http', '$interval', '$q','uiGridConstants', function ($scope, $http, $interval, $q, uiGridConstants) {
我构建这些演示,是angularjs 1.x数据表的最佳解决方案。
angularjs 1.x + jquery(datatables)不是最佳解决方案。
ui-grid是纯angularjs 1.x,是目前为止的最佳解决方案。