Save and clear state

Save and clear state

ASKempASKemp Posts: 19Questions: 7Answers: 0

I'd like to make use of save state but my issue is that my datatable works over several pages that act as filters which means that if I'm on, say on one page with 100 records and am currently display page 5, and then go to another page which only has 10 records, then the table doesn't initially display any records. I have to click 'previous' or '1' to go to the start of the list.

Is there a way to check the value of current page stored in the saved state data and, if the current calculated number of pages for the given records is less than this page number, then reset it to 1?

This question has an accepted answers - jump to answer

Answers

  • sandysandy Posts: 913Questions: 0Answers: 236

    Hi ASKemp,

    Thanks for your question. As noted in the forum rules, please post a link to a running test case showing the issue so we can offer some help. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Thanks,

    Sandy

  • ASKempASKemp Posts: 19Questions: 7Answers: 0

    Sorry, can't really post a test case nor provide an example due to the complexity.

    I'm effectively providing different data record sets to a datatable. E.g. 100 records, 10 records, 2 records.

    If I'm paging over 10 records at a time, the first set would give me 10 pages, whilst the other two would just give me 1 page.
    If I have save state set to on and I select page 5 on the first record set, navigate away then come back then it correctly goes to page 5 again.

    However, if I change to one of the other record sets then it displays no records initially because, I assume, it's trying to return what's on page 5 when there isn't a page 5. If I click on the previous page then it resets correctly to show my single page of results.

  • sandysandy Posts: 913Questions: 0Answers: 236

    Ok, I've tried creating a test case to replicate what I think you are trying to do. Find it here.

    What I'm doing is
    1. Initialising a DataTable with stateSave set to true
    2. moving to page 5 of the table
    3. Saving the State
    4. Destroying the table so that it reverts to its HTML State
    5. Deleting 50 Rows from the table so that there are 7 remaining
    6. Redrawing the table with stateSave enabled.

    It seems to return you to page 1 and displays the data fine automatically.

    Please modify it to try and show the error if at all possible.

    Thanks,
    Sandy

  • ASKempASKemp Posts: 19Questions: 7Answers: 0
    edited August 2019

    Okay, many thanks, will give it a go.
    Yep, as you say, works fine like that but mine is paged server side so not exactly the same.

    Not to worry, will see if I can work around it - can I read the saved state info, e.g. the page number? If so I can check whether it's 'valid' given the amount of records in the recordset and, if not, reset it to 1.

  • ASKempASKemp Posts: 19Questions: 7Answers: 0

    Okay, think I know what my issue is.

    I'm bringing the total records count back in the same record set (via a stored procedure) as the records themselves (set the value of one column) so as to not make two DB calls.
    Trouble is by doing that when the saved page number sets the offset and returns no records, I'm not getting the count neither which I assume is messing up the page number counting.
    Will have to amend my sproc to return a single, empty row with the total record count if the page selected doesn't return any records.

  • ASKempASKemp Posts: 19Questions: 7Answers: 0

    Sorted that so it works now except...
    I don't like it :smiley: not really what you want when you switch like that given the context so...

    Can I limit what is saved to just the page show length? I.e. save state but purely whatever the user chooses as their show length preference?

    Many thanks :smile:

  • ASKempASKemp Posts: 19Questions: 7Answers: 0

    ...and perhaps col reorder :wink:

  • allanallan Posts: 63,179Questions: 1Answers: 10,410 Site admin
    Answer ✓

    Hi,

    Can I limit what is saved to just the page show length?

    Yes indeed. The stateSaveParams event provides the ability to manipulate the state object to be saved. So you can not only add extra information (which is how ColReorder works), but you can also remove information from it (delete state.columns; for example). DataTables allows any of the default parameters to be optional (i.e. delete what you don't want).

    Allan

  • ASKempASKemp Posts: 19Questions: 7Answers: 0

    Much appreciated Allan, will look through that.

  • ASKempASKemp Posts: 19Questions: 7Answers: 0
    edited August 2019

    .on("stateSaveParams.dt", function (e, settings, data) {data.start = 0;});

    Seems to do the trick (just removing saving the page number) by resetting the start point

This discussion has been closed.