Angularjs 的数据表:默认排序和搜索不起作用
Datatables with Angularjs: default sorting and searching not work
我在 ng-repeat 完成后使用指令建立数据库:
app.directive('repeatDone', function() {
return function(scope, element, attrs) {
if (scope.$last) {
scope.$eval(attrs.repeatDone);
}
}
});
function InformationController($scope, $http) {
$http.get("people").success(function(response) {
$scope.people = response;
});
$scope.initDataTable = function() {
$('#employeeTable').DataTable({
});
};
};
<table class="table table-striped table-bordered table-hover" id="employeeTable">
<thead>
<tr>
<th>id</th>
<th>Name</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="result in people" repeat-done="initDataTable()">
<td>{{ result.id }}</td>
<td>{{ result.name }}</td>
<td>{{ result.gender }}</td>
</tr>
</tbody>
</table>
数据显示正常,但无法使用默认排序或搜索。
有什么问题吗?我应该怎么做才能启用排序和搜索?
您不能直接使用 angular 中的数据表功能,因为这与重叠的执行序列和功能受到干扰高度相关。您可以像下面这样具体地引发超时
setTimeout(function () {
angular.element("#datatable").datatable();
}, 10);
或进行调整
像下面这样添加一个空白元素
$scope.temp = { "Name": "", "ID": "", "SSN": "" };
$scope.skills.unshift($scope.temp);
setTimeout(function () {
$scope.skills.shift();
$scope.apply;
}, 10);
上面的代码所做的基本上是将临时记录(即空白)最初添加到现有记录集合中,然后再次将其拉回,通过这种方式进行数据表搜索,在 angular 中检索排序功能。
同样,这将与为数据表创建的指令一起使用,如下所示
app.directive('datatable', function () {
return {
restrict: 'AC',
link: function (scope, element, attrs) {
//
scope.$watch('skills', function (newValue, oldValue) {
if (newValue != undefined) {
if (newValue.length > 0) {
var rows = element.find("tbody").find('tr');
var fixedColumn = 3;
if ($.fn.dataTable.isDataTable(element)) {
var tbl = $(element).dataTable();
tbl.fnClearTable();
for (var i = 0; i < rows.length; i++) {
tbl.fnAddData($(rows[i]));
}
}
else {
element.DataTable({ paging: true, sorting: true, "order": [[0, "asc"]], columnDefs: [{ orderable: false, "targets": fixedColumn }] });
}
element.find('tbody').on('click', 'tr', function () {
$(this).addClass('selected');
$(this).siblings('tr').removeClass('selected');
});
element.fadeIn();
}
}
}, true);
}
}
});
我的 table 在填充 table 后没有搜索或排序也有同样的问题。
angularjs 缺少的是 table 上的数据table="ng"
我最初有数据table=""。
希望对您有所帮助
我在 ng-repeat 完成后使用指令建立数据库:
app.directive('repeatDone', function() {
return function(scope, element, attrs) {
if (scope.$last) {
scope.$eval(attrs.repeatDone);
}
}
});
function InformationController($scope, $http) {
$http.get("people").success(function(response) {
$scope.people = response;
});
$scope.initDataTable = function() {
$('#employeeTable').DataTable({
});
};
};
<table class="table table-striped table-bordered table-hover" id="employeeTable">
<thead>
<tr>
<th>id</th>
<th>Name</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="result in people" repeat-done="initDataTable()">
<td>{{ result.id }}</td>
<td>{{ result.name }}</td>
<td>{{ result.gender }}</td>
</tr>
</tbody>
</table>
数据显示正常,但无法使用默认排序或搜索。
有什么问题吗?我应该怎么做才能启用排序和搜索?
您不能直接使用 angular 中的数据表功能,因为这与重叠的执行序列和功能受到干扰高度相关。您可以像下面这样具体地引发超时
setTimeout(function () {
angular.element("#datatable").datatable();
}, 10);
或进行调整
像下面这样添加一个空白元素
$scope.temp = { "Name": "", "ID": "", "SSN": "" };
$scope.skills.unshift($scope.temp);
setTimeout(function () {
$scope.skills.shift();
$scope.apply;
}, 10);
上面的代码所做的基本上是将临时记录(即空白)最初添加到现有记录集合中,然后再次将其拉回,通过这种方式进行数据表搜索,在 angular 中检索排序功能。
同样,这将与为数据表创建的指令一起使用,如下所示
app.directive('datatable', function () {
return {
restrict: 'AC',
link: function (scope, element, attrs) {
//
scope.$watch('skills', function (newValue, oldValue) {
if (newValue != undefined) {
if (newValue.length > 0) {
var rows = element.find("tbody").find('tr');
var fixedColumn = 3;
if ($.fn.dataTable.isDataTable(element)) {
var tbl = $(element).dataTable();
tbl.fnClearTable();
for (var i = 0; i < rows.length; i++) {
tbl.fnAddData($(rows[i]));
}
}
else {
element.DataTable({ paging: true, sorting: true, "order": [[0, "asc"]], columnDefs: [{ orderable: false, "targets": fixedColumn }] });
}
element.find('tbody').on('click', 'tr', function () {
$(this).addClass('selected');
$(this).siblings('tr').removeClass('selected');
});
element.fadeIn();
}
}
}, true);
}
}
});
我的 table 在填充 table 后没有搜索或排序也有同样的问题。
angularjs 缺少的是 table 上的数据table="ng" 我最初有数据table=""。
希望对您有所帮助