简体   繁体   English

在Jquery DataTable中添加Click函数到两个按钮

[英]Add Click Function To Two Buttons Inside Jquery DataTable

I have a jquery datatable with dynamically added buttons for edit and delete as below: 我有一个jquery数据表,动态添加按钮进行编辑和删除,如下所示:

这是我的数据表。

Below is my JS: 以下是我的JS:

 ajaxLoadSuccess: function (data) {
     var datatableVariable = $('#articleTable').DataTable({ 
         data: data,
         columns: [
             { 'data': 'Topic' },
             { 'data': 'SubTopic' },
             { 'data': 'Title' },
             //{ 'data': 'ParsedText' },
             {
                 'data': 'AddedOn', 'render': function (date) {
                      var date = new Date(parseInt(date.substr(6)));
                      var month = date.getMonth() + 1;
                      return date.getDate() + "/" + month + "/" + date.getFullYear();
                 }
             },
             { 'data': 'AddedBy' },
             {
                'data': 'Action', 'render': function (data, type, row, meta) {
                 return '<input id="btnEdit" type="button" value="Edit" class="sfBtn sfPrimaryBtn sfLocale" /> <input id="btnDelete" type="button" value="Delete" class="sfBtn sfPrimaryBtn sfLocale" />';
                 }
             }]
        });

And Below is my code for button click: 以下是我点击按钮的代码:

$("#articleTable tbody").on("click", "tr", function () {
    alert($(this).text());
});

The above function gets fired when I click Edit or Delete button, what I want is to add another function for delete button so that separate functions are fired when edit and delete is clicked. 当我单击编辑或删除按钮时,上面的函数被触发,我想要的是为删除按钮添加另一个函数,以便在单击编辑和删除时触发单独的函数。 How can I acheve that? 我该怎么做呢?

In this code you have triggered the click event to tr. 在此代码中,您已将click事件触发为tr。 Instead of triggering event to tr you can trigger click event to specific id. 您可以触发click事件到特定ID,而不是触发事件到tr。 Here you have #btnEdit and #btnDelete 这里有#btnEdit#btnDelete

so your code looks like 所以你的代码看起来像

$(document).on('click', '#btnEdit', function () {
    // Do something on edit
});

$(document).on('click', '#btnDelete', function () {
    // Do something on delete
});

I think you want like this 我想你想要这样

$("#articleTable tbody tr").on("click", "input", function () {
    if ($(this).attr('id') == "btnEdit") {
        fn_edit();
    }
    else {
        fn_delete();
    }
});

It seems like you want to perform the operation on specific row when the button of that row is clicked if that's the case then my suggestion would be access the button by giving the class to it because the approach you're using will generate multiple button with same ids and the html form cannot have elements with same ids. 看起来你想在单击该行的按钮时在特定行上执行操作,如果是这种情况,那么我的建议是通过给出类来访问该按钮,因为你正在使用的方法将生成多个按钮相同的ID和html表单不能包含具有相同ID的元素。

ajaxLoadSuccess: function (data) {
 var datatableVariable = $('#articleTable').DataTable({ 
     data: data,
     columns: [
         { 'data': 'Topic' },
         { 'data': 'SubTopic' },
         { 'data': 'Title' },
         //{ 'data': 'ParsedText' },
         {
             'data': 'AddedOn', 'render': function (date) {
                  var date = new Date(parseInt(date.substr(6)));
                  var month = date.getMonth() + 1;
                  return date.getDate() + "/" + month + "/" + date.getFullYear();
             }
         },
         { 'data': 'AddedBy' },
         {
            'data': 'Action', 'render': function (data, type, row, meta) {
             return '<input type="button" value="Edit" class="btnEdit sfBtn sfPrimaryBtn sfLocale" /> <input type="button" value="Delete" class="btnDelete sfBtn sfPrimaryBtn sfLocale" />';
             }
         }]
    });

now you can access the button using the below code: 现在您可以使用以下代码访问该按钮:

$("#articleTable tbody").on("click", ".btnEdit", function () {
                alert($(this).text());
            });

$("#articleTable tbody").on("click", ".btnDelete", function () {
                alert($(this).text());
            });

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

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