Hi i need help with this if statement

Hi i need help with this if statement

mosiahazuaje2019mosiahazuaje2019 Posts: 3Questions: 1Answers: 0

i have this colomns configuration
columns: [
{data: 'departments.name'},
{data: 'phases.name'},
{data: 'date_start'},
{data: 'date_close'},
{data: 'users.name'},
{data: 'debtors.name'},
{data: 'states.name'}
],

and i need to do somthing like

          if({data:'states.name' === 'Aproved' }){
              {data:'states.name', className:'aproved'}
          }

How can i change the className of my column with if statement, somebody can help?? thanks...

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421
    edited August 2020

    That means you want to assign a className to individual cells depending on their contents. Try using this:
    https://datatables.net/reference/api/cell().node()

    The example should point you in the right direction.

    You can check the contents with this:
    https://datatables.net/reference/api/cell().data()

    And to loop through the cells this would be the best option:
    https://datatables.net/reference/api/cells().every()

    This looks very much like what you require:

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421
    edited August 2020 Answer ✓

    This should do the job I guess:

    table.column( 6 ).cells().every( function () {
         if ( this.data === 'approved' ) {
            $(this.node()).addClass( 'approved' );
         } else {
            $(this.node()).removeClass( 'approved' );
         }
    } );
    
  • mosiahazuaje2019mosiahazuaje2019 Posts: 3Questions: 1Answers: 0
    edited August 2020

    Thanks a lot rf1234

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421

    you are welcome! I personally don't like to have column indexes "hard coded" like in the above code. You could assign an id to the column in your HTML and use that as the selector.

    Like this for example:

    <th id="status">Status</th>
    
    table.column('#status').cells().every( function () { ...
    
  • mosiahazuaje2019mosiahazuaje2019 Posts: 3Questions: 1Answers: 0

    oh i think is better, however i need only change the background color for one, something like that .

This discussion has been closed.