Custom sorting with Angular DataTables directive
Asked Answered
S

0

9

I am using Angular DataTables within my app. So far everything works well, except when I try to add custom sorting.

I have a set of data that returns a hyphen, "-" if there is no data. Here is my sorting function::

$.fn.dataTableExt.oSort['nullable-asc'] = function(a,b) {

    if (a == '-')
        return 1;
    else if (b == '-')
        return -1;
    else
    {
        var ia = parseInt(a);
        var ib = parseInt(b);
        return (ia<ib) ? -1 : ((ia > ib) ? 1 : 0);
    }
}

$.fn.dataTableExt.oSort['nullable-desc'] = function(a,b) {
console.log(a,b)
    if (a == '-')
        return 1;
    else if (b == '-')
        return -1;
    else
    {
        var ia = parseInt(a);
        var ib = parseInt(b);
        return (ia>ib) ? -1 : ((ia < ib) ? 1 : 0);
    }
}

..start controller...

    vm.dtColumnDefs = [
        DTColumnDefBuilder.newColumnDef(0).notSortable().withClass('hidden-print')
        DTColumnDefBuilder.newColumnDef(1).withClass('td-fieldname'), //name
        DTColumnDefBuilder.newColumnDef(2), //fieldType
        DTColumnDefBuilder.newColumnDef(3).withOption('type', 'html-num-fmt'),
        DTColumnDefBuilder.newColumnDef(4).notSortable(), //distribution
        DTColumnDefBuilder.newColumnDef(5).withOption('type', 'html-num-fmt'), //cardinality
        DTColumnDefBuilder.newColumnDef(6).withOption('type', 'nullable'), //Min
        DTColumnDefBuilder.newColumnDef(7).withOption('type', 'nullable') //Max
    ];

...other angular code..... 

This works just fine when tried on a standard table, but it doesn't play nice with the Angular app.

When I click on the heading of the table to sort - nothing happens. console.log above yields blank values. Is there a way to use custom sorting with the Angular directive?

Saxen answered 13/4, 2015 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.