DataTables-如何从特定行获取单元格?

时间:2018-10-09 10:21:38

标签: javascript jquery datatables

每行都有一个ID,#row-(来自ajax的id)。 现在我想按ID选择一行,就可以使用它。

var row = verzondenTable.row('#row-' + k);

k =来自ajax的密钥。

每个td每列都有一个类,因此第一列有.td-subject类,第二列有.td-open

我想从特定的选定行中选择.td-open单元格并为其设置数据。

代码:

$().ready(function() {
    var verzondenTable = $('#tblVerzondenItems').DataTable({
        "order": [[0,'desc']],
        "columnDefs":[
            { "type": "date-nl", "targets": [ 'th-datum' ] },
            {
                sortable: false,
                targets: [6,7]
            }
            ],
        "initComplete": function(settings, json) {
            $.ajax({
                url : '/mail/feed/mailgun.json',
                type : 'GET',
                dataType:'json',
                success : function(data) {
                    $.each(data, function(k,v) {

                        var row = verzondenTable.row('#row-' + k);
                        verzondenTable.row('#row-' + k).cell('.td-open').data((v['open_rate'] * 100).toFixed(2) + '%');

                    });
                    $('#alert-mailgun').alert('close');
                },
                error : function(request,error)
                {
                    alert("Request: "+JSON.stringify(request));
                }
            });
        }
    });
    // loop over each element and create a tooltip using the data-attribute
    $('.count').each(function() {
        Tipped.create(this, {
            ajax: {
                data: $(this).data('querystring'),
                type: "POST"
            },
            maxWidth: 300,
            skin: 'dark'
        });
    });
});

3 个答案:

答案 0 :(得分:1)

如果您想使用API​​,可以执行类似的操作

var row = verzondenTable.row('#row-' + k);
row.nodes().to$().find('.td-open').text((v['open_rate'] * 100).toFixed(2) + '%');
row.draw().invalidate();

nodes()->获取所有节点
to$()->转换为jQuery实例
invalidate->更新DT内部结构

答案 1 :(得分:0)

如何使用CSS选择器:

$('[id^="row-"] td.td-open').text(your_data);//your_data is the value you want to set.

答案 2 :(得分:0)

尝试从参考文献https://datatables.net/reference/api/row().child()function row().child( data [, className ] )