Javascript 如何使用onclick从另一个ajax请求重新加载数据表
我尝试使用datatables是因为它很简单,但现在我遇到了在单击后从ajax请求中重新加载和获取datatables中新数据的问题。有关详细信息,请参阅下面的我的脚本: index.htmlJavascript 如何使用onclick从另一个ajax请求重新加载数据表,javascript,php,ajax,datatables,Javascript,Php,Ajax,Datatables,我尝试使用datatables是因为它很简单,但现在我遇到了在单击后从ajax请求中重新加载和获取datatables中新数据的问题。有关详细信息,请参阅下面的我的脚本: index.html <html> <head> <title>Datatables</title> <link rel="stylesheet" type="text/css" href="../plugins/DataTables/styles.css"/>
<html>
<head>
<title>Datatables</title>
<link rel="stylesheet" type="text/css" href="../plugins/DataTables/styles.css"/>
<script type="text/javascript" src="../plugins/DataTables/datatables.min.js"></script>
<script type="text/javascript" src="../plugins/DataTables/javascript.js"></script>
<script src="../lib/js/jquery/jquery.min.js"></script>
</head>
<body>
<table id="tablenya" class="datatable responsive nowrap" style="width:100%">
<thead>
<tr>
<th>No</th>
<th>PO Date</th>
<th>WO No</th>
<th>PO No</th>
<th>Customer</th>
<th>Size</th>
<th>Status</th>
<th>Option</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
然后process.php节获取数据库中的数据
<?php
require 'connect.php';
$action = '';
$id = '';
if(isset($_GET['action'])){
$action = $_GET['action'];
if($action == 'result'){
if (isset($_GET['id'])){
$id = $_GET['id'];
if (!is_numeric($id)){
$id = '';
}
}
} else {
$action = '';
}
}
$mysqli_data = array();
if ($action == 'result'){
$query = "SELECT * FROM workorder WHERE status='1' ORDER BY id DESC";
$sql = $connect->query($query);
if (!$sql){
$result = 'error';
$message = 'query error';
} else {
$result = 'success';
$message = 'query success';
$no = 1;
while($row = $sql->fetch_array()){
$functions = '<div class="function_buttons"><ul>';
$functions .= '<li class="function_view"><a data-id="'.$row['id'].'" data-name="'.$row[$dataName].'" title="View details"><span>View details</span></a></li>';
$functions .= '</ul></div>';
$mysqli_data[] = array(
"no" => $no++,
"date_po" => $row['date_po'],
"no_spk" => $row['no_spk'],
"no_po" => $row['no_po'],
"customer" => $row['customer'],
"size" => $row['size'],
"status" => $status,
"functions" => $functions
);
}
}
}
mysqli_close($connect);
$data = array(
"result" => $result,
"message" => $message,
"data" => $mysqli_data
);
$json_data = json_encode($data);
print $json_data;
?>
这是另一个.php部分
<?php
require 'connect.php';
$action = '';
$id = '';
if(isset($_GET['action'])){
$action = $_GET['action'];
if($action == 'result'){
if (isset($_GET['id'])){
$id = $_GET['id'];
if (!is_numeric($id)){
$id = '';
}
}
} else {
$action = '';
}
}
$mysqli_data = array();
if($action == 'result'){
if ($id == ''){
$result = 'erro';
$message = 'ID missing';
} else {
$idx = mysqli_real_escape_string($connect, $id);
$query = "SELECT * FROM workorder LEFT JOIN workorder_process ON workorder.id_fk = workorder_process.id_fk WHERE workorder.id = '".$idx."'";
$sql = $connect->query($query);
$get = $sql->fetch_array();
if($get['delivery_type'] == '2'){
$result = 'success';
$message = 'query success';
$no = 1;
while($row = $sql->fetch_array()){
$functions = '<div class="function_buttons"><ul>';
$functions .= '<li class="function_edit"><a data-id="'.$row['id'].'" data-name="'.$row[$dataName].'" title="Edit"><span>Edit</span></a></li>';
$functions .= '</ul></div>';
$mysqli_data[] = array(
"no" => $no++,
"date_po" => $row['date_po'],
"date_spk" => $row['date_spk'],
"no_spk" => $row['no_spk'],
"no_po" => $row['no_po'],
"customer" => $row['customer'],
"size" => $row['size'],
"qore" => $row['qore'],
"roll" => $row['roll'],
"material" => $row['material'],
"ingredient" => $row['ingredient'],
"send_qty" => $row['send_qty'],
"volume" => $row['volume'],
"annotation" => $row['annotation'],
"functions" => $functions
);
}
} else {
$result = 'error';
$message = 'ID missing';
}
}
}
mysqli_close($connect);
$data = array(
"result" => $result,
"message" => $message,
"data" => $mysqli_data
);
$json_data = json_encode($data);
print $json_data;
?>
那么,在使用ajax请求单击函数视图之后,如何重新加载并获取新数据呢?在注释行javascript.js中
$(document).ready(function(){
var tablenya = $('#tablenya').dataTable({
"ajax": "process.php?action=result",
"columns": [
{ "data": "no" },
{ "data": "date_po" },
{ "data": "no_spk"},
{ "data": "no_po" },
{ "data": "customer"},
{ "data": "size"},
{ "data": "status"},
{ "data": "functions","sClass": "functions" }
]
});
});
//重新加载数据表并显示新请求
可能吗?请给我一些建议
$document.readyfunction{
$country.onchange,函数{
var country_id=$this.val;
如果国家/地区id{
$.ajax{
类型:POST,,
url:location_ajax.php,
数据:{
countryid:country\u id,
},
成功:functionhtml{
$state.htmlhtml;
}
};
}否则{
$state.htmlSelect Country;
}
};
};
地点:
选择一个国家
您只需要这一行来重新加载数据表,您还可以创建一个函数并调用它。试试看
tablenya.ajax.reload(null,false);
在onclick处理程序中,另一个解决方案是销毁表,然后重新创建。要销毁,请使用$'tablenya'.DataTable.destroy。然后,您可以像以前一样,在代码的前面初始化表格。请尽量少提问题并使其完整!我已经搞定了帖子,但我不知道如何传达更多细节,先生。总之,我希望在单击datatables索引中选项列中的view(查看)后重新加载并获取新数据。htmlonly reload datatables sir(重新加载数据表)而不是从ajax请求中替换并获取新数据,请给出更多建议
tablenya.ajax.reload(null,false);