سؤال

So I using custom parser to try and get tablesorter to sort like sql server. The data I am returning from entity call can be max 50 characters (varchar(50)).

Here is my parser code:

$.tablesorter.addParser({
    // set a unique id 
    id: 'entitySort',
    is: function (s) {
        // return false so this parser is not auto detected 
        return false;
    },
    format: function (s) {
        // format your data for normalization 

        // remove any special characters that are not alphabet or number
        return s.toLowerCase().replace(/[^a-z0-9]/g, '');
    },
    // set type, either numeric or text 
    type: 'text'
});
$('#headerTable').tablesorter({
    headers: {
        0: { sorter: 'entitySort' }
    },
    widthFixed: false,
    widgets: ["stickyHeaders"],
    widgetOptions: {
        // css class name applied to the sticky header
        stickyHeaders: "tablesorter-stickyHeader",
        stickyHeaders_addResizeEvent: true
    }
});

Here is a sample of the data that is returned via entity call:

1190001676346NB
1910-AM
309AMXG_QP
309CMXG_533: LA-C130/AS057-2222
523:2.9
523PAA2.15
526PAH2.33/ROTOR-76696A
AMXG-QP

First thing I did was remove all special characters on the data thinking that would help to sort:

1190001676346NB
1910AM
309AMXGQP
309CMXG533LAC130AS0572222
52329
523PAA215
526PAH233ROTOR76696A
AMXGQP

However the tablesorter always wants to sort by number size first on text or numeric

309AMXGQP
309CMXG533LAC130AS0572222
523PAA215
526PAH233ROTOR76696A
1910AM
52329
1190001676346NB
AMXGQP

I tried padding the end of the strings with '0' to get them all the same length. I tried changing type of sort to numeric, alphanumeric and nothing is working to sort the same as sql server. I realize the data is intensive to sort. I am desperate for help!

هل كانت مفيدة؟

المحلول

You can use the textSorter option to change the way a column is sorted. Here is an example:

$('table').tablesorter({
    theme: 'blue',
    // custom text sorter
    textSorter: {
        // text sorter for the first column only
        0 : function(a, b){
            return a > b ? 1 : (a < b ? -1 : 0);
        }
    }
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top