jQuery 数据表将类添加到 tr

新手上路,请多包涵

我正在使用 jQuery 和数据表。我想将一个类添加到特定行的 TR 元素。我知道如何找到该行。 console.dir(row); 显示 row 对象,它以 tr 元素开头。我无法让 jQuery 选择器执行任何操作。我错过了什么?

 table = $('#resultTable').DataTable({
    aaSorting: [],
    ajax: {...},
    columnDefs: [...],
    createdRow: function (row, data, index) {
        //
        // if the second column cell is blank apply special formatting
        //
        if (data[1] == "") {
            console.dir(row);
            $('tr', row).addClass('label-warning');
        }
    }
});

原文由 7 Reeds 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 477
2 个回答

$('tr', row) 正在行的上下文中寻找 tr 元素,这意味着它将 作为上下文参数提供的 row 中搜索 tr 元素。

根据 API ,这应该有效

$(row).addClass("label-warning");

原文由 Ozan 发布,翻译遵循 CC BY-SA 3.0 许可协议

您只需要使用 createdRow

 $('#data-table').DataTable( {
    createdRow: function( row, data, dataIndex ) {
        // Set the data-status attribute, and add a class
        $( row ).find('td:eq(0)')
            .attr('data-status', data.status ? 'locked' : 'unlocked')
            .addClass('asset-context box');
    }
} );

原文由 Zymawy 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题