stateLoadParams not fired in first visit
stateLoadParams not fired in first visit
okelet
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.
This discussion has been closed.
Answers
Up?
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
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)?
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 thatinitComplete
withstate.save()
if the previous state wasnull
, then you could read it backstate()
and do what you would normally do with the hash parameters.Cheers,
Colin
Thank you very much for your help @colin , i will try to share my results. Analyzing the order of the events, I get this:
stateLoadParams
(only if already visited)preXhr
xhr
stateSaveParams
drawCallback
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 thestate.save()
, the first ajax request will be sent without the hash parameters...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:
And on second:
Sooo, you could slip some code into the
preInit
(probably the best place), or possibly in thestateSaveParams
Hope that does the trick!
Cheers,
Colin
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:
Any idea why results are updated, but input doesn't?
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
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).
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
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