Data tables custom sorting, asc and desc functions are not called

Data tables custom sorting, asc and desc functions are not called

francissfranciss Posts: 2Questions: 1Answers: 0

Hi there.

I have been experiencing a problem with custom sorting. It seems like the asc and desc functions i made are not being called. Could you point me in the right direction? Thank you very much!

        jQuery.fn.dataTableExt.oSort['test-asc']  = function(a,b) {
            console.log(a.toString().toLowerCase());
            return 'a';
        };

        jQuery.fn.dataTableExt.oSort['test-desc']  = function(a,b) {
            console.log('test');
            return;
        };

        var table = $('#companies-table').DataTable({
            "dom": '<"row table-top-bar"<"col-sm-3"l><"#tool-bar-btns.col-sm-6"><"col-sm-3"f>><t><"row"<"col-sm-3"i><"col-sm-9"p>>',
            "bInfo": true,
            "pagingType": "full_numbers",
            responsive: true,
            processing: true,
            serverSide: true,
            "deferRender": true,    

            "columnDefs": [ {
                                "targets": 'no-sort',
                                "orderable": false,
                            }
                        ],
            "ajax": {
                "url": '/company/data',
                'type': 'POST',
                'headers': {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                }
            },

            "columns": [
              { data: 'name', name: 'name', type: 'test'},
              { 
                "data": "id",
                "searchable": false,
                "render" : function(data, type, row, meta){
                    var result = "";
                    if (row['is_deactivated'] == false) {
                        result = "<a href='/company/"+data+"/edit'><button class='btn btn-primary btn-xs'>Edit</button></a>  <button type='button' class='btn btn-primary btn-xs modal-button' data-toggle='modal' data-target='#actionConfirmation' data-id='"+data+"' data-action='Deactivate' data-object='Company' data-name ='" + row['name'] + "'>Deactivate</button>";
                    }
                    else {
                        result = "<a href='/company/"+data+"/edit'><button class='btn btn-primary btn-xs'>Edit</button></a>  <button type='button' class='btn btn-primary btn-xs' modal-button' data-toggle='modal' data-target='#actionConfirmation' data-id='"+data+"' data-action='Activate' data-object='Company'  data-name ='" + row['name'] + "'>Activate</button>";
                    }                        
                    return result;
                }
              }
              ]
        });

Answers

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49
    edited March 2016

    You are using "columns" and "columnDefs", you should only be using one as they ultimately serve the same purpose. Try that and see it you can get your functions to fire, at first glance everything looks good.

    If that doesn't fix it, try replacing

    jQuery.fn.dataTableExt.oSort
    

    with

    jQuery.fn.dataTable.ext.type.order
    
  • francissfranciss Posts: 2Questions: 1Answers: 0

    Hi jr42.gordon,

    Thanks for your answer. I tried both your advices but sadly it didn't fix it. the asc and desc functions are still not called.

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    Darn. Okay, instead of -asc or -desc, try using '-pre'.

    If that doesn't work then I do not know. None of my DataTables use server-side processing, I pull the data via Ajax and maintain accordingly.

This discussion has been closed.