[英]How can I get value into a row of html datatable?
我有一個PHP文件,其中包含一個從dataTable.net獲取的表
我使用以下代碼加載表和內容:
<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>
當我單擊帶有下一個代碼的行時,我添加了一個selected,但是我無法獲得值username,因為我進入了從jquery調用的[object object]或未定義的php文件。 這是在使用時得到的:
myFunction(table.row('.selected').value);
而且我也嘗試使用table.row('。selected')。eq(0).data('username')和value('username')...但我無法獲取該值。
<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>
如何獲得所選行的值? 謝謝!
您可以嘗試以下方法:
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 );
}
您應該偵聽相應的事件(設置事件處理程序),並使用API進行數據檢索或直接從DOM獲取它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.