Format individual cells

Format individual cells

sspagnasspagna Posts: 3Questions: 0Answers: 0

I have the problem to format individual cells depending on its content.
how to do?
Example:
If the cell n° 2 has a value = 35 in column 3 = 35 the cell red for all tables

Replies

  • rhinorhino Posts: 80Questions: 2Answers: 17

    I'm not sure I understand your example perfectly, but perhaps you could try something like:

    var tableApi = $('#theTable').DataTable();
    tableApi.$('td').each( function() {
        if ( /*condition matches*/ )
            $(this).css('background-color', 'red');
    });
    
  • sspagnasspagna Posts: 3Questions: 0Answers: 0

    There is a function that tells me the index of the td?

  • rhinorhino Posts: 80Questions: 2Answers: 17

    If you want the index of a specific td, use cell().index(), it returns an object that looks like:

    {
        "row":           integer, // Row index
        "column":        integer, // Column data index
        "columnVisible": integer  // Column visible index
    }
    

    So you might use that in this way:

    var tableApi = $('#theTable').DataTable();
    tableApi.$('td').each( function() {
        if ( tableApi.cell(this).index().column == 5 )
            $(this).css('background-color', 'red');
    });
    
  • rhinorhino Posts: 80Questions: 2Answers: 17

    Or if you want to color based on the data in the table:

    var tableApi = $('#theTable').DataTable();
    tableApi.$('td').each( function() {
        if ( tableApi.cell(this).data() == 5 )
            $(this).css('background-color', 'red');
    });
    
  • sspagnasspagna Posts: 3Questions: 0Answers: 0

    // for set data
    var tableApi = $('#theTable').DataTable();
    tableApi.$('td').each( function() {
    if ( tableApi.cell(this).data() == 5 ) {
    tableApi.cell(this).data(<a herf="">5</a>);
    }
    }

    this does it?
    and then I do the redraw?

  • rhinorhino Posts: 80Questions: 2Answers: 17

    Seems like that should work. Only way to know for sure is to try :)

This discussion has been closed.