AngularJS - 默认排序顺序未在搜索中呈现 table
AngularJS - Default Sort Order not rendering table on search
我创建了以下用于排序和搜索的代码。
当我添加 aaSorting
时,我能够得到初始 table 以按我指定的列排序。但是,在搜索中 table 完全消失,即从 DOM.
中删除
没有链接到 dtOptionsBuilder 的默认排序,我可以搜索,但没有设置默认排序顺序。
我该如何解决这个问题?
Angular代码
$scope.searchOrderHistoryData = function(){
if ($scope.searchQuery.trim() == "")
{
$scope.cancel=false;
return;
}
else{
$scope.IssearchQuery = true;
}
$scope.filterOrderHistoryData = $scope.orderHistory.filter(function(r) {
res = "";
found = true;
for (key in r)
{
if ((key == "OrderId" || key == "OrderName" || key == "TotalCost" || key == "SubmittedDate" || key == "SubmittedBy" || key== "Status") && r[key] != null)
res += angular.lowercase(r[key].toString()) + " ";
}
if (res.indexOf(angular.lowercase($scope.searchQuery)) >= 0)
return true;
return false;
});
}
var orderHistoryCols = {'OrderId':0, 'TotalCost':1, 'SubmittedDate':2, 'SubmittedBy':3, 'Status':4};
$scope.dtOptions = DTOptionsBuilder.newOptions()
.withOption('responsive', true)
.withOption('fnDrawCallback', function () {
$('.dataTables_scrollBody').on("scroll", function(){ //activate when #center scrolls
var left = $('.dataTables_scrollBody').scrollLeft(); //save #center position to var left
$('.dataTables_scrollHead').scrollLeft(left); //set #top to var left
});
})
.withOption('bAutoWidth', false)
//.withOption('sorting', false)
.withOption('scrollY', '450px')
.withOption('aaSorting', [[orderHistoryCols.SubmittedDate, 'desc']]);
模板 (HTML)
<table datatable="ng" class="table" dt-options="dtOptions" dt-column-defs="dtColumnDefs">
<thead style="background-color: #d8d8d8;">
<tr>
<!--th ng-hide="true"></th-->
<th>Order #</th>
<th>Total Cost</th>
<th>Submitted</th>
<th>By</th>
<!-- <th>Delivered</th> -->
<th>Status</th>
<th data-orderable="false"></th>
<th data-orderable="false"></th>
</tr>
</thead>
<tbody>
<tr style="font-weight: 100;cursor: pointer;" ng-repeat="data in filterOrderHistoryData">
Table data...
</tr>
</tbody>
</table>
为了仅按一列正确排序,最后一行需要写成一维(大小 = 1)数组。
.withOption('aaSorting', [orderHistoryCols.SubmittedDate, 'desc']);
我原来问题中的数组数组有双括号(左括号和右括号)。
.withOption('aaSorting', [[orderHistoryCols.SubmittedDate, 'desc']]);
我创建了以下用于排序和搜索的代码。
当我添加 aaSorting
时,我能够得到初始 table 以按我指定的列排序。但是,在搜索中 table 完全消失,即从 DOM.
没有链接到 dtOptionsBuilder 的默认排序,我可以搜索,但没有设置默认排序顺序。
我该如何解决这个问题?
Angular代码
$scope.searchOrderHistoryData = function(){
if ($scope.searchQuery.trim() == "")
{
$scope.cancel=false;
return;
}
else{
$scope.IssearchQuery = true;
}
$scope.filterOrderHistoryData = $scope.orderHistory.filter(function(r) {
res = "";
found = true;
for (key in r)
{
if ((key == "OrderId" || key == "OrderName" || key == "TotalCost" || key == "SubmittedDate" || key == "SubmittedBy" || key== "Status") && r[key] != null)
res += angular.lowercase(r[key].toString()) + " ";
}
if (res.indexOf(angular.lowercase($scope.searchQuery)) >= 0)
return true;
return false;
});
}
var orderHistoryCols = {'OrderId':0, 'TotalCost':1, 'SubmittedDate':2, 'SubmittedBy':3, 'Status':4};
$scope.dtOptions = DTOptionsBuilder.newOptions()
.withOption('responsive', true)
.withOption('fnDrawCallback', function () {
$('.dataTables_scrollBody').on("scroll", function(){ //activate when #center scrolls
var left = $('.dataTables_scrollBody').scrollLeft(); //save #center position to var left
$('.dataTables_scrollHead').scrollLeft(left); //set #top to var left
});
})
.withOption('bAutoWidth', false)
//.withOption('sorting', false)
.withOption('scrollY', '450px')
.withOption('aaSorting', [[orderHistoryCols.SubmittedDate, 'desc']]);
模板 (HTML)
<table datatable="ng" class="table" dt-options="dtOptions" dt-column-defs="dtColumnDefs">
<thead style="background-color: #d8d8d8;">
<tr>
<!--th ng-hide="true"></th-->
<th>Order #</th>
<th>Total Cost</th>
<th>Submitted</th>
<th>By</th>
<!-- <th>Delivered</th> -->
<th>Status</th>
<th data-orderable="false"></th>
<th data-orderable="false"></th>
</tr>
</thead>
<tbody>
<tr style="font-weight: 100;cursor: pointer;" ng-repeat="data in filterOrderHistoryData">
Table data...
</tr>
</tbody>
</table>
为了仅按一列正确排序,最后一行需要写成一维(大小 = 1)数组。
.withOption('aaSorting', [orderHistoryCols.SubmittedDate, 'desc']);
我原来问题中的数组数组有双括号(左括号和右括号)。
.withOption('aaSorting', [[orderHistoryCols.SubmittedDate, 'desc']]);