简体   繁体   English

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

[英]DataTables - How to get cell from specific row?

Every row has an ID, #row-(id from ajax).每行都有一个 ID,#row-(id from ajax)。 Now I want to select an row by id, I got this to work.现在我想通过 id 选择一行,我让它工作了。

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

k = the key from ajax. k = 来自 ajax 的密钥。

Every td has a class per column so the first column has the class .td-subject and the second one has .td-open .每个 td 每列都有一个类,所以第一列有类.td-subject ,第二列有.td-open

I want to select the .td-open cell from the specific selected row and set the data for it.我想从特定的选定行中选择.td-open单元格并为其设置数据。

Code:代码:

$().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'
        });
    });
});

If you want to go through the API you can do something like如果您想通过 API,您可以执行以下操作

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

nodes() -> get all nodes nodes() -> 获取所有节点
to$() -> convert to jQuery instance to$() -> 转换为 jQuery 实例
invalidate -> update DT internals invalidate -> 更新 DT 内部

如何使用 CSS 选择器:

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

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

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

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