如何在一行html数据表中获取价值?

时间:2015-12-07 17:18:02

标签: javascript php jquery mysql datatable

我有一个带有从dataTable.net

获取的表的php文件

我用这段代码加载我的表和内容:

<div class="table-responsive">
     <div class="panel-responsive">
        <div class="panel-heading" align="center"><h3>Usuarios</h3></div>
    <table id="example" class="display" cellspacing="0" width="100%">

        <?php 
        //require_once('funciones.php');
        //conectar('localhost', 'root', '', 'agroapp');
        $result = mysql_query("SELECT * FROM usuario WHERE username <> 'admin'"); 

  if ($row = mysql_fetch_array($result)){ 
  ?>
  <thead>
  <tr> 
  <th>Username</th>
  </tr>
  </thead>
<tfoot>
        <tr>
            <th>Username</th>
        </tr>
         </tfoot>

 <tbody>

 <?php
  do { 
    $titulo=$row['nombre'];
    $post = $row['username'];
    $rol = $row['rol'];
    $idUser = $row['idUsuario'];

        echo "<tr > \n"; 
          //if($row["username"]!="admin"){
        //echo "<td >  </td> \n";
        echo "<td ><img src= ".$row["ruta"]." height='30' width='30 class=profile-photo img-circle'>  ".$row["username"]." </td> \n";


        echo "</tr> \n"; 
        //}
     } while ($row = mysql_fetch_array($result)); 
       //echo "</tbody></table> \n"; 
} else { 
    echo '<h5 align="center">¡ No tiene empleados añadidos! </h5>'; 
} 
 ?>

</tbody>
</table>
</div>
</div>

当我用下一个代码点击一行时,我添加了一个选中,但是我无法获得值username,因为我从jquery a [object object]或undefined调用了php文件。这是在使用时得到的:

 myFunction(table.row('.selected').value); 

我也尝试使用table.row('。selected')。eq(0).data('username')和value('username')...但是我无法获得值。< / p>

<script>
$(document).ready(function() {
var table = $('#example').DataTable();

$('#example tbody').on( 'click', 'tr', function () {
    if ( $(this).hasClass('selected') ) {
        $(this).removeClass('selected');
    }
    else {
        table.$('tr.selected').removeClass('selected');
        $(this).addClass('selected');
    }
} );

$('#button').click( function () {

    myFunction(table.row('.selected').eq(0).data('username'));  //Here I'm trying get the value
    table.row('.selected').remove().draw( false );
 } );
} );
function myFunction(val) {

var getted = val;

        $.ajax({
data: 'empleado=' + getted,
url: 'updateEmpleado.php',
method: 'POST', // or GET
success: function(msg) {
    //alert(msg);
    location.reload();
}
});
}

 </script>

如何获取所选行的值?谢谢!

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

var table = $('#example').DataTable();
var myData;
$('#example tbody').on( 'click', 'tr', function () {
    if ( $(this).hasClass('selected') ) {
        $(this).removeClass('selected');
    }
    else {
        table.$('tr.selected').removeClass('selected');
        $(this).addClass('selected');
    }
    myData = table.row( this ).data();
} );

$('#button').on('click', myFunction);

function myFunction(){
        var getted = myData.username;

        $.ajax({
           data: 'empleado=' + getted,
           url: 'updateEmpleado.php',
           method: 'POST', // or GET
           success: function(msg) {
              //alert(msg);
             location.reload();
           }
       });
       table.row('.selected').remove().draw( false );
}

参考:https://datatables.net/reference/api/row().data()

答案 1 :(得分:0)

您应该监听相应的事件(设置事件处理程序)并使用API​​进行数据检索或直接从DOM获取。

https://datatables.net/reference/event/select