首页 > 解决方案 > 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'
        });
    });
});

标签: javascriptjquerydatatables

解决方案


如果您想通过 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 内部


推荐阅读