DataTables grouping with ordering
DataTables grouping with ordering
Abdelrahman_Alnashar
Posts: 10Questions: 2Answers: 0
I have a DataTable for grouping users who are active and inactive, I want to be able to order the inactive users first, then the active ones. The code below results in grouping the active users first, I want the opposite.
columnDefs: [
{ visible: true, targets: groupColumn }
],
order: [[groupColumn, 'asc']],
displayLength: 10,
lengthMenu: [10, 15, 20, 25, 30],
drawCallback: function(settings) {
var api = this.api();
var rows = api.rows({ page: 'current' }).nodes();
var last = null;
api.column(groupColumn, { page: 'current' })
.data()
.each(function(user_status, i) {
var group = user_status === "Inactive" ? "Inactive Users" : "Active Users";
if (last !== group) {
$(rows)
.eq(i)
.before(
'<tr class="group" ' + groupStyle + '><td colspan="4">' +
group +
'</td></tr>'
);
last = group;
}
});
}
Answers
I'm not clear how that grouping would be expected to work with searching and ordering. It would be worth looking at RowGroup, or possibly just using
option order
to set an initial order to the table. If you want the Active users to always be at the top of the table, it would be worth looking at the Absolute ordering plugin.Colin
I added the Absolute ordering plugin, and I am still getting the active users to be ordered first in the dataTable.
@colin
Use
orderFixed
to apply an initial order to the table that the end user cannot change (unless you provide a way to do so via the API).Allan
I hope this helps. As you can see, if the user_name is "Inactive" then the user will be displayed in the Inactive User's group; otherwise, it will be displayed in the Active User's group. What I want exactly is for the Inactive User's group to be displayed first in the data table.
Thanks for the clarification - it sounds like the absolute sorting described here will be what you want. Set it up so a column displayed the
user_name
data point and us the absolute sorting plugin to show "Inactive" first. The row grouping is based off the sorting order.Allan