繁体   English   中英

获取所选数据表行的 id 并将其传递给多行删除函数

[英]get the id of selected datatable row and pass it through a function for multiple row deletion

我得到了这段代码,几乎可以完成我想要的

<script> $(document).ready(function() {
    var table=$('#dataTable').DataTable();
    $('#dataTable tbody').on( 'click', 'tr', function () {
        $(this).toggleClass('selected');
    }
    );
    $('#button').click( function () {
        alert( table.rows('.selected').data().length +' row(s) selected');
    }
    );
}

);
</script>
<button type="button" id="button">select all</button>

它显示在 alert(); 选择了多少行。 但我想要它做的是获取表的第一列中的 id 并通过此函数将其传递以进行删除

function confirmRemove(id) {
    if (id) {
        $.ajax( {
            url: "product-delete.php", type: "post", data: {
                id_delete: id
            }
            , success:function(response) {
                var productTable=$("#dataTable").DataTable();
                productTable.ajax.reload(null, false);
                swal( {
                    title: "SUCCESS!", text: "Product Deleted!", icon: "success", timer: 1800, button: false,
                }
                );
            }
            , error: function(jqXHR, textStatus, errorThrown) {
                console.log(errorThrown);
            }
        }
        );
        return false;
    }
}

您必须遍历selected行并获取第一列并将其传递给您的confirmRemove函数。 下面的代码是这样做的:

$('#button').click(function () {

     var data = table.rows('.selected').data();
     data.each(function (value, index) {
     //call the delete function
     confirmRemove(value[0]);
     });
}

暂无
暂无

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

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