我使用了以下脚本,数据未加载到dataTable,我收到错误click here to show error image 这是我的jquery代码
Table = $("#example").DataTable({
data:[],
columns: [
{ "data": "Course" },
{ "data": "Batch" },
{ "data": "Admission No" },
{ "data": "Rollno" },
{ "data": "Student Name" },
{ "data": "Email" }
],
rowCallback: function (row, data) {},
filter: false,
info: false,
ordering: false,
processing: true,
retrieve: true
});
$('.view_search_btn').click(function(){
//alert("clicked");
var search_key = $('input[name=view_stu_search]').val();
$('.box-body').show();
$.ajax({
url: "../live_search.php",
type: "post",
data: "key="+'view_student_search_key'+"&search_keyword="+search_key
}).done(function (result) {
Table.clear().draw();
Table.rows.add(result).draw();
}).fail(function (jqXHR, textStatus, errorThrown) {
// needs to implement if it fails
});
}); //点击此处关闭的活动 }); //文件在这里关闭 我的PHP代码是
if(!ctype_digit($_POST['search_keyword'])){
//echo "given value is string".$_POST['search_keyword'];
$rows = search_keyword($view_student_search_key);
//print_r($rows);
foreach($rows as $row)
{
$query = "SELECT a.stu_rollno, c.stu_course,c.stu_batch,a.admission_no,p.stu_firstname,p.stu_lastname,co.stu_email FROM current_course c, admission_details a,stu_personal_details p, stu_contact_details co WHERE a.stu_rollno = p.stu_rollno AND c.stu_rollno = co.stu_rollno AND a.stu_rollno = c.stu_rollno AND (c.stu_degree = ".$row['degree_id']." AND c.stu_course = ".$row['course_id']." AND c.stu_branch = ".$row['branch_id'].");";
//echo "<br><br>";
$run_query = mysqli_query($con, $query);
while($result = mysqli_fetch_array($run_query))
{
echo "
<tr>
<td>".$row['course_name']."</td>
<td>".$result['stu_batch']."</td>
<td>".$result['admission_no']."</td>
<td>".$result['stu_rollno']."</td>
<td>".$result['stu_firstname'].$result['stu_lastname']."</td>
<td>".$result['stu_email']."</td>
<td align='center'><a href='edit.php?rollno=".$result['stu_rollno']."°ree=".$row['degree_name']."&course=".$row['course_name']."&branch=".$row['branch_name']."' class='btn btn-info btn-sm btn-flat'><i class='fa fa-edit'></i> Edit</a>
<button type='button' class='btn btn-danger btn-sm btn-flat' name='remove_levels' data-toggle='modal' data-target='.bs-example-modal-sm'><i class='fa fa-close'></i> Delete</button>
</td>
</tr>
";
}
}
}`
我的php中的以下函数
function search_keyword($view_student_search_key)
{
$query = "SELECT d.degree_id,d.degree_name,c.course_id,c.course_name,b.branch_id,b.branch_name FROM degree d,courses c,branch b WHERE d.degree_id = c.degree_id AND b.course_id = c.course_id AND (d.degree_name like '%$view_student_search_key%' OR c.course_name like '%$view_student_search_key%' OR b.branch_name like '%$view_student_search_key%')";
global $con;
$run_query = mysqli_query($con, $query);
//Declare the rows to an array
$rows = array();
// Insert Each row to an array
while($row = mysqli_fetch_array($run_query))
{
$rows[] = $row;
}
// Return the array
return $rows;
}`
答案 0 :(得分:1)
此处,您为标题添加了总共6 td
,而对于内tr
,您正在添加7 td
。因此,每行的数据表td
不匹配。
您可以做的是:
只需在td
(逗号)之前的数据表初始化中再添加一个,
:
{ "data": "Actions" }
注意: Action
是td
编辑&amp;的标题。其他图标栏。
答案 1 :(得分:0)
使用xhr.dt并使用datatables load() api重新加载数据。 xhr.dt将在加载时监听ajax调用数据。您可以在xhr回调函数中清除和绘制,即附加数据。希望有所帮助。