简体   繁体   English

DataTable JQuery如何根据ID从表中删除行?

[英]DataTable JQuery How to remove a row from a table based on an ID?

I have a web application where you can drag and drop pictures into boxes. 我有一个Web应用程序,您可以将图片拖放到框中。 If you drop it in the one it will add the picture's information to the datatable, if you drop in the left it will remove the data from the datatable. 如果你将它放在一个中它会将图片的信息添加到数据表中,如果你向左下方它将从数据表中删除数据。 I was wondering If there was a way I could remove the row based on the id? 我想知道是否有一种方法可以根据ID删除行?

$('#pictures')
    .dataTable({
        "columnDefs": [{
                "orderable": false,
                "targets": 1
            },
            {
                "orderable": false,
                "targets": 3
            }
        ]
    });
var t = $('#pictures')
    .DataTable();


$("#left")
    .droppable({
        accept: ".draggable",
        drop: function (event, ui) {
            console.log("drop");
            $(this)
                .removeClass("border")
                .removeClass("over");
            var dropped = ui.draggable;
            var droppedOn = $(this);
            $(dropped)
                .detach()
                .css({
                    top: 0,
                    left: 0
                })
                .appendTo(droppedOn);

            var $id = $(this)
                .children()
                .last()
                .attr('id');
            var rowId = pictures[id].id;
            t.row(pictures[$id].id)
                .remove()
                .draw(false);
        }
    });

This obviously isn't the entire thing; 这显然不是整件事; however, I think this is enough to identify any problems. 但是,我认为这足以发现任何问题。

You can use DataTable's API to remove the corresponding row: 您可以使用DataTable的API删除相应的行:

t.row("your selector here").remove().draw();

in row() you could use several kind of selectors . row()你可以使用几种选择器 If you saved the row's id in a variable, simply use 如果您将行的id保存在变量中,只需使用

t.row("#"+rowId).remove().draw();

Note that you have to call draw() after a remove, since datatables doesn't redraw itself after a remove due to performance reasons. 请注意,您必须在删除后调用draw() ,因为由于性能原因,数据表在删除后不会重绘自身。

$('#pictures').DataTable().row("#YourRowId").remove().draw();

If the item you want to delete is the parent window; 如果要删除的项目是父窗口;

window.parent.$('#pictures').DataTable().row("#YourRowId").remove().draw();

If you want to delete all the rows; 如果要删除所有行;

$('#pictures').DataTable().row().remove().draw();

You should pay attention to the JS version. 你应该注意JS版本。

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

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