【问题标题】:Datatable filtering with select option is not working in php ajax带有选择选项的数据表过滤在 php ajax 中不起作用
【发布时间】:2021-05-19 02:30:11
【问题描述】:

我在数据表中有多个基于 ajax jquery php 的选择选项过滤。它正在正确过滤记录,但在更改选择选项过滤后,会显示一条警报消息,并且底部计数显示不正确。

注意:-

(1)数据表下方的计数显示不正确。

(2) 显示一条警报消息。

这是带有记录和选择选项过滤器的数据表。

选择选项后会显示一条错误警报消息。

选择选项过滤器完成后,底部计数错误。

HTML 代码:-

<div class="col-md-12">
<form name="search_form" id="search_form" method="POST"> 
          
 <div class="col-md-3">
                <div class="formrow">
                  <select class="form-control select_filter" name="job_title" >
                    <option value ='' disabled selected>Job Title</option>
                    <option>PHP Developer</option>
                    <option>Android Developer</option>
                  </select>
                </div>
              </div>
              
  <div class="col-md-3">
                <div class="formrow">
                  <select class="form-control select_filter" name="emp_status" >
                    <option value ='' disabled selected>Employment Status</option>
                    <option>Permanant</option>
                    <option>Contract</option>
                    <option>Freelance</option>
                  </select>
                </div>
              </div>
              
                      
          
         
     <div class="col-md-3">
                <div class="formrow">
                  <select class="form-control select_filter" name="experience" >
                    <option value ='' disabled selected>Experience</option>
                    <option>Fresher</option>
                    <option>1 Year</option>
                    <option>2 Years</option>
                    <option>3 Years</option>
                    <option>4 Years</option>
                    <option>5 Years</option>
                    <option>6 Years</option>
                    <option>7 Years</option>
                    <option>8 Years</option>
                    <option>9 Years</option>
                    <option>10 Years</option>
                  </select>
                </div>
              </div>
</form>
 </div>


        
    
      <div class="row">
      <div class="col-xs-12">
          <div class="box box-danger">
            <div class="box-body table-responsive">
              <table id="example" class="table-bordered table-striped table-hover">
                <thead>
                <tr>
                  <th>S.No</th>
                  <th>Company name</th>
                 <th>Company email</th>
                  <th>Company mobile</th>
                    <th>Company address</th>
                     <th>Job title</th>
                      <th>Industry</th>
                       <th>Salary</th>
                        <th>Employment Type</th>
                         <th>No. Of Positions</th>
                          <th>Experience</th>
                     <th>Job Description</th>
                 <th>Status</th>
                  <th>Action</th>
                </tr>
                </thead>
                <tbody id="projects">
  </tbody>
</table>
</div>
 </div>
</div>
 </div>

ajaxCompany_search.php

<?php
include('../../config.php');
print_r($_POST);
?>

jQuery / Ajax 代码:-

 <script type="text/javascript" rel="stylesheet">
 $(document).ready(function(){

 $('.select_filter').on('change',function(){
       
      $.ajax({
           type: "POST",
           url: "ajaxCompany_search.php",
           data: $('#search_form').serialize(), // You will get all the select data..
            success:function(data){
    
         var data = $(data);
         datatable.clear().rows.add(data).draw();
             
    }
        });
  });
  });

  
      var datatable = $('#example').DataTable({
    dom: 'Bfrtip',
    buttons: [
      'copy', 'csv', 'excel', 'pdf', 'print'
    ]
  });

【问题讨论】:

  • 那个ajaxCompany_search.php 文件看起来不正确,它应该返回表格行数据,但它所做的只是回显已发布的参数。
  • ajaxCompany_search.php 这个 php 页面将动态数据提供给数据表。
  • @KIKO Software 先生,请帮忙。

标签: javascript php jquery ajax datatables


【解决方案1】:

在从 ajax 响应添加 HTML 时,您的数据表未绑定,您必须删除数据表 js 并将其重新分配给选择器,然后它才能正常工作。 或者在 ajax 发送响应的页面上使用数据表选择器创建整个表,然后用您当前的 HTML 替换整个表。

从这里获取参考 https://datatables.net/reference/option/ajax

【讨论】:

  • 如何删除数据表js并重新分配给选择器?
  • 您从 ajax ajaxCompany_search.php 获取数据并在您的主页上创建表,但是这样做会在 jax 页面 ajaxCompany_search.php 上创建整个数据表并在此处获取响应并更新html
  • 请给我示例代码 sn-p 请更新您的答案
猜你喜欢
  • 2021-05-12
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 2011-01-19
  • 2021-05-11
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
相关资源
最近更新 更多