在ajax数据加载时,Jquery Datatable排序不起作用

时间:2015-11-02 07:49:54

标签: javascript php jquery ajax datatables

Jquery数据表排序不适用于ajax数据加载

index.php

<div> Select Department :
  <select  name='st'  onChange="selectdep(this.value)" >
    <option value="">Choose option</option>
    <?php
     $query_depname = mysqli_query($con,"select d.department_id,d.dept_short_name from t_dep d,t_user e where e.user_user_name like '$user_name' and d.dept_status = 'A'" );
      $rows_depname = mysqli_num_rows($query_depname);
      if ($rows_depname > 0) {
    ?>
    <?php
       while($dep_names = mysqli_fetch_assoc($query_depname)) {
    ?>
      <option value="<?php echo $dep_names["department_id"];  ?>">
      <?php
    echo $dep_names["dept_short_name"];
    ?>
    </option>
    <?php
      }
    }
    ?>
  </select>
</div>
<span id="txtHint"></span>

当用户从上面的选择选项中选择部门时,表格数据将根据部门选择进行更新

ajax call script 

<script type="text/javascript">
  function selectdep(str) { 
    if (str=="") {
      document.getElementById("txtHint").innerHTML="";
      return;
    }
    if (window.XMLHttpRequest) {
      xmlhttp=new XMLHttpRequest();
    } else { 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
      xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
      }
    }
    xmlhttp.open("GET","data.php?st="+str,true);
    xmlhttp.send();
  }
</script> 

Bellow data.php

   <table cellpadding="0" cellspacing="0" border="0" class="stdtable" id="emptable">
     <colgroup>
       <col class="con0" />
       <col class="con1" />
       <col class="con0" />
       <col class="con1" />
       <col class="con0" />
       <col class="con1" />
     </colgroup>
     <thead>
       <tr>
         <th class="head0">S.No</th>
         <th class="head1">First Name</th>
         <th class="head0">Last Name</th>
         <th class="head1">Gender</th>
         <th class="head0">Mobile</th>
         <th class="head1">Department</th>
         <th class="head0">Designation</th>
         <th class="head1 no-sort">Edit</th>
       </tr>
     </thead>
     <tfoot>
       <tr>
         <th class="head0">S.No</th>
         <th class="head1">First Name</th>
         <th class="head0">Last Name</th>
         <th class="head1">Gender</th>
         <th class="head0">Mobile Number</th>
         <th class="head1">Department</th>
         <th class="head0">Designation</th>
         <th class="head1">Edit</th>
       </tr>
    </tfoot>
    <tbody>
     <?php 
       session_start();
       $st =$_GET['st'];
      include_once("includes/dbConnect.inc.php"); 
      $sql= mysqli_query($con,"SELECT * FROM t_emp e, t_account a , t_addr ad, t_dep d where e.employee_id = a.acc_employee_id and e.employee_id = ad.addr_employee_id and e.emp_department_id =d.department_id and e.emp_department_id like '%".$st."%' ");
      $select_dep= mysqli_num_rows($sql);
      if ($select_dep > 0){
        $i=1;
        while($select_dep = mysqli_fetch_assoc($sql)){
          echo "<tr>";
          echo "<td>".$i++."</td>
          <td>".$select_dep["emp_first_name"]."".$select_dep["emp_middle_name"]."</td>
          <td> ".$select_dep["emp_last_name"]."</td>
          <td> ".$select_dep["emp_gender"]."</td>
          <td> ".$select_dep["emp_mobile_num"]."</td>
          <td> ".$select_dep["dept_short_name"]."</td>
          <td> ".$select_dep["emp_experience"]."</td>
          <td><a class='edit_link' href='employee_edit.php?id=$select_dep[e_id]''></a></td> ";
          echo "</tr>";
        } 
      } 
      else {
        echo "0 Results";
      }
     ?>
   </tbody>
 </table>

我包含了所有必需的jquery文件。如果我只是静态表排序和分页工作完美的地方,当我使用上面的代码只是表显示没有排序和分页。 任何人都可以帮我解决这个问题,任何建议将不胜感激,提前谢谢

0 个答案:

没有答案