Custom order 1,2,3,4 as 4,2,1,3 for example

Custom order 1,2,3,4 as 4,2,1,3 for example

george.levinesgeorge.levines Posts: 11Questions: 4Answers: 0

How would one best approach this ...

I have a column of values 1-14. When I click the sorter for that column I'd like them to be ordered: 1,2,10,6,3,11,7,4,12,8,5,13,9,14.

I have tried replacing the HTML using a createdRow function and while that 'works' it appears to be too late (i.e., the column still sorts 1,2,3,4,...)

I feel like I'm not fully grokking columnRender or Renderers generally.

For reference my data structure is

ajax: {
            url: 'data/table_house_info.json',
            dataSrc: ''
        },
        "columns": [
            { "data": "ratingPhrase" },
            { "data": "seatID" },
            { "data": "fullName" },
            { "data": "trump_margin" },
            { "data": "firstName" },
            { "data": "lastName" },
            { "data": "raceRatingID", },
            { "data": "open_seat", },
            { "data": "turnover" }
        ],

raceRatingID is the data in question and it will ultimately also be used as the sorting target for ratingPhrase.

This question has an accepted answers - jump to answer

Answers

  • george.levinesgeorge.levines Posts: 11Questions: 4Answers: 0

    Wound up doing this ...

    {
        "targets": 6,
        "data": "raceRatingID",
        "render": function (data, type, row, meta) {
            if (data == "1") {
                return "A"
            } else if (data == "2") {
                return "B"
            } else if (data == "10") {
                return "C"
            } else if (data == "6") {
                return "D"
            } else if (data == "3") {
                return "E"
            } else if (data == "11") {
                return "F"
            } else if (data == "7") {
                return "G"
            } else if (data == "4") {
                return "H"
            } else if (data == "12") {
                return "I"
            } else if (data == "8") {
                return "J"
            } else if (data == "5") {
                return "K"
            } else if (data == "13") {
                return "L"
            } else if (data == "9") {
                return "M"
            } else if (data == "14") {
                return "N"
            }
            else {
                return ""
            }
        }
    } // Sets rating column data based on raceRatingID 1,2,10,6,3,11,7,4,12,8,5,13,9,14
    

    No idea if that's best practice but it's working.

  • kthorngrenkthorngren Posts: 20,264Questions: 26Answers: 4,764
    Answer ✓

    Looks like the Dynamic Enum Sorting plugin will do the trick:
    http://live.datatables.net/joresiqe/1/edit

    This way if you want to change the numeric order you just simply need to change the array sent to the plugin.

    Kevin

  • george.levinesgeorge.levines Posts: 11Questions: 4Answers: 0

    Yep, this is better. :-)

This discussion has been closed.