Can't work out how to stop user being able to change the initial load order

Can't work out how to stop user being able to change the initial load order

sketchgalsketchgal Posts: 4Questions: 1Answers: 0

I am using datatables to display a list of sales staff along with their monthly sales results and targets. I've got the tables set up so that when it initially loads it orders the data based on the grad total percentage column, which works fine. I can't get it to not allow a user to then change the sort order by clicking on the column headers. I donlt want anyone to be able to change the order of the table once it has initially loaded the data in and sorted it by the column I have set. The code I'm using is below.

$(document).ready(function() {
    var t = $('#myTable').DataTable( {
        "columnDefs": [ {
            "searchable": false,
            "orderable": false,
            "targets": 0,
            "rowReorder": false,
            "bFilter": false,
            "bPaginate": false,
            "bLengthChange": false,
        } ],
        "order": [[ 11, 'desc' ]],
        "bFilter": false,
        "bPaginate": false,
        "bLengthChange": false,
        "orderFixed": [ 11, 'desc' ],
        "rowReorder": false
    } );
        
 
    t.on( 'order.dt search.dt', function () {
        t.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
            cell.innerHTML = i+1;
        } );
    } ).draw();
} );

Can anyone help me get this to work please?

Answers

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    You can use ordering for this.

    Kevin

  • sketchgalsketchgal Posts: 4Questions: 1Answers: 0

    I've tried putting the ordering option into the code I have but when I do that the initial order I need when the tables loads gets cancelled aswel! Is there a way for me to keep the initial order when the page loads that I can and then to stop this being changed?

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    I think I see one problem. According to the docs the columnDefs needs to have a target assignment. See if that helps.

    Kevin

  • sketchgalsketchgal Posts: 4Questions: 1Answers: 0

    Thanks for your help with this Kevin.
    How would I put that into the code I have? Can you show me what I would need to change please?

  • kthorngrenkthorngren Posts: 21,344Questions: 26Answers: 4,954

    If I understand correctly you want all ordering disabled for all columns. If so then I think you need to change "targets": 0 to "targets": "_all".

    Kevin

  • sketchgalsketchgal Posts: 4Questions: 1Answers: 0

    Brilliant! I tried that way and it didn't work for keeping the initial order but I've just put in all my other columns as targets except the one I need for the initial order and it works! Thanks very much fro the help Kevin!

This discussion has been closed.