在JQuery DataTable上编辑按钮

时间:2016-09-21 12:03:24

标签: javascript jquery asp.net

我正在使用JQuery DataTable显示数据,我想为该表中的每一行创建一个编辑按钮。

但是,关于该按钮,我有两个问题。

 1. Once I run the application, the Edit button will trigger automatically (which redirect to the new page).
 2. I want to get the information of the first column of the selected row, but what I got is undefined value.

以下是我正在使用的代码:

HTML:

<table id="tblMember">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Gender</th>
                    <th>Status</th>
                    <th>Account</th>
                    <th>Action</th>
                </tr>
            </thead>
        </table>

JS:

$('#tblMember').dataTable({
            bProcessing: true,
            bServerSide: true,
            iDisplayLength: 10,
            sAjaxSource: "~/MemberService.ashx",
            fnServerData: function (sSource, aoData, fnCallback) {
                aoData.push({ "name": "GroupAccount", "value": "Account" })

                $.ajax({
                    type: "POST",
                    data: aoData,
                    url: sSource,
                    dataType: "json",
                    success: function (msg) {
                        fnCallback(msg);

                        $("#tblMember").dataTable().show();
                    }
                });
            },
            columnDefs: [
                {
                    width: "10%",
                    className: "dt-body-center",
                    targets: -1,
                    data: "Name",
                    render: function (data, type, full, meta) {
                        return "<button onclick='" + GetSelectedData(data) + "'><i class='fa fa-pencil' aria-hidden='true'></i></button>";
                    }
                }
            ]
        });
    }

    function GetSelectedData(value) {
        window.location = "~/Registration.aspx?Name='" + value + "'";
    }

我缺少什么?

你的回答非常感谢。

谢谢。

1 个答案:

答案 0 :(得分:1)

当我有类似的要求时,我创建了具有属性columnDefs的按钮,但是在DataTable定义之外处理了它的点击调用。

我正在提供解决方案,假设您的表正在初始化,除了编辑按钮问题。

演示: https://jsfiddle.net/Prakash_Thete/j3cb2bfo/5/

在表格中添加编辑按钮:

"columnDefs": [
    {
        className: "dt-body-center",
        targets: -1,
        defaultContent: ["<i class='fa fa-pencil editButton' aria-hidden='true' aria-hidden='true'></i>"]

     }
 ]

处理编辑按钮的点击通话

假设

var memberTable = $('#tblMember').dataTable({

然后

$('body').on('click', '#tblMember tbody tr .editButton', function () {
    var rowData = memberTable.row( $(this).parents('tr')).data();
    console.log("Rows data : ",  rowData);

    //fetch first column data by key if you are passing map as input to table
    console.log("First column data : ", rowData[0]); 
});