Select Rows when data is loaded from ajax

Select Rows when data is loaded from ajax

hdsahdsa Posts: 7Questions: 0Answers: 0
edited November 2013 in Web-site
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

Replies

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin
    > $('#TEmpresas tr').click( function() {

    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
This discussion has been closed.