Using enumeration in sorting function

Using enumeration in sorting function

FabioLucasFabioLucas Posts: 3Questions: 1Answers: 0

I have a table with different fields. One of them contains keywords like "good", "bad", "terrible", "perfect". I would like to sort this column in a custom order (keywords would be enumeration like "perfect" = 1, "good" = 2, "bad" = 3, "terrible" = 4). I found the enumeration in the documentation but I did not succeed in using it properly. Can someone help? If you need a reference, here you find an example: http://www.lyra.it/blog/movie-list/

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin

    Sounds like you want this plug-in :-)

    Allan

  • FabioLucasFabioLucas Posts: 3Questions: 1Answers: 0
    edited January 2015

    Thanks, I tried that already but I did not manage to make it work. This was my code: (hope it gets formatted properly)

    jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    
                "enum-pre": function ( a ) {
    
                    // Add / alter the switch statement below to match your enum list
    
                    switch( a ) {
                        case "capolavoro": 1;
                        case "epico": 2; 
                        case "interessante": 3; 
                        case "buono": 4; 
                        case "buono,<br />noioso": 5; 
                        case "passabile": 6; 
                        case "deludente": 7;
                        case "noioso": 10; 
                        case "solo una storia": 20;
                        case "meglio un foglio bianco": 30;
                        case "barocco": 40;
                        case "inutile": 50; 
                        default: return 100;
                    }
                },
             
                "enum-asc": function ( a, b ) {
                    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
                },
             
                "enum-desc": function ( a, b ) {
                    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
                }
            } );
    

    How can I associate this sorting to a column?

  • allanallan Posts: 63,689Questions: 1Answers: 10,500 Site admin
    Answer ✓

    Using the columns.type option. There is an example available here.

    Allan

  • FabioLucasFabioLucas Posts: 3Questions: 1Answers: 0

    Thanks a lot, that helped!

This discussion has been closed.