Num-html sorting not sorting

Num-html sorting not sorting

PierceMcGeoughPierceMcGeough Posts: 6Questions: 2Answers: 0

I have added the num-html plugin but its not sorting acs/desc when i click the title. The order is working on first page load but clicking the change order direction doesn't do anything. If I go to a different page it and click the header it will bring back to initial page load.

$.extend($.fn.dataTableExt.oSort, {
    "num-html-pre": function (a) {
        var x = String(a).replace(/<[\s\S]*?>/g, "");
        return parseFloat(x);
    },

    "num-html-asc": function (a, b) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },

    "num-html-desc": function (a, b) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
});


$('#datatable1').DataTable({
  columnDefs: [
    { targets: 0, type: 'num-html' }
  ]
});

Please see code pen https://codepen.io/piercemcgeough/pen/mdEdwpG

This question has an accepted answers - jump to answer

Answers

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

    Column 0 isn't numeric, it's a string - you've got values like "AN Other3" so it won't work. You can just use the built-in columns.type value of html,

    Colin

  • PierceMcGeoughPierceMcGeough Posts: 6Questions: 2Answers: 0

    I thought thats what the num-html was doing. I've tried swapping that out for just html but no joy.

    I was looking AN Other1, AN Other2, AN Other3,
    instead of AN Other1, AN Other100, AN Other101,

  • allanallan Posts: 63,213Questions: 1Answers: 10,415 Site admin
    Answer ✓

    DataTables will detect number columns (including those with HTML) automatically. The columns.type option should basically never be used - it means that the automatic type detection has failed to match your column and therefore it won't sort correctly even if forced to use it.

    I think you probably want a natural sort for this kind of column.

    Allan

  • PierceMcGeoughPierceMcGeough Posts: 6Questions: 2Answers: 0

    Thanks Allan that does the trick.

This discussion has been closed.