Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi -

I am using the jquery datatables. What I'm trying to do is adjust a column css based on another column if it contains an @ symbol. So here is the code that i've tried and cant seem to get working. Here is how the table is rendered.

JavaScript
var table = $("#datatable").DataTable({
    searching:false,
    data: data,
    columns: [
        {
           'className': 'details-control',
           'data': null,
           'defaultContent': ''
        },
        {
            'data': 'id',
            'render': function (id) {
                return '<a href=' + 'Default_XXXXX.aspx?XXXX='+ id + '>' + id+'</a>'
            }
        },
        { 'data': 'orderId' },
        { 'data': 'description'},
        { 'data': 'machineType' },
        { 'data': 'manufacturer' },
        {
            'className': 'valid-id',
            'data': null,
            'defaultContent': '',
            'sortable': false,
            'render': function (id) {
                var validId = $(this).closest('tr').find('td:eq(1)').text();


                if (validId.charAt(0) === "@") {

                    $("#datatable tbody").closest('tr').find('td:eq(6)').removeClass('valid-id');
                    //$("#datatable tbody").closest('tr').find('td:eq(6)').addClass('invalid-id');
                }
            }
        }
    ]
});


What I have tried:

I've tried first finding the id text. Then using the substring or the charAt to determine if first character was "@" symbol. then from there find the table cell remove the initial class and add the invalid class. I've even tried a regular expression but in the render nothing is happening.
Posted
Updated 8-Nov-18 1:15am

This Solution help fix my problem might help u.

for more info check this link: columns.createdCell

Using createdCell manipulate the DOM within columnDefs options.

For example,

JavaScript
var table = $("#datatable").DataTable({
searching:false,
data: data,
"columnDefs": [
{
   "targets": [1] // first CELL That will be checked,
   "createdCell": function (td, cellData, rowData, row, col) {
             if (cellData < 1) {
                 $(td).addClass('someClass');
              }
    }
 },{
   "targets": [2] //second CELL That will be checked,
   "createdCell": function (td, cellData, rowData, row, col) {
             if (cellData < 1) {
                 $(td).addClass('someClass');
              }
    }
 }  ],
...Etc
 
Share this answer
 
Why are you using $("#datatable tbody").closest('tr')?

That's looking for the closest tr element which is a parent of the tbody element. Unless you're nesting your tables, that won't work.

I suspect you meant to use $(this).closest('tr') instead.

Also, the index passed to .eq()[^] is zero-based. You're trying to modify the seventh cell in the row, but it looks like you want to modify the sixth cell instead.

JavaScript
'render': function (id) {
    var validId = $(this).closest('tr').find('td:eq(1)').text();
    if (validId.charAt(0) === "@") {
        $(this).closest('tr').find('td:eq(5)').removeClass('valid-id');
        //$(this).closest('tr').find('td:eq(5)').addClass('invalid-id');
    }
}
 
Share this answer
 
v2
Comments
Troy Bryant 8-Feb-16 12:39pm    
made that change but still cant remove that class
Richard Deeming 8-Feb-16 13:09pm    
Is the class being added after the cell has been rendered?

Try adding console.log($(this).closest('tr')); to the function, to see if it's finding the row correctly.
Troy Bryant 8-Feb-16 14:02pm    
class is being added as the table is being initialized
Richard Deeming 8-Feb-16 14:10pm    
And the output from console.log? Is it finding the parent tr?
Troy Bryant 8-Feb-16 14:39pm    
Its finding the current table row.
You either have static CSS classes and add/remove them to/from element dynamically, or you can just use HTMLElement.style.* properties immediately. Last option is probably obvious, and the second one is done in jQuery in the following way:
.addClass() | jQuery API Documentation[^],
.removeClass() | jQuery API Documentation[^],
.toggleClass() | jQuery API Documentation[^],
see also CSS | jQuery API Documentation[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900