简体   繁体   English

我正在从数据库中获取表数据并显示在表上,但数据表搜索无法处理数据

[英]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.

相关问题 从表中搜索数据不起作用 - Search data from the table is not working 从 MySQL 到 JavaScript DataTables 到网页显示 24 列的数据表 - Displaying data table of 24 columns from MySQL to JavaScript DataTables to a webpage 从sql数据库检索数据并将其显示在表中 - Retrieving data from a sql database and displaying it in a table 数据表-搜索; 但不要立即过滤数据表 - Datatables - Search; but do not instantly filter the data table 使用Jquery Datatables在表内的表上显示数据时出现问题 - Issue displaying data on a table inside a table using Jquery Datatables 从数据库中获取数据时,数据表不起作用 - datatables is not working when fetching data from database 使用服务调用从共享点列表数据中获取数据并显示在html表中,一切正常,但一个javascript失败 - Using service call getting data from sharepoint list data and displaying in html table, everything is working but one javascript is failing 根据搜索条件通过表格显示数据库中的数据 - Display data from database via a table based on search criteria 为什么我要从一个空的mongodb数据库中获取数据? - Why am I getting data from an empty mongodb database? Codeigniter Datatables /表添加数据 - Codeigniter Datatables / table add data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM