Parent child issue
Parent child issue
Hello,
I am trying to implement the parent child as described in the example and the blog.
I need something simple, without editor buttons or the column count in the parent table.
Here is my code:
(function($){
$(document).ready(function() {
const iso20022_pain001_GrpHdreditor = new DataTable.Editor({
ajax: '../php/table.iso20022_pain001_GrpHdr.php',
table: '#iso20022_pain001_GrpHdr'
});
const iso20022_pain001_GrpHdrtable = $('#iso20022_pain001_GrpHdr').DataTable( {
ajax: 'php/table.iso20022_pain001_GrpHdr.php',
columns: [
{data: "iso20022_pain001_GrpHdr.MsgId_id"},
{data: "iso20022_pain001_GrpHdr.InitgPty_Nm"},
{data: "iso20022_pain001_GrpHdr.NbOfTxs"},
{data: "iso20022_pain001_GrpHdr.CtrlSum"}
],
select: {
style: 'single'
}
} );
const iso20022_pain001_PmtInfeditor = new DataTable.Editor({
ajax: {
url: '../php/table.iso20022_pain001_PmtInf.php',
data: function (d) {
var selected = iso20022_pain001_GrpHdrtable.row({ selected: true });
if (selected.any()) {
d.MsgId = selected.data().id;
}
}
},
table: '#iso20022_pain001_PmtInf'
});
const iso20022_pain001_PmtInftable = $('#iso20022_pain001_PmtInf').DataTable( {
ajax: {
url: '../php/table.iso20022_pain001_PmtInf.php',
type: 'post',
data: function (d) {
var selected = iso20022_pain001_GrpHdrtable.row({ selected: true });
if (selected.any()) {
d.MsgId = selected.data().id;
}
}
},
columns: [
{data: "iso20022_pain001_PmtInf.MsgId"},
{data: "iso20022_pain001_PmtInf.PmtInf_id"},
{data: "iso20022_pain001_PmtInf.NbOfTxs"},
{data: "iso20022_pain001_PmtInf.ReqdExctnDt"}
]
} );
iso20022_pain001_GrpHdrtable.on('select', function (e) {
iso20022_pain001_PmtInftable.ajax.reload();
iso20022_pain001_PmtInfeditor.field('iso20022_pain001_PmtInf.MsgId').def(iso20022_pain001_GrpHdrtable.row({ selected: true }).data().id);
});
iso20022_pain001_GrpHdrtable.on('deselect', function () {
iso20022_pain001_PmtInftable.ajax.reload();
});
iso20022_pain001_PmtInfeditor.on('submitSuccess', function () {
iso20022_pain001_GrpHdrtable.ajax.reload();
});
iso20022_pain001_GrpHdreditor.on('submitSuccess', function () {
iso20022_pain001_PmtInftable.ajax.reload();
});
} );
}(jQuery));
But it is failing:
1. the child table data is displayed even without selecting a row from the parent table
2. when selecting a row from the parent table, I am getting the following message: "Uncaught Error: Unknown field name - iso20022_pain001_PmtInf.MsgId" related to line 58 above.
Any idea of where it is coming from?
In the example, what does "site" refer to in:
data: function (d) {
var selected = siteTable.row({ selected: true });
if (selected.any()) {
d.site = selected.data().id;
}
}
I replaced it by the column name of the child table on which I have a foreign key with the parent one, but I am not sure.
This question has an accepted answers - jump to answer
Answers
Are you saying you aren't using Editor functionality for either table? If you aren't using the Editor then remove that part of the code.
Not sure what you mean by column count.
Is your server script setup to look for the
MsgId
parameter being sent? This is used to filter the child table rows. If there are no parameters sent, which will be the case with no selected rows, then the server script should respond with no rows. Otherwise use theMsgId
parameter to get the ID needed for the child data.Likely due to not defining any Editor fields.
site
is the parameter sent to the server to define the child data to send. You can use any parameter name that makes sense to your solution.Maybe it would be helpful to look at the ajax request and responses from the example you linked to. Open the browser's network inspector and select XHR filter. These steps are based on using Chrome:
users.php
and you will see there are no parameters sent to the server. The JSON response is{"data":[]}
for no rows.users.php
request.site
parameter sent. The server script fetches all the child rows that match the parameter.HTH,
Kevin
Hi Kevin,
Thank you for your prompt answer.
I removed all the Editor-linked code (I will add it again later) and have no more the Unknown field error message.
I was indeed missing the
Editor->where()
method as mentioned here.The problem is that I again don't understand where the site is coming from in $_POST['site'] of the example.
I am getting a Undefined array key error message.
The example is using this:
It is creating the
site
parameter in line 8 withd.site
. In your code you haved.MsgId = selected.data().id;
. So you will want to use$_POST['MsgId']
instead. Also the example has anid
object which isn't displayed in the table. You might need to change the.id
in line 8 to match the unique id you have in the parent data.Kevin
Thank you Kevin, but still getting the same Undefined array key "MsgId" message.
I must be missing something.
Server side for child table, with error on line 10:
Server side for parent table:
The parameter will exist only when a parent row is selected. Take a look at the server script in the Child table section. Specifically this:
Kevin
Thanks Kevin,
it solved the fact that the child row data was populated without selecting a row in the parent table.
But i now get an "Uncaught Error: Unknown field name - iso20022_pain001_PmtInf.MsgId" error message...
Are you using the Editor or not? I thought you removed the Editor code.
This suggests that an Editor field for
iso20022_pain001_PmtInf.MsgId
has not been defined.Kevin
Thanks Kevin. No more error message now, so getting close.
But no query triggered when selecting a row from the parent table, hence no data displayed in the child table.
I will review the code and compare with the example, but I am pretty sure it comes from the site part, which still sounds ambiguous to me.
I would start by making sure the
select
event for the parent is fired. Then use the browser's network inspector to see the ajax request and response. Make sure the request has the correctMsgId
value.My guess is that you don't have a
id
object in your parent row data so this code isn't setting theMsgId
parameter correctlyKevin
Hi Kevin,
What do you mean exactly? I am not sure what part of the code you are refering to.
* The primary key of the parent table is:
iso20022_pain001_GrpHdr.MsgId_id
* The primary key of the child table is:
iso20022_pain001_PmtInf.PmtInf_id
* The child table field on which I have the fk is:
iso20022_pain001_PmtInf.MsgId
So my where statement must be:
iso20022_pain001_PmtInf.MsgId = iso20022_pain001_GrpHdr.MsgId_id
Which I converted into:
I know that there is something wrong between:
and (equivalent of that)
because when I remove the if statement (so the request on the child table is made) I get "Undefined array key "MsgId_id"".
You have defined the columns like this:
You will need use this instead:
Kevin
Now getting: Uncaught TypeError: selected.data().iso20022_pain001_PmtInf is undefined
Did you put the if statement back? The error is likely due to not having any rows selected.
You have this:
But are trying to get the parameter with
$_POST['MsgId_id']
. Use$_POST['MsgId']
so it matches the parameter name you are sending.Kevin
Hi Kevin,
I sent you a private message so you can have a look directly to the code.
Your parent table has this config:
Change this:
To this:
The reference to
iso20022_pain001_PmtInf.MsgId
is not found so the Ajax request silently fails and is not sent.Use the browser's debugger to easily see this error, for example:
Kevin
iso20022_pain001_PmtInf.MsgId_id does not exist in the database, so it fails.
Your names are confusing me :-). You want the ID from the parent table, like this:
Kevin
Then: Uncaught TypeError: selected.data().iso20022_pain001_GrpHdrtable is undefined.
I think I tried every single possible combination.
Are we sure the code mentioned in the blog is correct?
Its because you have this:
But it should be this:
So yes I believe the blog is correct.
Kevin
My apologies.
Undefined array key "MsgId_id" in /home/carrarm/test/SpryMedia/table.iso20022_pain001_PmtInf.php on line 32
We are back to
$_POST['MsgId_id']
I don't understand why that part in the blog
becomes
and why that part in the blog
becomes
it looks like we are complicating
The idea of that code is the unique ID in the parent can be used to fetch the child data.
This is the parent's unique ID to the script in the parameter
site
.Look at the full example code:
The child table is defined to display fields from both the child table and from the parent using
sites.name
. TheleftJoin
is used to fetch thesites.name
. And thewhere
is used to filter the data to those matching the parent ID.If you don't want to display any of the parent data in the child then you should be able to remove the
leftJoin
.Kevin
Hi Kevin,
I started from scratch to make sure I was not missing anything.
Here is the code that works. I hope that I did not make any mistake and it can help.
Which gives in my case:
And as you recommended above:
Part of the problem was as well in the if statement of the child.php file
Thank you very much for your help.