简体   繁体   English

如何在不刷新页面的情况下刷新数据表?

[英]How can I refresh datatables without page refresh?

I want to refresh the datatable after adding, removing and updating.我想在添加、删除和更新后刷新数据表。 Can someone help ?有人可以帮忙吗? I put the select room and html parts following.我把选择房间和 html 部分放在下面。 I also have separate ajax code snippets that I do update adding and deleting.我也有单独的 ajax 代码片段,我会更新添加和删除。 I want to refresh the table after adding updating and removing.我想在添加更新和删除后刷新表格。

//HTML
<table id="datatables" class="table table-striped table-no-bordered table-bigboy" cellspacing="0" style="width:100%;">
     <thead>
         <tr>
            <th class="disabled-sorting">Room Plan</th>
            <th>Name</th>
            <th class="disabled-sorting text-right">Actions</th>
         </tr>
     </thead>
     <tfoot>
     <tr>
          <th>Room Plan</th>
          <th>Name</th>
          <th class="text-right">Actions</th>
     </tr>
      </tfoot>
        <tbody id="colmn">
            @*  *@

        </tbody>
</table>

//-------------------------SELECT ROOM ----------------------------

$.post("/Home/selectRooms", {}, function (data) {

    var ndx = 0;
    $.each(data.xroom_name, function (key, value) {

        var Xroom_name = data.xroom_name[ndx];
        var Xroom_plan = data.xroom_plan[ndx];

        var column =
          ('<tr>' +
            '<td>' +
            '<div class="img-container">' +
            '<img src="../../assets/img/room-plan/' + Xroom_plan + '" alt="..." id="imgsrc">' +
            '</div>' +
            '</td>' +
            '<td id="imgname">' + Xroom_name + '</td>' +
            '<td class="text-right">' +
            '<a href="#" class="btn btn-simple btn-warning btn-icon edit" data-toggle="modal" data-target="#myModal"><i class="fa fa-edit"></i></a>' +
            '<a href="#" class="btn btn-simple btn-danger btn-icon remove" ><i class="fa fa-times"></i></a>' +
            '</td>' +
            '</tr>');

        document.getElementById('colmn').innerHTML = document.getElementById('colmn').innerHTML + column;

        ndx++;

    });
});

How are you building your table?你是怎么搭建你的桌子的? Is it dynamic?是动态的吗?

If so, after your Ajax call returning a success response, you can update your object that is being used to build the table or even call the function that is building it.如果是这样,在您的 Ajax 调用返回成功响应之后,您可以更新用于构建表的对象,甚至调用正在构建它的函数。

If it's not dynamic, you can simply manipulate the DOM to add a new cell, to change the innerHTML of a certain cell or remove a particular cell from the table.如果它不是动态的,您可以简单地操作 DOM 以添加新单元格、更改特定单元格的 innerHTML 或从表格中删除特定单元格。

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

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