我有一个表,每行都有按钮显示基于数据行用户点击(使用数据表)的更多详细数据,过程是我使用ajax将数据从js文件传递到php文件然后在那个php文件之后的一些进程生成一个数组再将它返回到js然后按1回显数组1 ..
我成功使用datatablescript.js中的ajax将变量传递给childdatatatable.php .
但我仍然无法想象如何将我从childdatatatable.php生成的数组传回datatablescript.js并显示它(通过alert或echo或任何东西)
sry for bad english ..
这是代码:
transactions.php
<table id="transactiontable" class="table table-striped table-bordered display" cellspacing="0" width="99%">
<thead>
<tr>
<th width="4%"></th>
<th width="4%">Order Date</th>
<th width="10%">ID Transaksi</th>
<th width="7%">User ID</th>
<th width="9%">Total</th>
<th width="5%">Status</th>
</tr>
</thead>
<tbody>
<?php
$query = mysqli_query($koneksi, "select * from penjualan order by tanggal desc");
while($data = mysqli_fetch_array($query)){
?>
<tr data-child-value="<?php echo $data['no_pen'];?>" id="<?=($data['no_pen'])?>">
<td class="details-control"></td>
<td>
<center><?php echo $data['tanggal']; ?></center>
</td>
<td>
<center><?php echo $data['no_pen']; ?></center>
</td>
<td>
<center><?php echo $data['id_usr']; ?></center>
</td>
<td>
<center>Rp. <?php echo number_format($data['jumlah_total'],0,'','.'); ?>,-</center>
</td>
<td>
<center><?php echo $data['status']; ?></center>
</td>
</tr>
<?php } ?>
</tbody>
</table>
datatablescript.js
function format(value) {
var id = value;
return '<div>Detail Item : ' + value + '</div>';
}
$(document).ready(function() {
var table = $('#transactiontable').DataTable({});
// Add event listener for opening and closing details
$('#transactiontable').on('click', 'td.details-control', function() {
var elem = $(this),
selecteditem = elem.val(),
id = elem.closest('tr').attr('id');
$.ajax({
type: "post",
url: "childdatatable.php",
data: {
'id': id
},
success: function(data) {
if (data) {
var test = '<?php echo json_encode($brid) ?>';
alert(test);
}
}
});
var tr = $(this).closest('tr');
var row = table.row(tr);
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
} else {
// Open this row
row.child(format(tr.data('child-value'))).show();
tr.addClass('shown');
}
});
});
childdatatatable.php
require_once("koneksi.php");
session_start();
if (!isset($_SESSION['username'])){
echo "<script>alert('You must register an account first, we will redirect you to register page !'); window.location = 'registuser.php'</script>";
}
$no_pen = $_POST['id'];
$query = "SELECT br_id from item_penjualan where penjualan_id = '$no_pen'";
if ($stmt = mysqli_prepare($koneksi,$query))
{
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt,$brid);
while (mysqli_stmt_fetch($stmt)){
printf ($brid);
}
json_encode($brid);
mysqli_stmt_close($stmt);
}
1 回答
在对数组进行编码后@
childdatatatable.php
,回显它 .另外:printf是浪费的一条线,对吧?我的意思是,你没有对
$brid
进行任何格式化 .@
datatablescript.js
你可以告诉.js期望和解码你的json像这样: