locked
Problem with ordering datatable when it has no records RRS feed

  • Question

  • User-1821287852 posted

    hi, 

    i am trying to order a bootstrap datatable for a specific column, but i get an error when the datatable returns no records. 

    the code is:

            <div class="widget-body">
                <table class="table" id="tab_oe">
    
                    @foreach (var item in Model)
    ...
                        }
                        else
                        {
                            <thead><tr><th> Tabela sem registos</th></tr></thead>
                        }
                    }
                </table>
            </div>
    
    
            $('#tab_oe').DataTable({
                "order": [[3, "desc"]],
                "bLengthChange": false,
                "bFilter": false,
                "iDisplayLength": 10,
                "oLanguage": {
                    "sInfo": "Registos _START_ até _END_ de _TOTAL_",
                    "sInfoFiltered": "(filtrado de _MAX_ registos totais)",
                    "sInfoEmpty": "Sem registos",
                    "oPaginate": {
                        "sNext": "Próxima",
                        "sPrevious": "Anterior"
                    }
                }
            });

    the error i get is "Unhandled exception at line 1, column 29796 in http://localhost:52998/Scripts/plugi...aTables.min.js 0x800a138f - JavaScript runtime error: Unable to get property 'aDataSort' of undefined or null reference".

    any hints on this?

    regards,

    bruno,

    Monday, October 10, 2016 11:53 PM

Answers

  • User283571144 posted

    Hi santiago17,

    the error i get is "Unhandled exception at line 1, column 29796 in http://localhost:52998/Scripts/plugi...aTables.min.js 0x800a138f - JavaScript runtime error: Unable to get property 'aDataSort' of undefined or null reference".

    any hints on this?

    As far as I know, this error means jQuery Datatable order method couldn't find the column which index is '3'.

    It means column not exists.

    As you say, this error occurs when it has no records.

    So I suggest you could use if condition to check html table column's number.

    If this number has value and is more than order column's index you could use jQuery datatable.

    More details, you could refer to follow js function:

         var numCols = $("#tab_oe").find('tr')[0].cells.length; //get column's number
                alert('Total columns : ' + numCols);
                if (numCols > 3) { //Check 
                    $('#tab_oe').DataTable({
                        "order": [[3, "desc"]],
                        "bLengthChange": false,
                        "bFilter": false,
                        "iDisplayLength": 10,
                        "oLanguage": {
                            "sInfo": "Registos _START_ até _END_ de _TOTAL_",
                            "sInfoFiltered": "(filtrado de _MAX_ registos totais)",
                            "sInfoEmpty": "Sem registos",
                            "oPaginate": {
                                "sNext": "Próxima",
                                "sPrevious": "Anterior"
                            }
                        }
                    });
                }

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 11, 2016 2:37 AM

All replies

  • User283571144 posted

    Hi santiago17,

    the error i get is "Unhandled exception at line 1, column 29796 in http://localhost:52998/Scripts/plugi...aTables.min.js 0x800a138f - JavaScript runtime error: Unable to get property 'aDataSort' of undefined or null reference".

    any hints on this?

    As far as I know, this error means jQuery Datatable order method couldn't find the column which index is '3'.

    It means column not exists.

    As you say, this error occurs when it has no records.

    So I suggest you could use if condition to check html table column's number.

    If this number has value and is more than order column's index you could use jQuery datatable.

    More details, you could refer to follow js function:

         var numCols = $("#tab_oe").find('tr')[0].cells.length; //get column's number
                alert('Total columns : ' + numCols);
                if (numCols > 3) { //Check 
                    $('#tab_oe').DataTable({
                        "order": [[3, "desc"]],
                        "bLengthChange": false,
                        "bFilter": false,
                        "iDisplayLength": 10,
                        "oLanguage": {
                            "sInfo": "Registos _START_ até _END_ de _TOTAL_",
                            "sInfoFiltered": "(filtrado de _MAX_ registos totais)",
                            "sInfoEmpty": "Sem registos",
                            "oPaginate": {
                                "sNext": "Próxima",
                                "sPrevious": "Anterior"
                            }
                        }
                    });
                }

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 11, 2016 2:37 AM
  • User-1821287852 posted

    Hi Brando, 

    that works. thanks

    kind regards

    Tuesday, October 11, 2016 8:05 AM