Nested Editing Example
Nested Editing Example
https://editor.datatables.net/examples/datatables/nested.html
I guess the server script shown is this: '../php/joinNested.php'
I would also like to see this script: '../php/sitesNested.php'
Where can I see it?
This question has accepted answers - jump to:
Answers
I believe these files are in the Editor download. Look here:
Editor-PHP-x > Controllers
Kevin
Thanks Kevin. I have just realized that I am using the "datatable" field type quite differently than suggested in the docs. In the example you are working on a child table "users" and assign a "site" from the parent table which is saved as a foreign key in the child table ("users.site").
That is opposite of what I am doing:
I have a parent table "sub" and I want to define cashflows for "sub" which are saved in a child table "sub_exec_cashflow". I got it working for "edit" when I can pass the parent id to the child table. I wonder whether I could get it working for "create" as well. Question is: How can I implicitly pass the required id to the child table on "create" of the parent table? (actually it's two fields "ctr_id" and "is_sub_provider")
Here is my Editor code:
Hi Roland,
Interestingly, I use the terminology that "users" is the parent table and "sites" is the child table, since the user's table is the one that is being displayed as the main table and the sites table as the child.
As a result, I might be misunderstanding the question, apologies if so, but, my form of the terminology:
With a single value, that is what the
datatable
field type is doing is it not? You select a value from the list (including any newly created ones) and that is what gets submitted to the host (what I call the parent).Let's take this example.
If I click the New button, and then in the nested table select New again, I can create a new row, which has an id / value. That becomes the value of the field for the parent / host table.
Perhaps the issue is that you are looking for two values (perhaps a compound key?). The
datatable
field type does not support having two values for different fields - it is a single field input only.Allan
Hi Allan, thanks!
Let me clarify. For me "parent - child" is derived from Entity-Relationship-Modeling exclusively. In that regard "users" is the child because it holds the id from "sites" as foreign key. Also: One site can have many users, but one user can only have one site. That is traditional "parent - child". Just like in the 1960's when IBM published its hierarchical database IMS DB. This database could only do 1:N and not N:M like modern relational databases that use a link table for that. (Reading the database you had commands like "get next within parent" etc. if I recall it correctly. Was a little bit better than indexed files but not much.)
What I want to do is the following (in the words of your example):
Creating a new "site" I want to create new "users" working on that site in one go! What does that mean? When inserting the child table rows I need to know the parent site_id (which you call "users_site" I guess). Right now I don't have it and I have no way to pass it unless I created the site before hand.
This would need to be done by Editor:
- Editor would need to insert the parent table row first ("sites"),
- get the "lastInsertId" and
- use that "lastInsertId" as the foreign key to insert the n child rows (new users).
What would I need to "tell" Editor to get this done?
- names of the source and target variables (source: new sited.id, target: new users.sites_id (= foreign key))
- Those names should be passed in an array: hence you have more than just one foreign key field if you will. (that would also cover my use case).
It would be awesome if this could be achieved!
Forgot to mention: I am already doing all of this! But with a trick. The trick is I split the creation process of a new "site" into several steps. After the first step the "site" already exists in the database - even though it is still incomplete.
This trick allows me to use field type "datatable" in one of the following steps: I can pass the "lastInserId" from "sites" when inserting the new "users" working on the "site". You can see that in my code above and in this picture. "d.ctr_id" is nothing but the "lastInsertId".
For the sake of completeness this is the code of the Editor for field type "datatable":
And another one
To make my suggestion work when creating the parent table entry you would need a joint submission of the new parent table record and all of the child records as defined in field type "datatable".
It seems to me that this joint submission simply has not been part of the design: Whenever you create or edit a child record with field type "datatable" it is immediately submitted to the server and hence this cannot work before the parent table entry exists! Hmmmm ...
So what is really needed is a new field type "datatableChildren" that buffers all of the child rows on the client side and submits them jointly with the parent table record! Then the parent record is inserted first and the child rows are inserted using "lastInsertId" as foreign key.
For editing it could be left as is because in that case the foreign key is already known.
So that would be my requirement
If the join reference is in the nested table, then yes, that is absolutely correct.
Taking our example when you create a new
site
, you don't need to know the user id (the user table links to the site).Where that doesn't work is if you list the sites with a one-to-many join to the user's table. Correct me if I'm wrong please, but that is what you are looking for here isn't it?
Clever workaround to address this! I didn't have any plans to implement something for this in Editor, but I absolutely can see how it would be useful. I'll consider how we can add something like this in future. Many thanks for the suggestion!
Allan
Thanks a lot Allan! You are doing a great job! The richness of your software can be overwhelming sometimes!
Yes, and that is my standard use case. You have a parent and the nested table entries contain the join reference (i.e. the foreign key from the parent table). I know that your examples don't have my use case. No worries - we'll get that resolved.
I think I already have a solution for my parent - child editing using field type "datatable". I only need this solution when I want to "create" the parent table record and the N child records (field type "datatable", nested editing) in one go.
This is how it could work:
- fill out the fields for the parent record in Editor
- do local table editing of the child tables (when editing field type "datatable", nested editing)
- "on SubmitSuccess" of the parent record: Submit the buffered child table records to the server either using Editor or by simply inserting them "manually" myself with a proprietary ajax call.
In case the local child tables get destroyed on "close" of the main editor form I would need to capture them at that time. Do they get destroyed at that time?
There is only one issue: How can I turn local table Editing on and off dynamically?
I tried the code below, but it didn't work. Yes, no server update was made, but the record didn't show up in the table either. I needed to completely remove the "ajax" option to make it work, but that can't be done dynamically, can it? Is there a trick or work around?
@allan @colin
Can you answer those questions from my post above, please?
Oops - sorry - I missed those!
No - the child DataTable is initialised once and gets reused, potentially getting its options changes between uses. It is very similar to the
select
field type in this regard. So if you need the DataTable on close, you can useeditor.field('myDT').dt()
in theclose
event handler or at any other point to get an API instance for it.There isn't an API to do that at the moment I'm afraid. If you fancy a little hack to make it would you could do
editor.s.ajax = null
to turn it off, andeditor.ajax({ ... })
to set an Ajax configuration. I could look at makingnull
allowable inajax()
to disable Ajax - I'm a little concerned about the async actions and doing that just too early or too late though.Perhaps try the hack and see how that goes for you and I could put that in if it works - I think you'd be the only one using that specific feature for the time being
Allan
Thanks a lot, Allan!
That is really surprising. For me this is THE standard use case in any business I can think of: A project (parent) with many activities (children).
During the 20+ years that I worked as a business executive my focus was on getting things (= projects) done (= many activities)!