Select Rows when data is loaded from ajax
Select Rows when data is loaded from ajax
Hi there,
I'm having some problem regarding the selection of rows when data is retreived by ajax call.
The selection don´t work.
Here is my code:
***************** html file:******************
Empresa
NIPC
Morada
Código Postal
Telefone
Email
Website
Concelho
***************** js file:******************
$(document).ready(function() {
$('#TEmpresas').dataTable( {
"bProcessing": true,
"sAjaxSource": 'ajax/lista_empresas.php',
"sPaginationType": "full_numbers"
} );
$('#TEmpresas tr').click( function() {
$(this).toggleClass('row_selected');
} );
***************** php for ajax call file:******************
$query = "SELECT * FROM TEmpresas";
@$db = mysqli_connect($host,$user,$password,$database) or die(mysql_error());
$result = mysqli_query($db, $query);
mysqli_close($db);
$i=1;
$data="{\"aaData\": [";
while($row = mysqli_fetch_array($result))
{
$data.="[\"".
$row["Empresa"]."\",\"".
$row["NIPC"]."\",\"".
$row["Morada"]."\",\"".
$row["CPostal"]."\",\"".
$row["Telefone"]."\",\"".
$row["Email"]."\",\"".
$row["URL"]."\",\"".
$row["IDConcelho"]."\"]";
if ($i
I'm having some problem regarding the selection of rows when data is retreived by ajax call.
The selection don´t work.
Here is my code:
***************** html file:******************
Empresa
NIPC
Morada
Código Postal
Telefone
Website
Concelho
***************** js file:******************
$(document).ready(function() {
$('#TEmpresas').dataTable( {
"bProcessing": true,
"sAjaxSource": 'ajax/lista_empresas.php',
"sPaginationType": "full_numbers"
} );
$('#TEmpresas tr').click( function() {
$(this).toggleClass('row_selected');
} );
***************** php for ajax call file:******************
$query = "SELECT * FROM TEmpresas";
@$db = mysqli_connect($host,$user,$password,$database) or die(mysql_error());
$result = mysqli_query($db, $query);
mysqli_close($db);
$i=1;
$data="{\"aaData\": [";
while($row = mysqli_fetch_array($result))
{
$data.="[\"".
$row["Empresa"]."\",\"".
$row["NIPC"]."\",\"".
$row["Morada"]."\",\"".
$row["CPostal"]."\",\"".
$row["Telefone"]."\",\"".
$row["Email"]."\",\"".
$row["URL"]."\",\"".
$row["IDConcelho"]."\"]";
if ($i
This discussion has been closed.
Replies
That is a static function which is being run before the Ajax data has been loaded - thus there are no cells there. What you want to do is use a delegated event:
[code]
$('#TEmpresas tbody').on( 'click', 'tr', function() {
[/code]
Regards,
Allan