简体   繁体   中英

DataTables - How to get cell from specific row?

Every row has an ID, #row-(id from ajax). Now I want to select an row by id, I got this to work.

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

k = the key from ajax.

Every td has a class per column so the first column has the class .td-subject and the second one has .td-open .

I want to select the .td-open cell from the specific selected row and set the data for it.

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

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
to$() -> convert to jQuery instance
invalidate -> update DT internals

如何使用 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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