Php 向Datatables中的标记添加事件

Php 向Datatables中的标记添加事件,php,jquery,ajax,datatables,Php,Jquery,Ajax,Datatables,所以我决定使用Datatables,并设法让Datatables从json加载数据,但我不知道如何向行中添加onclick事件或类 以下是我使用的旧代码: <tbody id="skladove_table"> <?php $query_checkObekt = "SELECT * FROM obekti WHERE owner='$user_hash'"; $result_checkObekt = mysqli_query($conn, $query_ch

所以我决定使用Datatables,并设法让Datatables从json加载数据,但我不知道如何向行中添加onclick事件或类

以下是我使用的旧代码:

    <tbody id="skladove_table">

 <?php
  $query_checkObekt = "SELECT * FROM obekti WHERE owner='$user_hash'";
  $result_checkObekt = mysqli_query($conn, $query_checkObekt);

  if(mysqli_num_rows($result_checkObekt) > 0) {

    $query = "SELECT * FROM items WHERE owner ='$user_hash'";
    $result=mysqli_query($conn, $query);

    while($row = mysqli_fetch_assoc($result))

            {
              echo '<tr id="' . $row['unique_id'] . '" onmouseover=showItem(' . $row['unique_id'] . ');>';

              echo "<td>" . "<center>" . $row['custom_id'] . "</center>" . "</td>";
              echo "<td>" . "<center>" . $row['name'] . "</center>" . "</td>";
              echo "<td>" . "<center>" . $row['barcode'] . "</center>" . "</td>";
              echo "<td>" . "<center>" . $row['grupa'] . "</center>" . "</td>";
              echo "<td>" . "<center>" . $row['quantity_type'] . "</center>" . "</td>";
              echo "<td>" . "<center>" . $row['quantity_number'] . "</center>" . "</td>";
              echo "<td>" . "<center>" . $row['obekt'] . "</center>" . "</td>";
              echo "<td>" . "<center>" . $row['price_delivery'] . "</center>" . "</td>";
              echo "<td>" . "<center>" . $row['price_sale'] . "</center>" . "</td>";
              echo "<td style='width: 1%; padding-left: 1%; padding-right: 1%;' onclick='deleteItem(" . $row['unique_id'] . ");'><i class='fa fa-close'></i>"."</td>";

              echo "</tr>";
            }

  } else {
    //if no records in DB
  }

  ?>

  </tbody>


您可以使用
rowCallback
,在其中您可以向行添加任何您想要的内容,例如单击处理程序、类或更多内容。大概是这样的:

"rowCallback": function( row, data ) {
  $(row).addClass('myClass');
  $(row).click(function() {
    alert('hello');
  });    
}

您可以使用rowcallback:@alanfcm如何从使用ajax检索的数组中插入内容?
"rowCallback": function( row, data ) {
  $(row).addClass('myClass');
  $(row).click(function() {
    alert('hello');
  });    
}