Datatables Nested tables getting rows for child table based on parent table
Datatables Nested tables getting rows for child table based on parent table
I have set up nested Datatables, with a parent table MAINtable
and child table adjlinesTable
. Everything almost works, except I am drawing all lines from the adjlinesTable
to every row in the MAINtable
. For each row in the MAINtable
, I only want the adjlinesTable
to contain the records where MAINtable(id) = adjlinesTable(adjustmentid)
. I am very new to javascript, and I have been running loops trying different methods for 3 days, and just can't beat this!
I know I need to define the variable for the MAINtable
, and then either send it to my fetch_adjlines.php file, where I can use it to limit the draw, or perhaps I can do something in the createChild
section to only generate the lines where the adjustmentid
is equal to the MAINrowID?
Here is my script:
$(document).ready(function(){
function createChild ( row ){
var table = $('<table class="display" width="100%"/>');
row.child( table ).show();
var adjlinesTable = table.DataTable( {
ajax: "php_pages/fetch_adjlines.php",
pageLength: 5,
columns: [
{ data: "id" },
{ data: "adjustmentid" },
{ data: "product" },
{ data: "quantity" }
],
order: [[1, 'asc']],
} );
}
var table = $('#MAINtable').DataTable( {
ajax: "php_pages/fetch_adjustments.php",
pageLength: 5,
columns: [
{
className: 'details-control',
orderable: false,
data: null,
defaultContent: ''
},
{ data: "id" },
{ data: "date" },
{ data: "reason" }
],
order: [[1, 'asc']],
} );
$('#MAINtable tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = table.row( tr );
var rowData = row.data();
if ( row.child.isShown() ) {
row.child.hide();
tr.removeClass('shown');
$('#' + rowData.id.replace(' ', '-')).DataTable().destroy();
}
else {
createChild(row);
tr.addClass('shown');
}
} );
} );
Answers
We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
Ok - I think I did this as best I can. Here is a test case. Obviously, in this case I am drawing from the same file for both. In reality, I have two different tables and two different php pages per the code above. However, this does replicate the issue in that everyone shows up in all offices.
live.datatables.net/muyuguji/1/edit
Looks like you are doing something similar as this blog but maybe without the Editor. Whether using the Editor with this or not doesn't matter. Take a look at this section of the child Datatable configuration for how to use the
ajax.data
option to send theid
to the server.Your server script will need to get the id parameter to limit the results to the desired data.
Kevin
OK, for the subtable, I need to make sure the only records pulled have the adjustmentid = rowData.id (assuming this is the id from the main row). So I revised my main script to the following:
This totally broke it ... presumably I need to do something in fetch_adjustments.php?
This is my code there:
What happens?
Yes, you will need to do something in your
fetch_adjustments.php
script to get the parameters sent and use it for the query. I don't use PHP so won't be much help there.Kevin
Maybe you are getting an error with this
d.adjustmentid = rowData.id;
. I don't think you have a variablerowData
in your createChild function. Yu probably just want to userow
for the row parameter in the function.Kevin
Slowly, slowly, step by step... You are correct.
d.adjustmentid = rowData.id
I get an error, rowData is not defined.I replaced it with
d.adjustmentid = row.id
What I am getting over to my php file now is row_1 - which is the DT_RowId, not my id from the column. Any clue on how to send the column value?
Use
console.log( row );
inside thecreateChild()
function to see the structure ofrow
. Theid
property should be{ data: "id" },
from your main table. Then use the browser's network inspector tool to verify the parameters sent by looking at the bottom of theheader
tab (at least when using Chrome).Do both show the correct id?
Then in your
fetch_adjlines.php
script you will need some code to get the parameters. Have you done that? Sorry I'm not much help with PHP.Kevin
Kevin, you are wonderful! While you didn't answer it, you definitively pointed me in the right direction to make it work. I had to throw out the base php file from Datatables and do a bunch of rewriting to make my code generate the right formats to be imported correctly, but doing this I was able to define the DT_RowId as my table id. The tables still work, as this is a unique field. And this field then gets passed to my child table and draws the correct records.
After over a week beating my head against a wall on this, it works!!! I just need to pretty it up, and then move on to my next set of problems ...