简体   繁体   English

jQuery:如何将onClick事件绑定到DataTable行?

[英]jQuery : How to bind an onClick Event to a DataTable row?

Currently, the dataTable is populated via the server side and everything is working. 当前,dataTable是通过服务器端填充的,一切正常。 But I want to add Details|Edit|Delete actionLinks when a row is clicked. 但是我想在单击一行时添加Details | Edit | Delete actionLinks。

Right now I have them showing in a column at the right side, but I want the links to appear when the user clicks on each row and I cannot workout how to implement it to show onClick . 现在,我将它们显示在右侧的一列中,但是我希望当用户单击每一行时显示链接,而我无法锻炼如何实现显示onClick

Can someone please assist me in getting them to show on click? 有人可以协助我让他们显示点击吗? Thank you. 谢谢。

var dt = $('#datatableServer').DataTable({
            "serverSide": true,
            "ajax":
            {
                "type": "POST",
                "url": "@Url.Action("DataHandler", "Department")"
                    },
                    "rowId": 'departmentID',
                    //"fnRowCallback": function (nRow, aData, iDisplayIndex) {
                    //    nRow.setAttribute('id', aData[0]);
                    //},
                    "columns":
                    [
                        {
                            "data": "Name",
                            "searchable": true
                        },
                        {
                            "data": "Budget",
                            "searchable": false
                        },
                        {
                            "data": "StartDate",
                            "searchable": false
                        },
                        {
                            "data": "Administrator",
                            "searchable": true,
                            "orderable": false
                        },
      {
// this is the Actions Column I want to show when a Datatable row is clicked, not under a "Action" column
              mRender: function (data, type, row) {
             var linkEdit = '@Html.ActionLink("Edit", "Edit", new {id= -1 })';
                                linkEdit = linkEdit.replace("-1", row.DT_RowId);

             var linkDetails = '@Html.ActionLink("Details", "Details", new {id= -1 })';
                                linkDetails = linkDetails.replace("-1", row.DT_RowId);

             var linkDelete = '@Html.ActionLink("Delete", "Delete", new {id= -1 })';
                                linkDelete = linkDelete.replace("-1", row.DT_RowId);

                   return linkDetails + " | " + linkEdit + " | " + linkDelete;
            }
         }
      ],
      "order": [0, "asc"],
      "lengthMenu": [[10, 25, 50, 100], [10, 25, 50, 100]]
   });


        $('#datatableServer tbody').on('click', 'tr', function () {

            console.log('clicked');
            // get the row Id 
            console.log(dt.row(this).data().DT_RowId);
        });
    }); // end of document.ready tag

I made my mRender function a separate function and then called it in the click event function for the datatable body. 我将mRender函数mRender一个单独的函数,然后在数据表主体的click事件函数中对其进行了调用。

function format (data, type, row) {
                 var linkEdit = '@Html.ActionLink("Edit", "Edit", new {id= -1 })';
                                    linkEdit = linkEdit.replace("-1", row.DT_RowId);  



             var linkDelete = '@Html.ActionLink("Delete", "Delete", new {id= -1 })';
                                linkDelete = linkDelete.replace("-1", row.DT_RowId);

                   return linkEdit + " | " + linkDelete;
            }

$('#dtServer tbody').on('click', 'td', function () {

            var tr = $(this).closest('tr');
            var row = dt.row(tr);

            if (row.child.isShown()) {
                // This row is already open - close it
                row.child.hide();
                tr.removeClass('shown');
            }
            else {
                // Open this row
                row.child(format(row.data())).show();
                tr.addClass('shown');
            }
        });

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

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