Sort column in angular 'datatables'

2019-05-22 23:27发布

I created table

<table class="table table-striped table-bordered table-hover" width="100%" datatable="ng" dt-options="options">
<thead>
<tr>
    <th> Nannie ID</th>
    <th> Name</th>
    <th> Last name</th>
    <th> Email</th>
</tr>
</thead>
<tbody>
<tr class="" ng-repeat='item in items'>
    <td><a ui-sref="admin.nanniesEdit({id:item.id})">id{{item.id}}</a></td>
    <td>{{item.profile.name}}</td>
    <td>{{item.lastname}}</td>
    <td>{{item.profile.email}}</td>
</tr>
</tbody>

Table loaded with first column order:

NannieID
id1 
id10    
id12    
id13    
id2 
id3 
id5 

I want get correct order for each click reorder, and when first loading. Expected result:

NannieID
id1 
id2 
id3 
id5 
id10    
id12    
id13    

I added this code, but it helped only when table is loading, after click for reorder column, I got wrong order

$scope.options = DTOptionsBuilder.newOptions().withOption('aaSorting', [[5, 'asc']])

Please, help me

2条回答
冷血范
2楼-- · 2019-05-23 00:03

Change the aaSorting to order. Your code will be like:

$scope.options = DTOptionsBuilder.newOptions().withOption('order', [[5, 'asc']])
查看更多
一夜七次
3楼-- · 2019-05-23 00:04

You can reorder the array of objects with the javascript function sort

$scope.asc = false;
$scope.reorder = function(direction){
    items.sort(function(a, b) {
      return a.id - b.id;
    });
    if(asc){
        items.reverse();
        asc = false;
    }
}

And add a button with an ng-click calling reorder

<button ng-click="$scope.reorder()">Reorder</button>

Every time you press the Reorder button, your list is going to switch from Asc Order and Desc Order

查看更多
登录 后发表回答