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?

jcreadyjcready Posts: 44Questions: 0Answers: 0
edited August 2012 in General
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?

Replies

  • jcreadyjcready Posts: 44Questions: 0Answers: 0
    edited August 2012
    If sort direction was provided to `mRender` as a fourth parameter then I could easily append the value of `trackno` and flip the value depending on the sort direction param. I figure the parameter could simply be `1` for "asc", `0` for when the column isn't being sorted, and `-1` for "desc".

    [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.
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    There currently isn't a way of telling aDataSort what direction of sorting you want - it is based on the parent's sort direction when using it for column column sorting. Definitely a limitation and one I will look at addressing.

    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
  • arcaninearcanine Posts: 16Questions: 0Answers: 0
    Apologies if this sounds naive, but how do you target a specific column header with jquery (or otherwise) for the purposes of unbinding the data tables click event?
This discussion has been closed.