简体   繁体   English

如何仅使用数据表中的现有数据重绘使用服务器端处理的数据表?

[英]How to redraw a Datatable that uses server side processing using only the existing data in the datatable?

I'm trying to delete a row from a Datatable that uses server side processing (removing the row data and the row/tr visually) based on the value of certain attribute of the row.我正在尝试根据行的某些属性的值从使用服务器端处理(视觉删除行数据和行/tr)的数据表中删除一行。

I'm using the remove() function to do it and it removes the row data, but visually the table stills the same.我正在使用remove() function 来执行此操作,它会删除行数据,但在视觉上表格仍然相同。

So I added the draw() function but it reinitializes the table, including the data.所以我添加了draw() function 但它重新初始化了表格,包括数据。

So, how can I "redraw" the table after removing a row from the Datatable that uses server side processing?那么,在从使用服务器端处理的数据表中删除一行后,如何“重绘”表? Is there any other function like draw() to redraw the table but using only the existing data in the datatable?是否有任何其他 function 像draw()来重绘表格但仅使用数据表中的现有数据?

 $("#tableSelector").DataTable()
     .rows( function ( idx, data, node ) {
         return data.attribute_value == value_to_delete;
     } )
     .remove()
     .draw();

Finally, I found the solution.最后,我找到了解决方案。 I followed this thread then read about the DataTable's property "dataSrc" .我关注了这个线程,然后阅读了有关 DataTable 的属性"dataSrc"的信息。
Then with this information searched for something similar in Stack Overflow and found this .然后使用此信息在 Stack Overflow 中搜索类似的内容并找到.
So the trick to do what I asked for was to use a variable to filter the row which I want to ignore in the ajax call.所以做我要求的技巧是使用一个变量来过滤我想在 ajax 调用中忽略的行。

I've reused the code provided in the referred question .我重用了引用问题中提供的代码。 And this is the result:这是结果:

HTML: HTML:

<table id="example" class="display" cellspacing="0" width="100%">
     <thead>
         <tr>
           <th>Name</th>
           <th>Email</th>
           <th>Subject</th>
           <th>Status</th>
           <th>Message</th>
           <th>Details</th>
         </tr>
        </thead>
 </table>


JS: JS:

$(document).ready(function() {
    function loadDataTable(excluded_name = undefined){
        $("#example").DataTable().destroy();
      $('#example').DataTable({
         // "processing" : true,
          "ajax" : {
              "url" : "https://api.myjson.com/bins/12uwp2",
               "dataSrc": (json) => {
               if(excluded_name !== undefined){
                return json.filter((item) => item.name !== excluded_name)
               }else{
                return json
               }
              }
          },
          "columns" : [ {
              "data" : "name"
          }, {
              "data" : "email"
          }, {
              "data" : "subject"
          }, {
              "data" : "status"
          },{
              "data" : "message"
          },
          {
             "mData": "Details",
             "mRender": function (data, type, row) {
                      return "<a class='delete' data-name='"+ row.name + "' data-id=" + row.id + " href='/Details/" + row.id + "'>Delete</a>";
                  }
          }]
      });
    }
    $(document).on("click", ".delete", function(e) {
        e.preventDefault()
        let excluded_name = $(this).data("name")
        alert("Excluding this name in the next load: " + excluded_name);
        loadDataTable(excluded_name);
    })
    loadDataTable();
});

Fiddle: https://jsfiddle.net/ek94mh1x/4/小提琴: https://jsfiddle.net/ek94mh1x/4/

Note: This is a client side "delete", I know this could be done with server side processing, but in my case I was only allowed to do it in the client side.注意:这是客户端“删除”,我知道这可以通过服务器端处理来完成,但就我而言,我只被允许在客户端进行。
Additionally, in case you need to delete multiple rows you could use an array as a parameter and follow the same logic.此外,如果您需要删除多行,您可以使用数组作为参数并遵循相同的逻辑。
I hope this could help someone.我希望这可以帮助某人。

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

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