parent child datatables
parent child datatables
SalmanS
Posts: 102Questions: 12Answers: 0
Hi,
Its been a while, I am back again. trying to get parent and child datables on click of parent row.
here is my code
`<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.18/datatables.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>DataTables - Parent and Child</title>
</head>
<body>
<div class="container">
<table id="example" class="display nowrap" width="100%">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>1</td>
<td>xxx</td>
<td>uyy</td>
<td>dddd</td>
</tr>
<tr>
<td>2</td>
<td>dfff</td>
<td>ddd</td>
<td>dddd</td>
</tr>
</tbody>
</table>
</div>
<div>
<table id="child" class="LogDetails">
<thead>
<tr>
<th>Id</th>
<th>Loged Date</th>
<th>Loged Out Time</th>
</tr>
</thead>
</table>
</div>
</body>
</html>`
my javascript
$(document).ready(function() {
var table = $('#example').DataTable( {
"columns": [
{ "orderable": true,
"data": null,
"defaultContent": ''
},
{ "data": "A" },
{ "data": "B" },
{ "data": "C" },
{ "data": "D" }
],
"order": [[1, 'asc']]
} );
// Add event listener for opening and closing details
$('#example tbody').on( 'click', 'button', function () {
var data = table.row( $(this).parents('tr') ).data();
editMember(data[0]);
} );
} );
Anyone around to help, so i can use ssp class to retrieve data from my sql database please?
OR
Just from the text file the child data?_
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Sorry, the add event listner for opening and closing details
Have you looked through this tech note?
https://datatables.net/blog/2016-03-25
If you are not using editor you can ignore those config sections.
Kevin
Yes Kevin,
I can see that its available in editor version, is there a way i can achieve using datatables please?
$('#example').on('click', 'tr', function () {
Yes, you can use that example to build just the Datatables portion.
The example uses the Select Extension to select the rows. The child table will use an ajax request similar to this from the example:
It sends the parent row ID to the server using the ajax data option. The server script will receive the ID parameter and run a query to fetch the desired rows for the child and return them to the client.
Like I said just ignore the Editor config pieces.
Kevin
Thanks Kevin, Please see my code? two issue now.
listusers.php
`
<?php
/* * DataTables example server-side processing script.
*
* Please note that this script is intentionally extremely simply to show how
* server-side processing can be implemented, and probably shouldn't be used as
* the basis for a large complex system. It is suitable for simple use cases as
* for learning.
*
* See http://datatables.net/usage/server-side for full details on the server-
* side processing requirements of DataTables.
*
* @license MIT - http://datatables.net/license_mit
*/
Index.php
dbconfig.php //no erros when run in the browser - that means the connection is fine.
Hi Colin/Kevin,
Can you guys see what I am doing wrong in my code above.
Hi Kevin,
I tried using the suggestion earlier but datatables just saying processing message. here is my modified code:
Do you get any alert error messages?
Do you get any browsers console errors?
Have you looked at the browser's developer tools and used the network tab to see what the XHR request and response is?
Have you looked at your server script for errors?
Kevin
No errors,everything works fine. but the moment you try to add the suggested code as below. the datatables stopped working, processing message.
If you remove the above code all works fine.
Not sure what I am doing wrong?
You don't get any console errors when you add the above code?
That code is for the "child" table as showing here:
https://datatables.net/blog/2016-03-25#Child-table
Not sure if the '#example' table is the parent or child. If have
var selected = example.row( { selected: true } );
. Where is theexample
variable defined.Kevin
This is the master table: parent table: #example is the ID: so first i am trying to gather the value of the row column... in this case name [0]. once I have the value than...display or build the child table, based on the value of column name[0].
Where is the example variable defined.---- example is the parent table.. there is no child table in my code yet. first want to alert the value .onclick or .click of the respective row.
In the
ajax
is not the place for that code. It belongs in the child table to send an ajax request just for that id. You can use this example to see how the row selection is working.Kevin
I am terribly confused.
I got a master/parent table named #example and child table #users.
which code is going where.? do i need any code in my master or parent table...
or do i need code in both tables?..
is there any live example of what codes goes where.
parent table #example (Site)
child table #users (Users - List of users on each site)
more simpler another example:
Customers:
CustomerID CustomerName
———- —————
1 ABC Foundation
2 The XLZ Company
Products:
CustomerID Product Sales
———- ———- —–
1 Product 1 $200
1 Product 2 $437
1 Product 3 $523
2 Product 1 $240
2 Product 2 $892
2 Product 3 $395
Since you are not using Editor there isn't much you need to do with the master/parent table. It needs to have the select extension and set for single row selection.
The child table will use the
ajax.data
option to send the ID to the server script for the child table.I don't have a way to put together a good example as I don't have a way to simulate a server script to retrieve the child data. I did put together this example though:
http://live.datatables.net/biliniyu/1/edit
It uses the same ajax url for both the parent and child tables. Likely yours will be different. Also it uses the
ajax.dataSrc
to filter the returned data from the server to display only the child row matching the name of the row selected in the parent. This would not be used in a normal parent/child solution. It is expected that the child script returns only the data expected.Kevin
I don't have words to thank you, the concept is so clear with that example. There are only two true champions in this world one is Kevin and another is Allan. Love you both.
Glad that example helped.
Kevin
Last one week was so stressful for this issue, I was almost giving up on DATATables and thought of going back to PHP pagination and set our own rules. but this example means a lot to me.
Hi Kevin, What is the format of objects.txt file please? Comma delimited or with header?
here is my objects.txt file:
"name";"position";"office";"extn";"start_date"; "salary"
"Mike"; "Director"; "UK"; "ext. 123"; "12/12/2018"; "5000"
"DanMike"; "Director"; "UK"; "ext. 123"; "12/12/2018"; "5000"
"Tan"; "Director"; "UK"; "ext. 123"; "12/12/2018"; "5000"
"Blonde"; "Champion"; "UK; ext."; "123"; "12/12/2018"; "5000"
"Beauty"; "Director"; "UK"; "ext. 123"; "12/12/2018"; "5000"
Please clarify?
That is something supplied by Datatables which is why I didn't have a way to have a proper child script.
https://datatables.net/manual/tech-notes/9
You can see the format by looking at this example and clicking the ajax tab:
https://datatables.net/examples/ajax/objects.html
Kevin
many thanks got it. all working on my local pc.
But if you have multiple corresponding records in your child, it is just showing one record in the child table, though there are more than corresponding records in my child table.
I separated
Parent.txt (unique Records)
and child.txt (multiple records)
The child.txt contains multiple records for the same person "Tiger Nixon" the corresponding records for selected parent. see below:
"data": [
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
How are you retrieving the data from the child data?
That will control the number of rows shown in the child table.
Kevin
I separated
Parent.txt (unique Records)
and child.txt (multiple records)
The child.txt contains multiple records for the same person "Tiger Nixon" the corresponding records for selected parent. and changed the corresponding ajax call
ie.,
You would need to build an array and return the array instead of the individual row. See if these changes work:
Didn't try it so hope it works.
Kevin
It worked with the changes below:? However, when you do search in parent table - the child table still showing corresponding records for the previously selected parent record.
If you want to clear the child table on searching then you will probably need to use the
search
event. In the event userows().deselect()
to deselect all rows followed bychildTable.ajax.reload()
.Kevin
tried the following - no console error and still the child table showing old records.
class of my parent table is '.display'
Try this:
Kevin
perfecto...the last part is when you select show entries from the drop down 10,25 etc. its showing error message in the console.
Parent_Child.html:1 [Violation] Added non-passive event listener to a scroll-blocking 'mousewheel' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952