我尝试使用数据表,因为它很简单,但现在我有重新加载的问题,并在点击后从ajax请求获取数据表中的新数据。有关详细信息,请参阅下面的我的脚本:
的index.html
<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>
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" }
]
});
});
然后在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;
?>
在加载页面&#34; index.html&#34;之后,使用数据表生成我的数据。看一下&#34;功能查看&#34;在列选项中,我想用click创建另一个函数,根据值&#34; data-id =&#34;显示数据库中另一个表的数据。有ajax请求,在它下面:
javascript.js部分onclick功能
$(document).ready(function(){
$(document).on('click', .function_view a, function(e){
e.preventDefault();
var id = $(this).data('id');
var request = $.ajax({
url: "another.php?action=result",
cache: false,
data: 'id='+id,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'get'
});
request.done(function(output){
if (output.result == success){
/////////////////////////////////////////
// reload datatables and showing new request
////////////////////////////////////////
} else {
show_message('Information request failed', 'error');
}
});
request.fail(function(jqXHR, textStatus){
show_message('Information request failed: '+textStatus, 'error');
});
});
});
这是另一个.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;
?>
所以,点击&#34;功能视图后如何重新加载并获取新数据&#34;用ajax请求?在评论行javascript.js
有可能吗?拜托,给我建议//重新加载数据表并显示新请求
答案 0 :(得分:0)
$(document).ready(function() {
$("#country").on("change", function() {
var country_id = $(this).val();
if (country_id) {
$.ajax({
type: "POST",
url: "location_ajax.php",
data: {
countryid: country_id,
},
success: function(html) {
$("#state").html(html);
}
});
} else {
$("#state").html("<option value=''>Select Country</option>");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-9">
<label>Location:</label>
<select name="country" id="country" class="form-control" required>
<option value="">Select A Country</option>
<?php
if($count > 0) {
while($row = mysqli_fetch_assoc($result)){
$country_id = $row['country_id'];
$country_name = $row['c_name'];
echo "<option value='$country_id'>$country_name</option>";
}
}
else {
echo "<option value=''>Country is Not Available</option>";
}
?>
</select>
<select name="state" id="state" class="form-control" required>
<option value="">Select Country</option>
</select>
</div>
//location_ajax.php
<?php
include("connect.php");
if(isset($_POST['countryid'])){
$country_id = $_POST['countryid']; ?>
<script type="text/javascript">
var c = <?php echo "$country_id"; ?>
alert("My Number " + c);
</script>
<?php
if($country_id == 100) {
$sql = "SELECT * FROM states WHERE country = $country_id ORDER BY state_id";
$result = $db->query($sql);
$count = mysqli_num_rows($result);
if($count > 0) {
echo "<option value=''>Select State</option>";
while($row = mysqli_fetch_assoc($result)) {
$state_id = $row['state_id'];
$state_name = $row['state_name'];
echo "<option value='$state_id'>$state_name</option> ";
}
}
else {
echo "<option value=''>Select State</option>";
}
}
else {
echo "<option value=''>Select State</option>";
echo "<option value='0'>Others</option>";
}
}
?>
我只知道你正在尝试使用ajax来加载数据库值。 以下代码可能不适用于您的特定代码,但它可能对您有所帮助。请参考代码。 在此代码中,当您选择国家/地区时,ajax将自动加载该国家/地区的州!
答案 1 :(得分:0)
您只需要这一行来重新加载数据表,您也可以创建一个函数并调用它。试一次。
tablenya.ajax.reload(null,false);
答案 2 :(得分:0)
在onclick
处理程序中的另一个解决方案是销毁表,然后重新创建。要销毁,请使用$('#tablenya').DataTable().destroy()
。然后,您可以像之前一样在代码的前面初始化表格。