How can I force the 2nd index of aDataSort to sort 'asc' regardless of the target's sort order?
How can I force the 2nd index of aDataSort to sort 'asc' regardless of the target's sort order?
I'm using DataTables as the main list view of a media player web app. When sorting by "Album" I have `aDataSort` setup to sort by "Album", then by "Track number", and finally by "Title" (in case `trackno` is null). My goal is to always have the tracks in the intended `asc` order, regardless of how the target `album` column is being sorted. Here's what I have for `aoColumns` at index 3, `album` :
[code]{ "sName": "album", "sClass": "album", "aDataSort": [ 3, 7, 2 ], // 3: album, 7: trackno, 8: title
"mData": function ( data, type, val ) {
if (type === 'set') {
if (!_.isString(val)) { val = '' }
data['album'] = val;
data['album-sort'] = _.toSortString(val);
data['album-display'] = _.toTitleCase(val);
return;
}
else if (type === 'display') { return data['album-display']; }
else if (type === 'sort') { return data['album-sort']; }
return data['album'];
},
},[/code]
And for index 7, `trackno` :
[code]{ "bVisible": false, "sName": "trackno", "asSorting": [ "asc" ],
"mData": function ( data, type, val ) {
if (type === 'set') {
if (_.isUndefined(val) || _.isNull(val) || ''+val === '-1') { val = 99 }
data['trackno'] = Math.floor(val);
return;
}
return data['trackno'];
}
},[/code]
Sorting the "Album" column by `asc` works as I'd expect, however when I click again to sort `desc` things aren't quite right. It correctly sorts the initial `album` column by `desc` but it's also doing the second round of sub-sorting in `desc` even though I have `trackno` 's `asSorting` set to `["asc"]` .
How can I force the the `trackno` column to always be `asc` even when being used in other columns multisorting?
[code]{ "sName": "album", "sClass": "album", "aDataSort": [ 3, 7, 2 ], // 3: album, 7: trackno, 8: title
"mData": function ( data, type, val ) {
if (type === 'set') {
if (!_.isString(val)) { val = '' }
data['album'] = val;
data['album-sort'] = _.toSortString(val);
data['album-display'] = _.toTitleCase(val);
return;
}
else if (type === 'display') { return data['album-display']; }
else if (type === 'sort') { return data['album-sort']; }
return data['album'];
},
},[/code]
And for index 7, `trackno` :
[code]{ "bVisible": false, "sName": "trackno", "asSorting": [ "asc" ],
"mData": function ( data, type, val ) {
if (type === 'set') {
if (_.isUndefined(val) || _.isNull(val) || ''+val === '-1') { val = 99 }
data['trackno'] = Math.floor(val);
return;
}
return data['trackno'];
}
},[/code]
Sorting the "Album" column by `asc` works as I'd expect, however when I click again to sort `desc` things aren't quite right. It correctly sorts the initial `album` column by `desc` but it's also doing the second round of sub-sorting in `desc` even though I have `trackno` 's `asSorting` set to `["asc"]` .
How can I force the the `trackno` column to always be `asc` even when being used in other columns multisorting?
This discussion has been closed.
Replies
[code]"mRender": function ( mData, type, row, direction ) {
if (type === 'sort') {
return (row['album-sort'] + ' ' + (row['trackno'] * direction) + ' ' + row['title']);
}
return mData;
}[/code]
EDIT:
[code]{ "sName": "album", "sClass": "album", //"aDataSort": [ 3, 7, 2 ],
"mData": function ( data, type, val ) {
if (type === 'set') {
if (!_.isString(val)) { val = '' }
data['album'] = val;
data['album-sort'] = _.toSortString(val);
data['album-display'] = _.toTitleCase(val);
return;
} else if (type === 'display') { return data['album-display']; }
return data['album'];
},
"mRender": function ( val, type, row ) {
if (type === 'sort') {
return (row['album-sort'] + ' ' + row['trackno']);
}
return val;
}
}[/code]
Taking the `row['title']` out of the return and removing `aDataSort` from `albums` column def seems to have solved my issue.
Unless then, what you could do is bind your own sort event handler to the column and call fnSort as required - i.e. just unbind the 'click' listener DataTables puts on the column header, and bind your own which will call fnSort with a 2 column sort.
Allan