mercredi 21 mars 2018

Sorting table column with jQuery datables in Laravel

I have a column that I need to sort by 2 parameters when I click on header of this column.

Here what I have now:

 $('#states_table').on('processing.dt', function (e, settings, processing) {
        if (processing) {
            $(this).closest('.ibox-content').addClass('sk-loading');
        } else {
            $(this).closest('.ibox-content').removeClass('sk-loading');

            $(document).on('click', '.paginate_button', function() {
                enableTooltips();
                enableDropdown();
            });
        }
    }).DataTable({
        serverSide: false,
        info: false,
        pagingType: 'simple_numbers',
        bLengthChange: false,
        pageLength: 100,
        responsive: true,
        bFilter: false,
        destroy: true,
        aaSorting: [[14,'desc'], [15, 'asc']],
        ColumnDefs: [
            {
                aDataSort: [15, 14],
                aTargts: [4],
            }
        ],
        //aDataSort: [15, 14],
        ajax: {  
            url: '/states/filter',
            type: 'GET',
            data: (data) => { },
            columns: [ ...
                  //this is fourth column that I targeted in ColumnsDef

    {
        data: (data) => {
        if (data.latest_report === null) {
            return _const.ICON_DASH;
        }
            return helpers.createTrendIcon(data.latest_report.general_status_indicator, data.latest_report.general_status_trend);
        }, name: 'State', class: 'state-state text-center v-a-t dashboard-icon-padding'
    },
   ....
   // 14 and 15 columns
    {
        data: 'latest_report.general_status_indicator',
        sDefaultContent: '',
        visible: false,
    },
    {
        data: 'latest_report.general_status_trend',
        sDefaultContent: '',
        visible: false,
    },

Now when I load the page it sorted my table by 4 column in ascending order (or desc, I'm not sure, but it's unimportant now).

But I want to sort the table by click on header of the column (not when I load the table). One more time: I want to load the page and when I click the header of the column the table must be sorted like that:

`aaSorting: [[14,'desc'], [15, 'asc']],
 ColumnDefs: [{
                aDataSort: [15, 14],
                aTargts: [4],
             }],`

And when I click this column one more time - the table must be sorted like that:

`aaSorting: [[14,'asc'], [15, 'asc']],
 ColumnDefs: [{
                aDataSort: [15, 14],
                aTargts: [4],
             }],`



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2GOJUV7
via IFTTT

Aucun commentaire:

Enregistrer un commentaire