单击jQuery DataTable中的编辑按钮后从数据库中获取id

时间:2017-03-11 20:07:39

标签: javascript jquery asp.net-mvc html5 datatables

当我单击某行的删除按钮时,我需要从数据库中获取该行的id,这样一旦获得该id就可以将其发送到控制器以删除该行。

我试图这样做。

这是我的HTML表格。

<table id="tableClient" class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>Delete</th>
            <th>Name</th>
            <th>Last Name</th>
            <th>RUC</th>
            <th>Phone</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>                                                
    </tbody>
</table>

使用jQuery DataTable我正在尝试这样做:

var table = $('#tableClient').DataTable({
    "columnDefs": [{
        "width": "5%",
        "targets": [0]
    },
    {
        "className": "text-center custom-middle-align",
        "targets": [1, 2, 3, 4, 5]
    }, ],
    "language":
    {
        "processing": "<div class='overlay custom-loader-background'><i class='fa fa-cog fa-spin custom-loader-color'></i></div>"
    },
    "processing": true,
    "serverSide": true,
    "ajax":
    {
        "url": "/Mantenimiento/FiltrarClientesTablaAsync",
        "type": "POST",
        "dataType": "JSON",
    },
    "columns": [
    {
        bSortable: false,
        mRender: function (o) { return '<button type="button" onclick="delete(id);" class="btn btn-default delete"><span class="glyphicon glyphicon-remove" /></button>'; }
    },
    {"data": "Name"},
    {"data": "LastName"},
    {"data": "RucClient"},
    {"data": "Phone"},
    {"data": "Email"}
    ],
});

事情就是当我点击删除按钮时我想调用onclick =“delete(id);” event并将id发送到将删除该id行的事件。 我怎么能用jQuery和MVC做到这一点?

2 个答案:

答案 0 :(得分:0)

我能够找到我需要的东西。 我只需要添加名为data-id的data-atributte和数据库的id,然后将其作为参考传递给&#34;这个&#34;删除方法。

这是我的解决方案:

x <- read.table(textConnection(content(myfile, encoding = "UTF-8")), header = FALSE)
x
#   V1 V2 V3
# 1  1  1  1
# 2  1  0 11
# 3  0  1  6
# 4  0  0  6

答案 1 :(得分:0)

您可以使用此方法将值传递给您编写的delete()函数。

{
    "data": null,
    "className": "class1 class2",
    "orderable": false,
    "render": function (data, type, row) {
        return '<button type="button" onclick="delete(' + data.Id + ');" class="btn btn-default delete"><span class="glyphicon glyphicon-remove" /></button>'; 
        // where data.Id is the Id of the entry in the row
    }
},