[英]I am getting table data from database and displaying on table but datatables search is not working on data
I am using datatables and search field is not working on the data retrieved from database via ajax POST method.我正在使用数据表并且搜索字段无法处理通过 ajax POST 方法从数据库中检索到的数据。 Kindly help me
请帮助我
I am unable to understand server processing documentation of datatables.我无法理解数据表的服务器处理文档。
I am just simply taking data from database and appending it to tag.我只是简单地从数据库中获取数据并将其附加到标签。 I know i am doing wrong.
我知道我做错了。 I need help!
我需要帮助!
<?php $result=mysqli_query($conn,"select * from program"); while ($row = mysqli_fetch_assoc($result)) { $pid=(int)$row['ID']; $title=$row['TITLE']; $total=0; $result1=mysqli_query($conn,"select SUM(DISTINCT(TOTAL)) AS TOTAL from teams GROUP BY TEAM,PID having PID = $pid"); while($row1=mysqli_fetch_assoc($result1)) { $total=$total+(int)$row1['TOTAL']; } $result2=mysqli_query($conn,"select * from shows where PID = $pid"); $start=""; $end=""; $i=0; while($row2=mysqli_fetch_assoc($result2)) { if($i==0){ $start=$row2['START']; $i++; } $end=$row2['END']; $city=$row2['CITY']; } $arr[] = array("title" => $title, "total" => $total, "start" => $start, "end" => $end, "city" => $city, "pid"=>$pid); } echo json_encode($arr); ?>
$(document).ready(function() { $("#datatable").DataTable() }) $(document).ready(function() { $.ajax({ type: 'POST', url: 'getPrograms.php', dataType: 'json' }).done(function(res) { $("#allPrograms").html(""); for (var i = 0; i < res.length; i++) { $("#allPrograms").append('<tr><td>' + res[i].title + '</td><td>' + res[i].total + '</td><td>' + res[i].start + '</td><td>' + res[i].end + '</td> <td>' + res[i].city + '</td> <td><span class="badge badge-secondary font-10">Active</span></td>' + '<td><a type="button" class="btn btn-primary btnAction" value="' + res[i].pid + '"><i class="mdi mdi-eye-outline" style="color:white"></i></button></td></tr>') } }).fail(function(err) { console.log(err); }) })
<table id="datatable" class="table table-bordered dt-responsive nowrap"> <thead class="thead-light"> <tr> <th>Program Name</th> <th>Consultants</th> <th>Start Date</th> <th>End Date</th> <th>Location</th> <th>Status</th> <th>Action</th> </tr> </thead> <tbody id="allPrograms"></tbody> </table>
Initialize your datatable after you have filled the html in page.在页面中填充 html 后初始化您的数据表。 ie Call this line
$("#datatable").DataTable()
after you re done inserting HTML on page.即在您完成在页面上插入 HTML 后调用此行
$("#datatable").DataTable()
。
On a separate note, dont use append to add HTML.另请注意,不要使用 append 添加 HTML。 This is a bad practice.
这是一个不好的做法。 Instead take a variable and append all the HTML in it.
取而代之的是获取一个变量并将所有 HTML 附加到其中。 Once you are done, make a single call like
$("#allPrograms").html(yourVariableHere)
to insert the HTML on page.完成后,进行一次调用,如
$("#allPrograms").html(yourVariableHere)
以在页面上插入 HTML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.