Can I Open a StandAlone Editor instance on a record's Foreign Key?
Can I Open a StandAlone Editor instance on a record's Foreign Key?
I have a datatable (students) that is built on a join of 2 sql tables - Students and Parents.
Students.StudentID,
Students.StudentName,
Students.ParentID
Parents.ParentID
Parents.ParentName
I have 2 editor instances - studentsEditor and parentsEditor. The studentsEditor is attached to the students datatable. The parentsEditor is not attached to any datatable.
The regular Edit button opens the studentsEditor no problems.
I want to add a second button to open the parentsEditor as a stand alone Editor and have it operate on the parent record that is defined by the ParentID (by passing in the ParentID value to the editor instance).
For my parentsEditor button I've tried:
buttons: [
{extend: "edit", editor: studentsEditor},
{extend: "remove", editor: studentsEditor},
{extend: "selectedSingle",
text: "Edit Parent",
action: function (e, dt, node, config) {
parentsEditor
.edit(dt.rows({selected: true}).data()[0].Students.ParentID.valueOf(), true);
}
}
but that just opens an empty lightbox.
How do I call the parentsEditor to open as a stand alone Editor from my new button? I tried to follow the stand alone editor example in the php docs but could not see where the data-editor-field
fits in.
Thanks in advance
Brendon
This question has an accepted answers - jump to answer
Answers
Hi Brendon,
With a standalone Editor, if the values aren't available in the HTML (using the Editor
data-*
attributes) you need to use the API to set the values - specificallyval()
,field().val()
or one of the similar setter methods. You'll need to get the data you want to use from the selected row (which you already have from yourrow().data()
call.There is another consideration to make - Editor PHP and .NET libraries can actually edit the fields from the joined table as part of the master form, if that is all you want to do. You just need to make sure you submit the primary key value of the joined table for the row you want to edit - typically in a hidden field (
hidden
)!Allan
Thanks for the reply Allan.
I did consider the solution to 'edit the fields from the joined table as part of the master form', but as the production version has 3 quite 'wide' tables, then the edit form would have too many fields to be practical (I purposely simplified my example so I could explain it clearly).
I was also concerned about the amount of data that was being returned by the initial query (1000 rows, 3 tables of 20 columns). I wanted to present just a few fields from the query in the initial datatable and then drill down for the full details from each table in its own standalone editor.
Your suggestion to use the API to set the values was spot on and I am now able to open a standalone editor for each of my tables as required.
Many thanks.
In case it helps others I have included a snippet from my final code.
I use the ForeignKey_ID of the selected row in an ajax call to retrieve the full record which I load into a standalone instance of an editor. Sweet!
Looks awesome - thanks for sharing your code with us!
Allan