简体   繁体   English

onview函数用于jquery DataTable中的按钮

[英]onClick function for button inside jquery DataTable

I have a button for each row inside my jquery DataTable. 我的jquery DataTable中的每一行都有一个按钮。 I want when I click the button to alert the value of the first columns of the selected record. 我想在单击按钮时提醒所选记录的第一列的值。

The problem is that, with the code I have for the button, it does not work. 问题是,使用我对按钮的代码,它不起作用。

This is the documentation that I have followed: https://datatables.net/examples/ajax/null_data_source.html 这是我遵循的文档: https//datatables.net/examples/ajax/null_data_source.html

Here is my code: 这是我的代码:

jQuery jQuery的

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "WebService1.asmx/Bind",
        dataType: 'json',
        success: function (data) {
            $('#datatable').DataTable({
                data: data,
                columns: [
                    { 'data': 'CenterID' },
                    { 'data': 'CenterName' },
                    { 'data': 'LicenseKey' },
                    { 'data': 'ExpiryDate' },
                    { 'data': 'YearID' },
                    { 'data': 'Date' },
                    // This is where I add the button
                    { "defaultContent": "<button>Edit</button>"}
                ]
            });
        }

    });
    //This is where I make an attempt to add functionality for the button in the datatable
    $('#datatable tbody').on('click', 'button', function () {
        var data = table.row($(this).parents('tr')).data();
        alert("The value is: " + data[0]);
    });
});

HTML HTML

<table id="datatable" class="display" style="width: 100%">
            <thead>
                <tr>
                    <th>CenterID</th>
                    <th>CenterName</th>
                    <th>LicenseKey</th>
                    <th>ExpiryDate</th>
                    <th>YearID</th>
                    <th>Date</th>
                    <th>Action</th>
                </tr>
            </thead>
        </table>

Please assist. 请协助。 Thank you. 谢谢。

So after a lot of research, I got it. 经过大量的研究,我得到了它。 I had to change the button function. 我不得不改变按钮功能。

$('#datatable tbody').on( 'click', 'button', function () {
     var tr = $(this).closest('tr');
     var value = $(tr).find('td').eq(0)[0].innerHTML;
     alert(value);
     return false;
});

If anyone has a better way of doing this, please let me know. 如果有人有更好的方法,请告诉我。

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

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