Tebe
Tebe

Reputation: 3214

Datatables - td additional attributes

This is my first time I use datatables and I'm on the way of getting accustomed to it.

I found a convenient way to pass additional attributes or modify source data retrieved from server (I use seeding with json from server and client side rendering).

The code:

    "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')
        }
    }
}]

It smells a bit dirty to me, because I use indexes for distinguishing the data.

Will I have to pay off later for such code? Will there be any consequences?

Is there any proper and in the same time comfy way to reach the same?

Also I noticed "createdRow" property and going to use it the same way I use createdCell

Upvotes: 0

Views: 146

Answers (1)

Tebe
Tebe

Reputation: 3214

After reading source code I found this:

                        columns: [

                            {
                                title: "Код",
                                data: "codeSupplier",

                                className: 'codeSupplier',
                                orderable: false,
                                searchable: false,

                                render: function(data){
                                    return '5' + data;
                                }

                            },

Looks much niftier

So function render should be used

Upvotes: 1

Related Questions