简体   繁体   English

jQuery click click事件不适用于数据表

[英]jQuery clicking click event not working on Datatables

Hey all I have a unique issue that I can not seem to find a way to fixing. 嘿,我有一个独特的问题,我似乎找不到解决方法。

This is what my data table looks like: 这是我的数据表的样子:

在此处输入图片说明 在此处输入图片说明

I've added the red trashcan that you currently see in the image above in order for the user to delete that row. 我添加了您当前在上图中看到的红色垃圾桶,以便用户删除该行。 I add this trashcan by injecting it within the datatables createdRow : 我通过将其注入到createdRow数据表中来添加此垃圾桶:

fnRowCallback: function (nRow, aData, iDisplayIndex) {
   $(nRow).find('#trashIcon').attr({ 'data-indexer': (iDisplayIndex + 1) });
   $(nRow).find('#trashIcon').attr({ 'data-table': 1 });
},
createdRow: function (row, data, dataIndex) {    
   $(row).prepend('<i id="trashIcon" ' +
           'data-eventid="' + data.RequestID + '" ' +
                  'class="fa fa-trash-o fa-lg grow-1" ' +
            'aria-hidden="true" ' +
                  'style="color: rgba(169, 68, 66, 1); ' +
                         'left: 3px; top: 10px; ' +
                         'position: relative; ' +
                         'z-index: 500; ' +
                         'box-shadow: 0px 2px 2px rgba(150,150,150,0.3);">' +
                  '</i>');
},

And after all that's said and done with the injections the HTML code looks like: 说完这些并完成注入后,HTML代码看起来像:

<table width="100%" 
       class="display cell-border dataTable no-footer" 
       id="theDT2" 
       role="grid" 
       aria-describedby="theDT2_info" 
       style="width: 100%;" 
       cellspacing="0">
<thead>
    [HEADER CODE HERE...]
</thead>
<tbody>
    <tr class="odd" role="row">
        <i class="fa fa-trash-o fa-lg grow-1" 
           id="trashIcon" 
           aria-hidden="true" 
           style="left: 3px; 
                               top: 10px; 
                             color: rgba(169, 68, 66, 1); 
                          position: relative; 
                        box-shadow: 0px 2px 2px rgba(150,150,150,0.3);" 
           data-eventid="976" 
           data-indexer="1" 
           data-table="2">
        </i>
        <td class="sorting_1">976</td>
        [TABLE CODE HERE]
    </tr>
</table>

id="trashIcon" is the trashcan image you see in the rows. id =“ trashIcon”是您在行中看到的垃圾桶图像。 As you see I am setting trashcanHover to true on the trashcan click event but that doesn't seem to pass fast enough to the datatables click even when I console.log that out - it always says false. 正如你看到的我设置trashcanHover到真正的垃圾桶点击事件,但似乎并不足够快时,我CONSOLE.LOG说出来的数据表点击,甚至通过 -它总是说假的。

The jQuery code I have for clicking on the trashcan is this: 我要单击垃圾箱的jQuery代码是这样的:

$(document).on('click', '#trashIcon', function (e) {
    e.preventDefault();
    trashcanHover = true;
});

And this is the jQuery code for the clicking of any part of the datatables row: 这是单击datatables行的任何部分的jQuery代码:

$(document.body).on('click', 'tr', function (e) {
    //Listens for the user to click on a data row in the table
    if (typeof $(this).attr('class') != 'undefined') {
        var tblName = $(this).closest('table').attr("ID");
        var headerClick = $(this).attr("aria-controls");

        console.log(trashcanHover);

        if (typeof $(this).attr('class') != 'undefined') {
            if (tblName == "theDT") {
                edit_person(theTable.row(this).data().RequestID);
            } else {
                edit_event(theTable2.row(this).data().EventID);
            }
        } else {
            if ($(this).data('table') == 1) {
                $('#theDT').dataTable().fnDeleteRow($(this).closest('tr')[0]);
            } else {
                $('#theDT2').dataTable().fnDeleteRow($(this).closest('tr')[0]);
            }
        }
    }
});

Now if I comment out either one of those click functions then it does what it should do (either open data to edit or delete that row). 现在,如果我注释掉其中一个单击函数,则它将执行应做的事情(打开数据以编辑或删除该行)。 What I am having issues with is that it never fully sees the trashcan click (even though I click on it with the mouse). 我遇到的问题是,它从未完全看到垃圾桶的单击(即使我用鼠标单击了它)。 It just fires off the datatables click row function and I can not find a way to distinguish between the two of them in order to do an IF THAN. 它只是触发了datatables click row函数,我找不到一种方法来区分两者以进行IF THAN。

The code above does work with checking if just the headers were clicked on and if so, don't edit that row's data. 上面的代码确实可以检查是否仅单击了标题,如果不单击,则不要编辑该行的数据。

Currently though when I click on the trashcan it does delete that row but also opens the edit for that row that just got deleted. 目前,虽然当我单击垃圾箱时,它确实删除了该行,但也打开了刚删除的那一行的编辑

for Louys Patrice Bessette 适用于Louys Patrice Bessette

在此处输入图片说明

Seems to always goto the datatable's click event first... 似乎总是总是先进入数据表的click事件...

Use a class to target elements that are created via a loop. 使用一个class来定位通过循环创建的元素。
Because an id must be unique. 因为id 必须是唯一的。

I assume this is an immediate delete row function... 我认为这是一个立即删除行功能...
So this should work: 所以这应该工作:

$(document).on('click', '.fa-trash-o', function (e) {
    // To stop the click propagation up to the `tr` handler
    e.stopPropagation();

    // Delete the row.
    $('#theDT2').dataTable().fnDeleteRow($(this).closest('tr')[0]);
});




EDIT 编辑

okay... Let's try something else then. 好吧...那我们再试试

In the tr handler, add: tr处理程序中,添加:

 $(document.body).on('click', 'tr', function (e) { if(e.target.className.split(" ")[1]=="fa-trash-o"){ return; } // rest of your code... 

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

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