如何单击行表并提醒特定单元格值

时间:2015-02-22 21:06:19

标签: javascript jquery datatable

我有这个脚本,我正在寻找的是点击任何一行并提醒" DT_rowID"细胞价值。目前错误消息是:未捕获的ReferenceError:表未定义

$(document).ready(function() {

    $("body").append('<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"><tbody></tbody></table>');
    var string_id = "http://127.0.0.1:8080/get_data_datatable.php?q=";

    $.ajax({
        url: 'http://127.0.0.1:8080/search.php?q=exosome',

        dataType: 'json',
        type: 'get',
        async: true,
        success: function(data) {


            jQuery.each(data.ids, function(i, val) {
                string_id = string_id + val + ",";
            });

            var table = $('#example').dataTable({

                "ajax": string_id,
                "columns": [{
                    "title": "Product name",
                    "data": "product_name"
                }, {
                    "title": "Quantity",
                    "data": "product_quantity"
                }, {
                    "title": "Price",
                    "data": "price_sell"
                }, {
                    "title": "Currency",
                    "data": "currency_sell"
                }, {
                    "title": "Category",
                    "data": "category"
                }, {
                    "title": "Supplier",
                    "data": "supplier_id"
                }, {
                    "title": "DT_rowID",
                    "data": "product_version_id",
                    "bVisible": false
                }]
            });

        }
    });


    $("#example").css('cursor', 'hand');
    $('#example').on('click', 'tr', function() {
        alert(table.cell(this).data());
    });

});

1 个答案:

答案 0 :(得分:0)

只要该列仍然是表中的最后一列,这将起作用:

// Added 'tbody' to prevent alert from firing when header row is clicked
$('#example tbody').on('click', 'tr', function() {
    // $(this) is the <tr> element the user clicked
    // td:last finds the last cell within that <tr>
    alert($(this).find('td:last').text());
});

jsFiddle:http://jsfiddle.net/th708qf9/1/