Child Row with database SQL Server
Child Row with database SQL Server
Hello,
I would like to do : https://datatables.net/examples/api/row_details.html
Parent row : Nom - Adresse - CodePostal - Ville - Pays
Children row : Telephone - Telecopie - SiteInternet - SiteSupport
The data is stored in the table name : 'Para_Editeur'
I work with database SQL Server.
- How I can add icon "+" on the rows of my table ?
- How I search the data of my row parent ?
My script 'editeur.js' (Datatable parent):
/* Formatting function for row details - modify as you need */
function format ( d ) {
// `d` is the original data object for the row
return '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">'+
'<tr>'+
'<td>Téléphone :</td>'+
'<td>'+d.Telephone+'</td>'+
'</tr>'+
'<tr>'+
'<td>Télécopie :</td>'+
'<td>'+d.Telecopie+'</td>'+
'</tr>'+
'<tr>'+
'<td>Site Internet :</td>'+
'<td>'+d.SiteInternet+'</td>'+
'</tr>'+
'<tr>'+
'<td>Site Support :</td>'+
'<td>'+d.SiteSupport+'</td>'+
'</tr>'+
'</table>';
}
$(document).ready(function() {
var table = $('#table_editeur').DataTable( {
"bProcessing": true,
"serverSide": true,
"ajax":{
url :"./response-displayrow_editeur.php", // json datasource
type: "post", // type of method ,GET/POST/DELETE
error: function(){
$("#table_editeur_processing").css("display","none");
}
},
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": '',
},
{ "data": "Nom" },
{ "data": "Adresse" },
{ "data": "CodePostal" },
{ "data": "Ville" },
{ "data": "Pays" },
{ "data": "Telephone" },
{ "data": "Telecopie" },
{ "data": "SiteInternet" },
{ "data": "SiteSupport" },
],
"order": [[1, 'asc']]
} );
// Add event listener for opening and closing details
$('#table_editeur tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
// var tdi = tr.find("i.fa");
var row = table.row( tr );
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
// tr.find('svg').attr('data-icon', 'plus-circle');
tr.removeClass('shown');
// tdi.first().removeClass('fa-minus-square');
// tdi.first().addClass('fa-plus-square');
}
else {
// Open this row
row.child( format(row.data()) ).show();
// row.child(format(tr.data('ACA'), tr.data('ACA'))).show();
// tr.find('svg').attr('data-icon', 'minus-circle');
tr.addClass('shown');
// tdi.first().removeClass('fa-plus-square');
// tdi.first().addClass('fa-minus-square');
}
} );
} );
My script 'response-displayrow_editeur.php' (Datatable parent):
```
<?php
//include connection file
include_once(".\db_connection.php");
// getting total number records without any search
$sql = "SELECT dbo.Para_Editeur.Nom,dbo.Para_Editeur.Telephone,dbo.Para_Editeur.Telecopie,dbo.Para_Editeur.SiteInternet,dbo.Para_Editeur.SiteSupport FROM dbo.Para_Editeur";
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
//iterate on results row and create new index array of data
while( $obj = sqlsrv_fetch_object( $stmt)) {
$data[] = $obj;
}
echo json_encode($json_data); // send data as json format
<?php
>
```
?>
Thank you for help
Answers
Does that not work with the code you have?
EDIT: You may need to create a CSS similar to what is shown in the CSS tab of the example you linked.
You are using server side processing and it looks like the data you want to show in the Child Details is also defined in
columns.data
. You can usecolumns.visible
to hide those columns and be able to search them using server side process.If this doesn't help then please provide more details of the issues. Better yet please provide a link to your page or a test case replicating the issue so we can take a look.
Kevin
Hello,
I have a database SQL server.
I have a table name : 'Para_Editeur'.
In this table, I have the fields : ID_Para_Editeur - Nom - Adresse - CodePostal - Ville - Pays - Telephone - Telecopie - SiteInternet - SiteSupport.
In my datatable, I show the fields in parent rows : ID_Para_Editeur - Nom - Adresse - CodePostal - Ville
I would like show in child rows the fields : Telephone - Telecopie - SiteInternet - SiteSupport.
I based myself on the example : https://datatables.net/examples/server_side/row_details.html
But it's no work !!!
My file ccs 'rowdetail_editeur' for show "+" and "-"
My file php 'Editeur'
My file javascript "editeur_test":
My file php for the child rows 'response-displayrow_editeur':
```
<?php
// DB table to use
$table = 'dbo.Para_Editeur';
// Table's primary key
$primaryKey = 'dbo.ID_Para_Editeur';
$columns = array(
array(
'db' => 'dbo.ID_Para_Editeur',
'dt' => 'DT_RowId',
'formatter' => function( $d, $row ) {
return 'row_'.$d;
}
),
array( 'db' => 'dbo.Nom', 'dt' => 'Nom' ),
array( 'db' => 'dbo.Adresse', 'dt' => 'Adresse' ),
array( 'db' => 'dbo.CodePostal', 'dt' => 'CodePostal' ),
array( 'db' => 'dbo.Ville', 'dt' => 'Ville' ),
array( 'db' => 'dbo.Pays', 'dt' => 'Pays' ),
array( 'db' => 'dbo.Telephone', 'dt' => 'Telephone' ),
array( 'db' => 'dbo.Telecopie', 'dt' => 'Telecopie' ),
array( 'db' => 'dbo.SiteInternet', 'dt' => 'SiteInternet' ),
array( 'db' => 'dbo.SiteSupport', 'dt' => 'SiteSupport' )
}
)
);
$sql_details = array(
'user' => 'User_Web',
'pass' => 'P@ssword',
'db' => 'Catalogue',
'host' => 'srvwds01'
);
// require( 'ssp.class.php' );
// echo json_encode(
<?php > ``` ?>// SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
// );
I don't know how I could make an example online with a database ?
Thank you for your help
When you say it doesn't work what is not working?
Do you get errors?
Do you see the plus and minus buttons?
Looks like your Datatb le also shows
Telephone - Telecopie - SiteInternet - SiteSupport.
. Do these appear?You've posted a lot of code which is difficult to just look through to debug the issue. You can take an example of your data and, instead of using
ajax
to fetch it, usedata
to supply the data to Datatables. Similar to this example:https://datatables.net/examples/data_sources/js_array.html
Kevin
The first problem I see is you have this in your
table
:But are defining this with
columns.data
:If you look in your browser's console you are probably seeing an error, something like this:
If you don't want to display the last four columns in
Telephone - Telecopie - SiteInternet - SiteSupport
in your table then remove them from columns.data. You can still return that data from the server and use it in the child rows.Fix this and keep an eye on the browser's console for errors.
Kevin
Hello,
Yes I see this error in my console : Uncaught TypeError: Cannot read property 'style' of undefined.
Hello,
Now I see the symbol "+" and "-".
But I'm not see the data...(see picture attached)
Thank you for your help.
Does your JSON response data contain these columns Telephone, Telecopie, SiteInternet, SiteSupport?
In the format function you can use
console.log( d );
to see what is avaialb ein the row data.Kevin
Hello Kévin
After a few tests, this no work !!!
I'm not undestand !!!
My table :
* ID_Para_Editeur : Primary Key
* Nom : Parent row
* Adresse : Parent row
* CodePostal : Parent row
* Ville : Parent row
* Pays : Parent row
* Telephone : Child row
* Telecopie : Child row
* SiteInternet : Child row
* SiteSupport : Child row
1 - How I can show my image "+" and "-" (See picture) ?
My file 'rowdetail_editeur.css' :
My file 'editeur.php':
My file 'response-displayrow_editeur.php':
Thank you very much
Romuald
Is this the correct path to the PNG files `background: url('../_Test/img/details_open.png') '?
Do you get 404 errors in the browser's developer tools when trying to load these files?
Kevin
Kevin
No, I don't get 404 errors in the browser's developer tools when trying to load these files.
I have a other error...(see picture)
Thank you for help
There are lots of threads with that error, for example:
https://datatables.net/forums/discussion/comment/158655/#Comment_158655
It is not the result of loading the PNG files. It is due to a mismatch between your HTML table and Datatables config. If you are unable to solve the issue with what you can find in the format then we will need a link to your page or a test case replicating the issue to help debug the problem.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Kevin,
I advanced a little on my problem... (see picture)
I create the file Ajax manual.
My database in SQL Server...
How i can find the datain my database without writing it to a text file (JSON) ?
Thank you very much
Kévin
I create file JSON:
And in my file JS:
Why I'm not see the data in my datatable and no error in console ?
Thank you for help
Kévin
Why I'm not see the data in my datatable ?
My file JSON :
My file generate the file JSON:
My file JS for read the file JSON:
Thank you for help
This is your JSON:
Even though this structure is a valid JSON structure its not one that is supported by Datatables. Datatables expects an array of rows as d3scribed in the data manual. The JSON needs to look more like this:
Kevin
Kevin,
I succeeded to show the data...
My table count 136 rows. (Database SQL Server)
My process may not be the best for use datatable :
My file write JSON:
My file load JSON:
How I can stored the rows in var without write the file JSON ?
Thank you very much
If I understand correctly, you would need the Ajax to point to a web service that returns that structure, rather than going via the file.
Colin
Colin,
I have a database SQL Server.
I would like to show the fields with columns parents and children:
For the rows children, I create a file JSON.
How I can tu put the data in var ?
Sorry for my bad english
Thank you