Editor Inline Create
Editor Inline Create
william.moore
Posts: 1Questions: 1Answers: 0
Is there a way to extend Editor to allow for in-line create?
This discussion has been closed.
Answers
This is not directly supported but this thread should point you in the right direction.
https://datatables.net/forums/discussion/32858
Kevin
Hi @william.moore ,
This thread might also help as well - the example shows how to create an empty row then inline edit immediately.
Cheers,
Colin
Hi All,
I am facing this challenge as well. I invoke a REST api with certain mandatory fields. So creating (ie, POST'ing) an empty row and then immediately editing is not an option. Hence, I tried to hack around by adding an empty row just on the client side and then submitting it via Editor's edit() functionality. But the challenge is how to handle the actual row edit where I need to send a 'PATCH' http request? I explored dynamically deciding the http verb to send based on a condition, like:
That doesn't seem work. Gives out a jQuery error.
Kindly advise how to set the http verb dynamically.
Regards,
Harsha
I don't believe jQuery allows the
type
to be a function. If you need to be able to change the verb for edit dynamically you'd need to useajax
as a function - e.g.:Allan
Hi @allan ,
Thanks for responding. I came across this thread which states we can actually change 'action' parameter. That I think would be a suitable solution for my use case. So while still using inline edit, I am trying to change 'action' to 'create' like:
Kindly advise if this the right way. Action seems to be stuck at 'edit'.
Regards,
Harsha
Hi @hnhegde ,
The thread you refer to was discussing
preSubmit
, notinitSubmit
. For both, the return is a boolean withfalse
stating that the submission should be terminated.Cheers,
Colin
Hi @colin
Indeed, the thread is discussing
preSubmit
. I was trying to see if its possible to modifyaction
at the stage of reading the form. Kindly advise,Thanks,
Harsha
I think it is more or less supported to be honest - in conjunction with the new button though.
I have a data table with only two buttons "create" and "remove". The Editing is done inline only.
This is the code for the two buttons. The create button creates and submits an empty row.
This code positions the cursor in the first field of the newly created empty row to start editing. You need to assign the respective class (here .event) to the columns in the HTML to indicate which columns to edit.
If you don't like users leaving "garbage" in the table by creating new rows but not editing or deleting them you can do a nightly clean up batch run and delete those rows.
Oops, I see the question had been answered already by Colin with a good example... But another example might be helpful as well ...
Hi @allan ,
Wrt your response on October 18th, using $.ajax() function almost works. But, how to specify custom
ajax.dataSrc
? Kindly advise.Thanks,
Harsha
Hi Harsha,
You'd use an anonymous function for the
success
handler:e.g. use the function to resolve (or map) the data into the format that DataTables is expecting and then pass that into the
success
callback.Allan
Noted. Thanks @allan
Regards,
Harsha
Hi @colin , @allan
As per
initSubmit
's doc page:Can it be used to modify
action
? If yes, can you kindly help me understand how to achieve that?Regards,
Harsha
I am very sure you can't modify the "action" parameter. @allan, @colin can you confirm pls.
But you can return "false" from the "initSubmit" event and hence avoid anything to be sent. At the same time you should be able to submit an Editor "create" form with the values edited like this:
Not sure whether you need to create another Editor instance "yourCreateEditor" on the client side to get this working. Probably yes. But it is not a problem to have multiple instances for the same data table.
You can as of the 1.9.1 release with the
actionName
option. On the PHP side use->actionName('...')
to tell it the new action name.Allan
But that is just the name of the parameter, right. With actionName you can rename it to “widget“ for example.
But can you change its content e.g. from “edit“ to “create“?
But I might as well have misunderstood the entire discussion in this thread anyway. Never mind ...
Hi @rf1234 , @allan ,
Thanks for your responses. I am actually looking to change the CRUD operation by intercepting the action in initSubmit. Say, if a certain condition is true, I want to 'create' even if the user clicks Edit. Is it possible via initSubmit?
Regards,
Harsha
Well, that's what my example is about, Harsha.
It effectively does this by replacing the “edit“ action with “create“ ...
Noted, thanks @rf1234 !
Regards,
Harsha
Ah - for that yes you could change it, but you'd need to make sure your server-side script you are using will also match whatever you set it to. Our prebuilt scripts on;y work with the constants
create
,edit
, etc.Allan
ok, but does Editor for PHP for example support this? What exactly would you need to do to make this work on the client and the server side?
I could only think of the work around that I posted above in the "initSubmit" example which kind of plays a trick on Editor by copying the contents of the "edit" Editor instance into a new "create" Editor instance which is submitted while the "edit" submission gets canceled right after copying.
Let me give it another try! You can use "preSubmit" which is triggered right after "initSubmit". "preSubmit" allows you to manipulate the data that will be sent to the server which consist of the data read from the Editor form plus other stuff.
It looks like this (only example I found in my code):
You could try this. Should work, but I don't know whether the Editor server libraries will be able to process it correctly. According to @allan 's comment above it should work though.
According to the docs the 2nd parameter "d" is this:
Just tested this with my forex data table:
It does not work: Even though the "action" in the "data object that will be submitted to the server" is manipulated correctly Editor PHP still performs "edit" on the server side. @allan: isn't that a bug? Or are you referring to the data object within the data object, i.e. data.data or d.data in my code?
So forget about this suggestion please. You obviously can't do this but you can still do the "work around" I described above.
Great stuff! You could just watch me making a complete fool of myself.
Ok, this should be the correct code:
AND IT WORKS LIKE A CHARM. @allan: no bug! You can't fix the one in my mind, I guess ...
I will post more when I develop a button that is for "edit / create" as opposed to the existing "edit" and "create" buttons.
To whom it may concern:
Thanks for the updates. Good to hear it is working .
Allan
For the sake of completeness this is the entire code - and it works now! really ...
I had to fiddle a little with the Editor popup header content and the button label when using the new button. Since i18n for Editor is only triggered upon initialization I had to do this using jQuery. But no problem. On this occasion I discovered the "opened" event; couldn't get this working just with "on open".
Wow! Thanks so much @rf1234 ! Appreciate it!
Regards,
Harsha
You are welcome! And thank you as well. You had the right idea with changing the content of the “action“ parameter.
Already went live with something much bigger using the new button. Only took 15 minutes to make the modifications required once I had the forex table running.
@allan, such a “copy & new“ button wouldn't it make sense to have this available as a standard Editor button?
This example shows what I've called a "duplicate" button, which is basically the same thing.
Allan
Is there an easy way to remove the ID in the example above so that the duplicate can be saved in the DB?
Dirk