【问题标题】:Datatables - td additional attributes数据表 - td 附加属性
【发布时间】:2017-05-07 10:34:23
【问题描述】:

这是我第一次使用数据表,我正在逐渐习惯它。

我找到了一种方便的方法来传递附加属性或修改从服务器检索的源数据(我使用来自服务器和客户端渲染的 json 播种)。

代码:

    "columnDefs": [{
    "targets": "_all",
    "createdCell": function (td, cellData, rowData, rowIndex, colIndex) {



        //code column
        if (colIndex == 0) {
            cellData = '<input value="' + cellData + '">';
            $(td).html(cellData);

        }

       //expire column
        if (colIndex == 3) {
            $(td).html('4');
        }

          // colouring all columns with index less than 3 as red
        if (colIndex < 3) {
            $(td).css('color', 'red')
        }
    }
}]

我觉得有点脏,因为我使用索引来区分数据。

以后我需要为这样的代码还清吗?会有什么后果吗?

是否有任何适当且同时舒适的方式来达到相同的目标?

我还注意到“createdRow”属性,并以与使用 createdCell 相同的方式使用它

【问题讨论】:

    标签: javascript jquery datatables


    【解决方案1】:

    阅读源代码后发现:

                            columns: [
    
                                {
                                    title: "Код",
                                    data: "codeSupplier",
    
                                    className: 'codeSupplier',
                                    orderable: false,
                                    searchable: false,
    
                                    render: function(data){
                                        return '5' + data;
                                    }
    
                                },
    

    看起来更漂亮

    所以应该使用函数渲染

    【讨论】:

    • 是的。一般来说,如果你想自定义&lt;td&gt;,你应该使用createdCell(),如果你想自定义内容,你应该使用render()。您可以将两者结合使用,然后将两者用于各自的目的:)
    猜你喜欢
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2014-08-25
    相关资源
    最近更新 更多