How to get the sorted index in angular way
Hello, I'm working on a project in which on first page i have angular-datatables rendered using the angular-way. Now, when the user re-sorts any columns, I need to grab the re-sorted index and navigate to next page where details of each row loads up. User is given the option to load "next item" based on the index. How to get this resorted index?
Thank you.
From this answer:
<div ng-controller="AngularWayWithOptionsCtrl as showCase">
<table datatable="ng"
dt-options="showCase.dtOptions"
dt-column-defs="showCase.dtColumnDefs"
class="row-border hover"
dt-instance="showCase.dtInstance">
<thead>
<tr>
<th>ID</th>
<th>FirstName</th>
<th>LastName</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in showCase.persons"
ng-click="showCase.displayIndex(person.id, $event)">
<td>{{ person.id }}</td>
<td>{{ person.firstName }}</td>
<td>{{ person.lastName }}</td>
</tr>
</tbody>
</table>
</div>
angular.module('showcase.angularWay.withOptions', ['datatables', 'ngResource'])
.controller('AngularWayWithOptionsCtrl', AngularWayWithOptionsCtrl);
function AngularWayWithOptionsCtrl($resource, DTOptionsBuilder, DTColumnDefBuilder) {
var vm = this;
vm.persons = [];
vm.dtOptions = DTOptionsBuilder.newOptions()
.withPaginationType('full_numbers')
.withDisplayLength(2);
vm.dtColumnDefs = [
DTColumnDefBuilder.newColumnDef(0),
DTColumnDefBuilder.newColumnDef(1).notVisible(),
DTColumnDefBuilder.newColumnDef(2).notSortable()
];
$resource('data.json').query().$promise.then(function(persons) {
vm.persons = persons;
});
vm.dtInstance = {};
vm.displayIndex = function(id, $event) {
var index = vm.dtInstance.DataTable
.rows({order: 'applied'})
.nodes()
.indexOf($event.currentTarget);
console.log(id, index);
};
}
Hey Lin, Thank you for your response. Really appreciated. This solution logs the current row index. I'm seeking a way to get the entire re-sorted table. This solution gave me great head start and now I'm trying to use this solution to get what I need for the project. If you happen to know how to get it done then please let me know. Thank you so much.
What about vm.dtInstance.DataTable.rows({order: 'applied'}).nodes()?
I got it to work. This console logs the entire re-sorted table.
vm.displayIndex = function(id, $event) {
var index = vm.dtInstance.DataTable.rows({order: "applied"})[0];
for(var i = 0; i < index.length; i++){
console.log(vm.persons[index[i]]);
}
};
Thank you so much Lin for that headstart