Reputation: 5965
I have the following data I'm passing to a DataTables table:
data = [{'state': 'Baseline', 'contact': 'Johnny', 'years_active': 10},
{'state': '1', 'contact': 'Rachel', 'years_active': 10},
{'state': '2', 'contact': 'Steve', 'years_active': 8}]
The table has three columns: state
, contact
, and years_active
. When I sort on the state
column, the row with state='Baseline'
is at the bottom of the table. Is there any way to adjust the sorting mechanism so this row will be on top?
Maybe convert Baseline
to '0'
in the background or maybe define character values as less then number values?
Upvotes: 1
Views: 69
Reputation: 85528
You can control the sort value in the columns render
callback. The type
argument defines what scope the returned value will be used for, i.e filter, sort, display. With the magic of the ||
operator you can return -1 when the column is about to be sorted, and the value is not a number :
columns: [
{ data: 'state',
render: function(data, type) {
if (type == 'sort') {
return parseInt(data) || -1
}
return data
}
},
{ data: 'contact' },
{ data: 'years_active' }
]
see demo -> http://jsfiddle.net/721hdxa5/
Upvotes: 1