Parent Child question
Parent Child question
JanNL_FR
Posts: 47Questions: 15Answers: 2
Hi,
https://editor.datatables.net/examples/advanced/parentChild.html
In the Parent / Child example there is mentioned '.id'
Is this related to the sites.id field or to the users.id field?
Can I use another field in the table or has it to be the primary 'id' field?
var usersTable = $('#users').DataTable({
ajax: {
url: '../php/users.php',
type: 'post',
data: function (d) {
var selected = siteTable.row({ selected: true });
if (selected.any()) {
d.site = selected.data().id; /* this 'id' field */
var targetsTable = $('#targets').DataTable({
ajax: {
url: '../php/targets.php',
type: 'post',
data: function (d) {
var selected = portfolioTable.row({ selected: true });
if (selected.any()) {
d.company_t_id = selected.data().company_p_id; /* Can I replace it with this */
}
}
},
Jan
This question has accepted answers - jump to:
Answers
You may want to refer to this blog which describes the solution in the example you linked.
This is getting the parent's selected row
id
object. In the exampleid
not displayed in the table but is part of the parent's row data. You can see this using the browser's network inspector tool. It then assigns theid
value to thesite
parameter sent to the server.Yes, you can use any valid parent row object. Your server script will need to parse the sent POST parameters to get
company_t_id
parameter. This value is used as part of the where clause to fetch the data for the child table data.Kevin
Thanks Kevin.
When I don't comment out these lines, I get no data in the Child Table.
Jan
Use the browser's network inspector to see what is sent in the
company_t_id
parameter. Is it a number? Maybe this part of the if statement is failing! is_numeric($_POST['company_t_id']
.Or it could be the where clause isn't working. Its hard to say without seeing the data and your solution in action.
I would do a bit of debugging to see what is being sent and how its being handled in the PHP script.
If you still need help then start by using the browser's Network Inspector to collect the POST parameters sent to the server. Post it here.
Kevin
The Parent table works fine.
For the Child table I get
{data: []}
data: []
I understand that is what you are getting.
Have you performed any of the debugging steps I suggested?
What is being sent? Is it numeric? Does the value match the
company_t_id
field for the where clause to return rows? Code snippets don't give us this infomration.Ata a minimum use the browser's network inspector tool to get the POST parameters sent to the server and post here so we can take a look.
Kevin
Hi Kevin,
I haven't spent much time exploring JS and DataTables (I like it), nor debugging. I can find the BrowserTools but I don't really know where I can find, what you are asking for.
Maybe this helps?
campcare.eu/2/PortfolioTargets/pc.php
Jan
A link always provides the answers - thank you for that.
You have:
in the
ajax.data
function fortargetsTable
. But there is noid
property in the row.There is
DT_RowId
, but you might find it easier if you just added anid
parameter to the object. Or you could useDT_RowId
and remove therow_
prefix.Allan
company_p_id and company_t_id are the matching fields.
can i use this or what is a good way to do?
With this code there are still no records.
Jan
The problem is that you have nested objects for your parent table, for example:
But you are not accessing the nested data with this:
Try this:
Kevin
Thank you, that's it !
Jan