简体   繁体   English

使用 ajax 时表功能不起作用

[英]Table functions doesn't work when using ajax

So I've been using php for a while now but recently, I've decided to use ajax so I can view live data and I don't have to refresh the page to see changes in the database.因此,我一直在使用 php 一段时间,但最近,我决定使用 ajax,这样我就可以查看实时数据,而无需刷新页面即可查看数据库中的更改。 And I created a page and the fetch function works but when it's in the table, the table functions doesn't work.我创建了一个页面,提取 function 可以工作,但是当它在表格中时,表格功能不起作用。 At the bottom of the table it says it's showing 0 out of 0 entries and the sort by function and the show {limit} function doesn't work either.在表格的底部,它说它显示了 0 个条目中的 0 个,并且按 function 和 show {limit} function 的排序也不起作用。 It's like somehow the table div doesn't read the data inside.这就像表格 div 不读取里面的数据一样。

Here's my code:这是我的代码:

<div class="body" >
    <div class="table-responsive">
        <table class="table table-bordered table-striped table-hover js-basic-example dataTable" >
            <thead>
                <tr>
                    <th width="155">Action</th>
                    <th width="90">LRN</th>
                    <th width="45">Level</th>
                    <th>Name</th>
                    <th width="15">Gender</th>
                    <th width="65">Type</th>
                    <th width="110" style="font-size: 14px!important;">Date Enrolled</th>
                    <th width="40">Card</th>
                </tr>
            </thead>
            <tbody id="live-data">

            </tbody>
        </table>
    </div>
</div>

<script type="text/javascript">
        $(document).ready( function() {
            function fetch_data() {  
                $.ajax({  
                    url:"fetch.php",  
                    method:"POST",  
                    success:function(data){  
                        console.log(data);
                        $('#live-data').html(data);  
                    }  
                });  
            }
            fetch_data(); 
        });
</script>

Here's fetch.php这是 fetch.php

<?php 

include('../global/db.php');
$output = ''; 
$query ="SELECT * FROM students WHERE status = '0' ORDER BY date_enrolled DESC";  
$result = mysqli_query($db, $query);

if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_array($result)){  
        $date_enrolled = $row['date_enrolled']; 
        $card = $row['card'];
        $stud_birth = $row['stud_birth'];
        $age = date('Y', strtotime($stud_birth)); $year = date('Y '); $age = (int)$age; $year = (int)$year;
        $sage = $year - $age;

        $output .= ' 
            <tr>
                <td>
                    <button type="button" class="btn bg-cyan btn-xs waves-effect" data-toggle="modal" data-target="#'.$row['stud_id'].'">
                        <i class="material-icons">search</i>
                        <span>Profile</span>
                    </button>&nbsp;<button type="button" class="btn bg-orange btn-xs waves-effect" data-toggle="modal" data-target="#'.$row['stud_id'].''.$row['stud_id'].'">
                        <i class="material-icons">search</i>
                        <span>Approve</span>
                    </button>
                </td>
                <td>'.$row['stud_lrn'].'</td>
                <td>'.$row['stud_grade'].'</td>
                <td>'.$row['stud_lname'].', '.$row['stud_fname'].' '.$row['stud_mname'].'</td>
                <td>'.$row['stud_gender'].'</td>
                <td>'.$row['stud_type'].'</td>
                <td style="font-size: 12px!important;">'.$date_enrolled = date("M-d-Y g:i A", strtotime($date_enrolled)).'</td>
                <td>';
                if ($card == "") {                                          
                    $output .= ' 
                    <center>N/A</center>';
                }

                else {
                    $output .= ' 
                    <center><a target="_blank" href="../image-gallery/20.jpg" data-sub-html="Demo Description">
                    <img class="img-responsive" src="../image-gallery/thumb/thumb-20.jpg" style="width: 70px; height: 35px;"></a></center>';
                }
                $output .= '    
                </td>
            </tr>';
    }
}

else {  
    $output .= '
            <tr>  
                <td colspan="4">Data not Found</td>  
            </tr>';  
}  

echo $output;  

?>

I think you are using dataTable jquery library.我认为您正在使用 dataTable jquery 库。 Problem is its not right way to use datatabe.问题是它使用数据表的方式不正确。 you need a json format out from backend like as您需要从后端输出 json 格式,例如

{
  "data": [
[
  "Tiger Nixon",
  "System Architect",
  "Edinburgh",
  "5421",
  "2011/04/25",
  "$320,800"
],
[
  "Garrett Winters",
  "Accountant",
  "Tokyo",
  "8422",
  "2011/07/25",
  "$170,750"
],  

} }

Don't forget to warp with {data:[]} object不要忘记使用 {data:[]} object

after then simply use in ajax然后只需在 ajax 中使用

$(document).ready(function() {
  $('#live-data').DataTable( {
    "ajax": 'fetch.php'
  } );
} ); 

For more... https://datatables.net/examples/data_sources/ajax.html更多... https://datatables.net/examples/data_sources/ajax.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM