Can I do an immediate reload after a Create?
Can I do an immediate reload after a Create?
Link to test case: See Editor's most basic example#.
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
If I am proposing to add "Gloria Little" to the database, I might first search for that name in case I might be adding a duplicate.
If that name is present, but my new one is a different "Gloria Little", I will go ahead with the Create.
However my table is still showing only the original record, not my new one. (Let's assume I have only created the name, and the table only shows the name after a search.)
It is now very easy to assume that I am looking at my new "Gloria Little" and begin editing it.
How can I get around this? My maintenance section has its tables sorted by auto-increment id, so a reload will always have the newest at the top.
Answers
Hey, you should just be able to perform a
draw()
in thepostCreate
. Did that not work, or something not tried?Colin
Hi Colin; thanks for the speedy reply.
I think I may have gone over some of this ground before, but my memory is so bad these days that I'm likely to be missing something.
I found this in an external script which is included in all my major table scripts:
which doesn't seem to do anything. oEditor and oTable are globals, and other small funcs in the same file are working.
My first thought was to expand my postCreate PHP function, but I don't see how to apply a draw() call in PHP.
I'll keep looking. It's not number one priority right now.
Thinking about it more, you shouldn't even the need to draw, since the table is updated automatically. For example, if you search for "Ashton" in one of the Editor examples, and add a new record with the name "Ashton Cox", then you would see that appear in the table in the search results. The new record would also flash so the user would be aware that's the new one.
If I'm missing something, let me know, but I'm not clear what the steps are to get into the situation you're seeing.
Colin
Not sure what you are trying to accomplish with the code snippet but you probably want this if you are expecting column 1 to be sorted descending:
See the
order()
docs.I might not understand the question but if you are creating a new record it should automatically show in the table. Using the Basic example I can insert a new record for Gloria without doing anything special.
The server is expected to respond with the new record as shown in the Client/server data docs. Use the browser's network inspector to see what the response is.
Ignore this if I'm not understanding the question :-)
Kevin
Hi Kevin, thanks for joining in!
I need to simplify this, as my own code is looking like an old can of worms scenario the further I go into it.
Colin, looking at the Editor's basic example, it doesn't do a draw after a create - unless I misunderstand the "draw" function. What I want is for all the data to be re-shown, not just the new record (along with any prior search result). Is that what you mean by "draw"?
Kevin, I think you understand the question ok, but see my answer to Colin.
Are you wanting to clear the global search? In your event handler call
search()
with an empty string, for example:Kevin
Hey tangerine!
I think what Kevin suggests looks ok. I had similar issues a while ago because I had overlooked the consequences of state saving in that context.
I don't know whether you use state saving at all. If you do you will have to deal with state saving also saving stuff like your last search, row selection and even custom ordering. In order to avoid any issues I make sure that these things aren't loaded from the saved state for all of my data tables.
No, Kevin, that's not the issue.
Hi Roland. Welcome to the party!
No, statesaving isn't involved.
Right now I just want to establish that my expectations are valid regarding "draw()".
See my last comment to Colin.
ok, tangerine, I sometimes need to manually reload parent data tables if I change something in a child data table because that doesn't work automatically. You can use that logic too if you have an ajax sourced data table that doesn't reload newly created or changed records.
The event is for "editor" not "table" ... sorry.
draw()
does not default the search and sorting state of the table. It does jump to the first page unless you pass infalse
or"page"
as the parameter. The code snippet I provided above will clear the default search value then thedraw()
is used to update the table display with the search term.Kevin
I'm trying to simplify this, as we may be getting some crossed wires here.
I need my question to Colin resolved:
Kevin, I take your point about draw(), but I'm still not clear as to the expected result.
Should all the table's data be shown, or only what was displayed when the create was made plus the new record?
Datatables doesn't affect the search filters (global or column) when
draw()
is called. I built an example to demonstrate:https://live.datatables.net/honibabu/1/edit
Perform a search and click the
Call draw() Only
button. The table display doesn't change and the search is not reset. Clicking theShow All Data
executestable.search('').draw();
which clears the search string and all the data is displayed.Creating a new row does call
draw()
automatically. Go to the Basic Example and paste the following code into the console:Perform a search and create a new record and you will see the
draw
event is called. It is not expected that Datatables will reset the search and display all the rows in the table contrary to the applied search filters.Does this help?
Kevin
Yes, it does help, Kevin - I appreciate you taking the time to set that up.
So my expectations were wrong about create/draw. Only the currently applied search (if any) and the new record will be displayed.
Now I need to get back to my own particular problem. I have this postCreate function
which is correctly taking care of ordering. However, when I'm displaying a record selected by a search, a "create" does not show the new record and the display continues to show the result of the search, as if "draw" did nothing. (The new record does get created.)
When there is no prior selection, everything is fine. I have numerous "creates" across multiple tables, and everywhere the result is the same. I have made no changes to DT Editor's code.
I can grant you admin access if it would help.
Thanks again.
It seems the problem is the way you are trying to use the
order
API. The docs state that it expects an array with each element being an array containing the column index and sort direction. The docs don't indicate it works the way you have implemented it. I did try it and it does work. Maybe its a legacy form of the API. But it doesn't seem to return a usable API for the chainedsearch()
.I created an example using your code (commented out) and the documented way to use the API:
https://live.datatables.net/guwafemu/400/edit
Using this code works and shows the full table:
Comment out the above and uncomment the other code and the
search()
doesn't work:Kevin
Great - I can see that works in your example! BUT it doesn't work on my own site!
In fact, I just discovered that if a search found nothing, then "No data available" still remains displayed on its own after the "Create".
I would start digging deeper, but I don't know where to start. I suppose I'll be back...
Are you saying that
.search( "" )
, with an empty string, results in no records and "No data available" is displayed? Or are you saying that you performed a search which results in no records before creating a new record?Try sorting anything other than column 0 or 1 to see if the
.order( [[idx, 'desc']] )
works. Possibly thepostCreate
event isn't. firing or there is a Javascript error occuring.Kevin