Datatables 1.10 - HTML5 "data-order" attr in TD does not affect

I'm having trouble sorting a column that has HTML in it. The documentation for 1.10 states that this should be taken care of by default, but it is not. Then I examined the new features of 1.10 and saw that if for each TD element in the same column there was an attribute "data order", the order can be executed with the specified attributes. Fine! The problem is that I cannot get it to work.

The strange thing is that the example that they have when the static page works as expected, but not when the data and the table are loaded dynamically.

I am running a table with the following parameters and changes to add attributes. Invalidity is performed to tell the Datatables that she should redraw it (I saw that she was needed somewhere):

"createdRow": function ( row, data, index ) {
                if ( data[6] ) {
                    cell = $('td', row).eq(6);
                    value = cell.text();
                    if(value == "Ej fakturerad") {
                        cell.attr('data-order', 1);
                    }
                    else if(value == "Nej") {
                        cell.attr('data-order', 2);
                    }
                    else if(value == "Kredit") {
                        cell.attr('data-order', 3);
                    }
                    else if(value == "Ja") {
                        cell.attr('data-order', 4);
                    }
                }
                oTable
                    .row( index )
                    .invalidate()
                    .draw();
            },

I am implementing this DataTable with a composer package from Chumper / datatables in a Laravel project, which means the data source is Ajax, and uses server side processing.

Thanks in advance!

+4
source share
1 answer

CAUSE

Studying the source code for DataTables, it seems that HTML5 attributes are data-only read during table initialization and only for static data.

Decision

Decision No. 1

, . . .

, , :

{
    "name": "Tiger Nixon",
    "position": "System Architect",
    "salary": "$3,120",
    "start_date": {
        "display": "Mon 25th Apr 11",
        "timestamp": "1303682400"
    },
    "office": "Edinburgh",
    "extn": "5421"
}

DataTables :

$(document).ready(function() {
    $('#example').dataTable( {
        ajax: "data/orthogonal.txt",
        columns: [
            { data: "name" },
            { data: "position" },
            { data: "office" },
            { data: "extn" },
            { data: {
                _:    "start_date.display",
                sort: "start_date.timestamp"
            } },
            { data: "salary" }
        ]
    } );
} );

start_data.display , start_data.timestamp .

№2

columns.render , :

$('#example').dataTable({
    "columnDefs": [{
        "targets": 6,
        "data": "field_name",
        "render": function (data, type, full, meta) {
            if(type === 'sort'){
               if(data === "Ej fakturerad") {
                   data = 1;
               } else if(data === "Nej") {
                   data = 2;
               } else if(data === "Kredit") {
                   data = 3;
               } else if(data === "Ja") {
                   data = 4;
               }
            }

            return data;
        }
    }]
});
+5

Source: https://habr.com/ru/post/1547124/


All Articles