How To Stop Ordering of Columns Entirely

How To Stop Ordering of Columns Entirely

HansWorthHansWorth Posts: 5Questions: 3Answers: 0

Hello everyone. My company has a in-house contest that we are running. I'm trying to display the results of the winners on our webpage. Because of the nature of the contest, I'm trying to both keep users from ordering and sorting the results, as well as avoid any ordering done by Data Tables itself. I want to be able to display the results exactly as fed to it by the server.

Included in my code as well is some javascript to create totals in the footer. I'm not having much trouble with that, but it's being included just in case there's a conflict.

My code is as follows:

 $(document).ready(function () {
        $.noConflict();
        $(target1).DataTable({
            "footerCallback": function (row, data, start, end, display) {
                var api = this.api(), data;

                // Remove the formatting to get integer data for summation
                var intVal = function (i) {
                    return typeof i === 'string' ?
                        i.replace(/[\$,]/g, '') * 1 :
                        typeof i === 'number' ?
                            i : 0;
                };
                //Creates the appropriate total through calling the API for the given column number (starting with 0) and formatting according to the symbol given
                function totalMaker(number, symbol) {
                    // Total over all pages
                    total = api
                        .column(number)
                        .data()
                        .reduce(function (a, b) {
                            return intVal(a) + intVal(b);
                        }, 0);
                    // Total over this page
                    pageTotal = api
                        .column(number, { page: 'current' })
                        .data()
                        .reduce(function (a, b) {
                            return intVal(a) + intVal(b);
                        }, 0);
                    switch (symbol) {
                        case "$":
                            result = $(api.column(number).footer()).html(
                                '$' + pageTotal + ' ($' + total + ')');
                            break;
                        default:
                            result = $(api.column(number).footer()).html(
                                pageTotal + ' (' + total + ')');
                    }
                    return result
                }
                totalMaker(1, "n");
                totalMaker(2, "$");



            }
            ,"order": []
            , columnDefs: [
                { targets: '_all', orderable: false }
            ]
            ,"ordering" : false
        });
    });

</script> 

As you can see I've attempted to stop all ordering, however I still am able to reorder by clicking the table headers. I'm at my wit's end. What suggestions do you have?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    Answer ✓

    This example uses order and ordering, as you have coded it, to disable table ordering and show the table in the order of the data:
    http://live.datatables.net/qowoxile/1/edit

    I don't see anything obvious in the above code that would allow for clicking to order the table. Can you post a link to your page or a test case replicating the so we can help debug?

    Do you have any HTML5 Attributes enabling ordering?

    Kevin

  • HansWorthHansWorth Posts: 5Questions: 3Answers: 0

    Thank you @kthorngren, your question about the HTML5 Attributes led me to look again at the layout page and I found an old script that was trying to sort things. I got rid of it and the problem fixed itself. I inherited this project from another developer, I should've thought of that. Thank you.

This discussion has been closed.