繁体   English   中英

如何使用jQuery Ajax重新加载数据表

[英]how to reload the datatable using jquery ajax

这是我的桌子

    <table class="table table-hover display search dataTable" id="test" cellspacing="0" >
  <thead>
    <tr>
      <th> ID </th>
      <th>Name</th>
      <th>Age</th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody >
    <% all_employees.each do |employee|%>
      <tr>
        <td> <%= check_box_tag 'employee_ids[]', employee.id %> </td>
        <td> <%= employee.name %> </td>
        <td> <%= employee.age %> </td>
        <td> <%= employee.email %> </td>
      </tr>
    <% end %>
  </tbody>
</table>  

和我的js

function filtered_employee(group_id){
    $.ajax({
      url: "/employee_groups/"+group_id+"/associated_employees",
      type: "get",      
      data: {
        "id" : group_id
      },
      success: function(result) { 
        $('#test').html(result)
      },
      error: function(result){
        $('#test').html(result.responseText)
      }
    });
}

我正在使用Ruby on Rails,并在按钮上单击rails 4版本,将其称为ajax req,获得了预期的响应,但是当在ajax中触发成功时,我得到了一张旧表上的新表,如何解决此问题?

提前致谢

我建议将<table>放在其他容器中,并在ajax成功时刷新该容器,如下所示:

<div class="tableWrapper">
    <table class="table table-hover display search dataTable" id="test" cellspacing="0" >
    ....
    </table>
</div>

和JS代码:

function filtered_employee(group_id){
    $.ajax({
      url: "/employee_groups/"+group_id+"/associated_employees",
      type: "get",      
      data: {
        "id" : group_id
      },
      success: function(result) { 
        $('.tableWrapper').html(result)
      },
      error: function(result){
        $('.tableWrapper').html(result.responseText)
      }
    });
}

暂无
暂无

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

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