asSorting is null or not an object

asSorting is null or not an object

ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
edited October 2010 in General
Hey Allan,

I have a table that gets generated dynamically and the columns are not fixed. Which means that on clicking of a button I am deciding the no. of columns needs to be shown in the table. I am using the "bStateSave": true as one of the properties. Now, if I am showing four columns on clicking of button first time and then perform sorting based on fourth column, the I again click the button and showing three columns since columns are displayed based on certain conditions then it throws "asSorting is null or not an object" error since the datatable tries to perform the sorting based on fourth column (because I am using "bStateSave" property) which is not exists in this condition.

To reproduce this error you can simply take code of "Show and hide columns dynamically", set its "bStateSave" property, on first load simply perform sorting based on last column and then from the code remove that column and refresh the page.

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi ansarisufiyan,

    An interesting one! It's probably not too surprising that this happens, since it's trying to sort on a column which is not longer there. However, what the correct solution is, isn't particularly obvious. There are a few options:

    1. Remove that option from the search array - but then you might end up with no sorting
    2. Set that sort option to the first column
    3. Alter the sorting code to ignore columns which are unknown

    3 doesn't sound great since it would add a bit more processor time to the sorting, and 1 / 2 might be the wrong interaction for some devs. I'm included to go for option 2 though... What do you think?

    Allan
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    Hey Allan,

    Thanks for the reply. As per your suggestion I have tried implementing the option 2. However, I am still getting the same error since sorting property only applied to the datatables if bStateSave property is false. (Note: I am using datatables 1.7.1)

    Also, I would rather like to go with the hybrid approach of option 2 and 3. Something like if the column is not exists then perform sorting based on 1st column (0 index column). If the column exists then perform sorting based on that column. Any thoughts of how can we achieve the same?

    Sufiyan Ansari
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    First things first - I'd suggest upgrading to 1.7.3, since there has been one or two little changes to the state handling code for the column visibility.

    How did you try to implement the code in DataTables? It will probably need to go in the sanity checking of aaSorting during initialisation.

    Allan
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    Upgraded to datatables 1.7.3 and tried option 2 in show/hide example. Still getting the same error (i.e asSorting is null or not an object). Please find the code below.

    Initial code:




    Rendering engine
    Browser
    Platform(s)




    Trident
    Internet
    Explorer 4.0
    Win 95+




    Rendering engine
    Browser
    Platform(s)




    $(document).ready(function() {
    $('#example').dataTable( {
    "bPaginate": false,
    "bStateSave": true,
    "aoColumnDefs": [ { "asSorting": [ "asc" ], "aTargets": [ 0 ] } ]
    });
    });

    Now, from the browser I performed sorting on third column and then remove the third column from the code. Now, the code will be




    Rendering engine
    Browser




    Trident
    Internet
    Explorer 4.0




    Rendering engine
    Browser




    $(document).ready(function() {
    $('#example').dataTable( {
    "bPaginate": false,
    "bStateSave": true,
    "aoColumnDefs": [ { "asSorting": [ "asc" ], "aTargets": [ 0 ] } ]
    });
    });

    Now, if I go to browser and refreshes the same page then I am getting an error.
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    Any help?
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    Still waiting for the reply.
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    edited October 2010
    Sorry for the delay - if you want a prompt reply you can hit the support button above.

    As I noted it's probably not too surprising that this happens, since it's trying to sort on a column which is not longer there. So this is really something that DataTables should take account of (our you can just delete the cookie). I've added this to my to-do list to address this in the next release.

    Allan
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    Thanks for the reply.

    I was just thinking that till the time you release the next datatables library, can I use "fnCookieCallback" function and perform the checking there and update the cookie value if required. I have tried implementing the same but when I use the "fnCookieCallback" function, it throws "sData is undefined" javascript error. I have used a simplest form of "fnCookieCallback" function as shown below. Could you please guide me through how can I use the same.

    "fnCookieCallback": function (sName, oData, sExpires, sPath) {
    /* Customise oData or sName or whatever else here */
    return sName + "="+JSON.stringify(oData)+"; expires=" + sExpires +"; path=" + sPath;
    }

    Sufiyan Ansari
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    fnCookieCallback is only called when the cook ie is saved (the idea being that you can control what is being saved), it is not called when a cookie is read. So sorry - the fix needs to be in DataTables itself.

    Allan
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    Thanks for the quick reply. While testing my datatables program, I also came to know that when we perform search and then moves to the 2nd page. Then perform search again but this time the datatable gets fit in a single page only since there are less than 50 records, then there is no paging option. However, the information section of datatables shows 51 to 100 records and the current page i.e. 1st is not selected by default. If we click on the 1st page from the pagination section then everything works as expected. It seems to be just an initial behavior and doesn't throws any javascript error.

    Note: Page size is 50 records. The above behavior appears only if we set bStateSave property.

    Any thoughts?

    Sufiyan Ansari
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Are you able to reproduce that with any of the DataTables examples? http://datatables.net/examples/data_sources/server_side.html for example (although it will need to be modified for state saving of course)? I've not seen behaviour quite like that.

    Allan
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    I am actually not tried to reproduce the same in any of the DataTables example. However, I am using server-side processing DataTables example as a reference point to generate my DataTables. My DataTables is also a server-side processing DataTables. Below is the code for my table and ajax call and everything else that you might want to look into.

    Table:



    Item ID







    DataTables server-side processing call:
    $('#Items').dataTable({
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "Ajax page url",
    "bDestroy": true,
    "bStateSave": true,
    "bFilter": false,
    "iDisplayLength": 50,
    "aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
    "sPaginationType": "full_numbers",
    "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
    nRow.className = (iRowIndex % 2 == 0) ? "DatatableEvenRow" : "DatatableOddRow";
    iRowIndex += 1;
    return nRow;
    },
    "oLanguage": {
    "sInfo": "Displaying _START_ to _END_ of _TOTAL_ entries",
    "sInfoEmpty": "Displaying 0 to 0 of 0 entries",
    "sEmptyTable": "No matching records found"
    },
    "fnDrawCallback": function() {
    iRowIndex = 1;
    },
    "bSortClasses": false
    });


    Initial JSON string, when the current page is "Page 1":
    {
    "sEcho": 1,
    "iTotalRecords": 54,
    "iTotalDisplayRecords": 54,
    "aaData": [
    ["12116"], ["12117"], ["12118"], ["12119"], ["12120"], ["12121"], ["12122"], ["12123"], ["12124"], ["12126"], ["12127"], ["12128"], ["12129"], ["12130"], ["12131"], ["12132"], ["12133"], ["12134"], ["12135"], ["12136"], ["12137"], ["12138"], ["12139"], ["12140"], ["12141"], ["12142"], ["12143"], ["12144"], ["12145"], ["12146"], ["12147"], ["12148"], ["12149"], ["12150"], ["12151"], ["12152"], ["12161"], ["12162"], ["12163"], ["12165"], ["12166"], ["12167"], ["12168"], ["12169"], ["12170"], ["12171"], ["12172"], ["12173"], ["12174"], ["12175"]
    ]
    }


    JSON string after clicking "Page 2" from pagination section:
    {
    "sEcho": 2,
    "iTotalRecords": 54,
    "iTotalDisplayRecords": 54,
    "aaData": [
    ["12176"], ["12177"], ["12178"], ["12179"]
    ]
    }


    JSON string when we recreate DataTables but this time with only 8 data rows:
    {
    "sEcho": 1,
    "iTotalRecords": 8,
    "iTotalDisplayRecords": 8,
    "aaData": []
    }

    Note: Information section displays message "Displaying 51 to 50 of 8 entries". "Page 1" is not selected by default in the pagination section.


    JSON string after clicking "Page 1" from pagination section:
    {
    "sEcho": 2,
    "iTotalRecords": 8,
    "iTotalDisplayRecords": 8,
    "aaData": [
    ["12161"], ["12162"], ["12163"], ["12165"], ["12166"], ["12167"], ["12168"], ["12169"]
    ]
    }

    Sufiyan Ansari
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi Sufiyan,

    I've just committed a fix for this issue, and it will be released in the 1.7.4 release, which should be out reasonably soon. You can grab the nightly which has this fix in it already if you like: http://datatables.net/download/ .

    Allan
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    Hey Allan,

    Thanks for the update. DataTables rocks.

    However, the second issue that I mentioned in one of comment on 26th Oct (i.e. While testing my datatables program, I also came to know that when we perform search and then moves to the 2nd page. Then perform search again but this time the datatable gets fit in a single page only since there are less than 50 records, then there is no paging option. However, the information section of datatables shows 51 to 100 records and the current page i.e. 1st is not selected by default. If we click on the 1st page from the pagination section then everything works as expected. It seems to be just an initial behavior and doesn't throws any javascript error. Note: Page size is 50 records. The above behavior appears only if we set bStateSave property.). Do you have any update on the same?

    Sufiyan Ansari
  • sumeetbsumeetb Posts: 3Questions: 0Answers: 0
    Hi,

    I have the same issue.
    No. of columns is variable. I have a list of table names in the left pane of the page, and clicking on any table table name will display all records from that table in the right pane.
    Hence no. of columns will change each time. So I tried to avoid aoColumns or aoColumnDefs and go with basic initialization.
    Here is my code:

    [code]datatableTableData = $('#grdTableData').dataTable({
    "iDisplayLength": 5,
    "aaData": JSON.parse(jsonResult)
    });[/code]

    Sumeet B.
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    Hey Allan,

    Do you have any update on my second issue mentioned earlier. Issue was

    "When we perform search and then moves to the 2nd page. Then perform search again but this time the datatable gets fit in a single page only since there are less than 50 records, then there is no paging option. However, the information section of datatables shows 51 to 100 records and the current page i.e. 1st is not selected by default. If we click on the 1st page from the pagination section then everything works as expected. It seems to be just an initial behavior and doesn't throws any javascript error. Note: Page size is 50 records. The above behavior appears only if we set bStateSave property."

    I am still seeing the same behavior in DataTables 1.8.0.

    Sufiyan Ansari
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi Sufiyan,

    Whenever DataTables does a filter it should automatically take the table back to the first row (iDisplayLength = 0), which you can see on the demos. However you note that you have bStateSave:true - so does this happen only when the page is reloaded? How do you set the default filter? Is the filter and / or the data being changed between the refresh? If you could link to an example that would be very helpful.

    Allan
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    Hey Allan,

    Sorry for so late response but I was actually busy with some other high priority projects. Also, it would be very difficult for me to point to the exact url of the error page since its against the companies policy. Hence, I have created a sample application and uploaded to aspspider.com free host site. URL is http://aspspider.info/ansarisufiyan/.

    In the example, you would see a drop down having values "Software Engineer" and "Quality Assurer". In the database, we are having 12 records for "Software Engineer" and 4 records for "Quality Assurer". So when we perform search for "Software Engineer" setting the "iDisplayLength = 10", it shows two pages. First page is having 10 records and second one is having 2 records. Now, when we move to second page and perform the search again for "Quality Assurer", it shows "Displaying 11 to 4 of 4 entries" in the navigation bar and "No matching records found" message in the table since page 1 is not selected by default. So if we click on page 1 then everything starts working as expected. But my expected behavior is it should show the first page itself if there is no second page exists.

    Datatables settings are

    $('#tblEmployee').dataTable({
    "bSort": false,
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "AjaxPage.aspx?designation=" + $('#ddlDesignation').val(),
    "bDestroy": true,
    "bStateSave": true,
    "bFilter": false,
    "iDisplayLength": 10,
    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
    "sPaginationType": "full_numbers",
    "fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
    nRow.className = (iRowIndex % 2 == 0) ? "DatatableEvenRow" : "DatatableOddRow";
    iRowIndex += 1;
    return nRow;
    },
    "oLanguage": {
    "sInfo": "Displaying _START_ to _END_ of _TOTAL_ entries",
    "sInfoEmpty": "Displaying 0 to 0 of 0 entries",
    "sEmptyTable": "No matching records found"
    },
    "fnDrawCallback": function() {
    iRowIndex = 1;
    },
    "bSortClasses": false
    });

    Please help me to resolve this issue.

    Sufiyan Ansari
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    Any help?
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    when the search button is clicked, you should reset iDisplayStart to 0 (or force existing tables to jump to the "first" page) before sending to the server for more data

    [code]
    $('#tblEmployee').dataTable().fnSettings()._iDisplayStart = 0; // this MIGHT work..?
    [/code]

    [code]
    $('#tblEmployee').dataTable().fnPageChange( 'first' ); // this will most definitely work
    [/code]
  • ansarisufiyanansarisufiyan Posts: 13Questions: 0Answers: 0
    Thank you very much fbas. Never tried the first option but second one is working for me. Still need to try to implement on the original project but it is working for the demo that I created. Thanks again.

    Sufiyan Ansari
This discussion has been closed.