stateLoadParams not fired in first visit

stateLoadParams not fired in first visit

okeletokelet Posts: 10Questions: 3Answers: 0

Hi, I have done some "hacks" in stateLoadParams method to override state using hash parameters (for example http://my/app#search=something). The problem is that the code is not executed in the first visit, as the state doesn't exist yet. Is there any way to modify this behaviour? Using DT 1.10.18.

Answers

  • okeletokelet Posts: 10Questions: 3Answers: 0

    Up?

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    Hi @okelet ,

    One thing you could do is in initComplete, check to see if state was loaded (state.loaded()), and if not, do your default behaviour.

    Hope that helps,

    Cheers,

    Colin

  • okeletokelet Posts: 10Questions: 3Answers: 0

    Thanks @colin ! I have tested that, and I receive a null state the first time I visit the page. Is there any way to generate a "default" state (with the columns, their visibility, search, etc)?

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    Hi @okelet ,

    Yep, that's what I meant - you could test for that null, then do your hash parameters. If that doesn't work for you - you could force a state save in that initComplete with state.save() if the previous state was null, then you could read it back state() and do what you would normally do with the hash parameters.

    Cheers,

    Colin

  • okeletokelet Posts: 10Questions: 3Answers: 0

    Thank you very much for your help @colin , i will try to share my results. Analyzing the order of the events, I get this:

    1. stateLoadParams (only if already visited)
    2. preXhr
    3. xhr
    4. stateSaveParams
    5. drawCallback
    6. initComplete

    My goal is to parse the hash before sending any request, overriding the local state (if any), but I can't get any event before preXhr to modify the ajax request on the first visit. Am I wrong, or missing something? If I put the hash parsing in the initComplete, even forcing the state.save(), the first ajax request will be sent without the hash parameters...

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    Hi @okelet ,

    Gotcha. Here's a couple of things you could try. Please take a look at this example here.

    On first execution, you'll see this ordering:

    preInit
    stateSaveParams
    preXhr
    xhr
    stateSaveParams
    draw
    init
    

    And on second:

    stateLoadParams
    stateLoaded
    preInit
    stateSaveParams
    preXhr
    xhr
    stateSaveParams
    draw
    init
    

    Sooo, you could slip some code into the preInit (probably the best place), or possibly in the stateSaveParams

    Hope that does the trick!

    Cheers,

    Colin

  • okeletokelet Posts: 10Questions: 3Answers: 0

    Thanks @colin , sorry for the late response. I have it almost working! You can see the demo here http://live.datatables.net/suhapeka/1. But I have one problem. Once loaded the page, If i set the hash, for example, to #search=kkkk, that gives no results, the datatable results are updated, but the search input is not updated. The relevant code here is this:

                    // Global search
                    var search = jQuery.url("#search");
                    if (search) {
                        theTable.search(search);
                    }
                    else {
                        theTable.search("");
                    }
    
                    updateHash = false;
                    theTable.draw();
                    updateHash = true;
    

    Any idea why results are updated, but input doesn't?

  • colincolin Posts: 15,118Questions: 1Answers: 2,583
    edited August 2018

    Hi @okelet ,

    Looks like you've found a bug there! It seems to have been introduced since the last release - if you use the 1.10.19 version, everything works there as expected (see here). I'll create a jira for it and we'll report back here when it's been addressed.

    Cheers,

    Colin

  • okeletokelet Posts: 10Questions: 3Answers: 0

    Thanks @collin , I am confused, you said it's a bug of 1.10.19, but tried also with 1.10.18 (the previous, here http://live.datatables.net/yiziyuco/1) and also doesn't work (the link you provided is a clean demo).

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    Hi @okelet ,

    Mmm I'm confusing myself now too - I just tried it again with the nightly builds and it's working as expected. I'll give it another blast in the morning and report back,

    Cheers,

    Colin

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    Hi @okelet ,

    Yep, whatever I saw yesterday, I can no longer reproduce - sorry about the confusion. search() should put that search string into the input element, and it is for me now - must've been gremlins in my computer.

    Would you be able to modify your example to show that problem? I'm unable to reproduce it,

    Cheers,

    Colin

This discussion has been closed.