Order a column that contains a range(a - b) based on the high value (b)

Order a column that contains a range(a - b) based on the high value (b)

crc2220crc2220 Posts: 4Questions: 0Answers: 0

Is it possible to tell datatables to rely on the high value(b) when sorting cells that contain (a - b). Can I insert something like <td data-sort="b"> a-b </td> and tell DataTables to rely on that data attribute when sorting that column?

Replies

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @crc2220 ,

    Yep, easily done, like this:

          render: function(data, type, row, meta) {
            return (type === 'sort')? data.split('-')[1].trim() : data;
          }
    

    See example here,

    Cheers,

    Colin

  • crc2220crc2220 Posts: 4Questions: 0Answers: 0

    Thanks!

  • crc2220crc2220 Posts: 4Questions: 0Answers: 0

    Actually I'm still having some issues. Any ideas what's going?
    My code is here: http://live.datatables.net/sipakuro/1/edit

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Ah, yep, the reason is because it was string sorting (1,10,100,200,3), as opposed to number (1,3,10,100,200) - you can override that by using columns.type as in this updated example here.

    Cheers,

    Colin

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Worth noting that you can use orthogonal data with HTML attributes as an alternative.

    Internally to DataTables its more or less the same as Colin's suggestion, but if you control how the HTML is being created, it can make things a little easier during initialisation since you don't need a function.

    Allan

  • crc2220crc2220 Posts: 4Questions: 0Answers: 0

    Thank you @colin and @allan !

    Both work.

This discussion has been closed.