简体   繁体   English

将点击事件添加到数据表单元格

[英]Add click event to datatable cell

I'm trying to add a click event to a dynamically created a datatabe.我正在尝试向动态创建的数据表添加点击事件。 So this is my js :所以这是我的 js :

   $('#report-table').dataTable({
        "proccessing": true,
        "serverSide": true,
        "ajax": {
            url: '@Url.Action("Get","Controller")',
            type: 'GET'

        },
       "columns": [
           { "data": "Name" },
                { "data": "Date" }, //lest say I want to add a click event here ???
                { "data": "sName", render: myRender }// I tried render but this is rendered without waiting on click function
        ]

    });

and this is my table for example :这是我的表格,例如:

<tr role="row" class="odd">
<td class="sorting_1">A Test</td>
<td class="aaaa">2/13/2020 3:34:40 PM</td> //lets say I want to fire a click event when I click the .aaaa class
<td>
    <a  asp-route-fileid="55555555" asp-action="Download5"><img src="img/download.svg" height="30" width="30"></a>
</td>

Let's say I want to fire a click event when I click the .aaaa class.假设我想在单击 .aaaa 类时触发单击事件。

Can anyone please give me an idea?任何人都可以给我一个想法吗? Can I do this?我可以这样做吗? I'm stucked right here.我被困在这里。 Any idea would be apprecciated very very much :)任何想法都会非常感谢:)

Try this, for more check this forum :试试这个,更多检查这个论坛

$('#report-table').on('click', 'td.aaaa', function (e) {
        // your code to do something
    )};

Instead of render use className to add a class to the cell而不是render使用className向单元格添加一个类

   "columns": [
        { "data": "Name" },
        { 
            "data": "Date",
            "className": "your-class"
        },
        { "data": "sName" }
   ]

then put an on click event handler on your class然后在你的班级上放置一个点击事件处理程序

$('#report-table').on('click', '.your-class', function() {
    //function code here
}
$("tr[role='row']").on('click',()=> {
    alert($(this).find('td.aaaa').text());
});

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

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