Datatable rowdetail td addclass add

Datatable rowdetail td addclass add

KonyaKonya Posts: 5Questions: 3Answers: 0

in datatable to assign class to the td (row) we want createdRow We are using , but I can't reach the detail td , detail td the s eq() how to get there?

"createdRow": function ( row, data, index ) {

$('td', row).eq(2).addClass('green');
}

detailed examination

https://codepen.io/BesiktaS_JK/pen/JjddXbW

I want to create this:

try, but it didn't happen

'createdRow': function(row, data, dataIndex){

var match_result = data["match_result"];

if(match_result=="2-2"){$('td', row).eq(5).css('background-color', 'Orange');}

},

Answers

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921

    The contents of the Child Detail rows are independent of Datatables and createdRow doesn't have access to them. The formatting you want needs to take place in the format() function being used to build the Child Details HTML.

    Kevin

  • KonyaKonya Posts: 5Questions: 3Answers: 0

    @kthorngren format() How can I reach with?

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921

    This is your format() function:

    function format (data) {
          return '<div class="details-container">'+
              '<table cellpadding="5" cellspacing="0" border="0" class="details-table">'+
                  '<tr>'+
                      '<td class="title" rowspan="2">Match Score</td>'+
                      '<td>2-2</td>'+
                  '</tr>'+
                  
                  '<tr>'+
                      '<td>'+ data.sccoreodd22 +'</td>'+
                  '</tr>'+
              '</table>'+
            '</div>';
      };
    
    

    Place your if statement before the return and add the class where you want inline in the return string, for example:

    function format (data) {
          var inlineCss = '';
          if (data.match_result === '2-2') {
              inlineCss = ' style="background-color:Orange;"`
          }
          return '<div class="details-container">'+
              '<table cellpadding="5" cellspacing="0" border="0" class="details-table">'+
                  '<tr>'+
                      '<td class="title" rowspan="2">Match Score</td>'+
                      '<td>2-2</td>'+
                  '</tr>'+
                  
                  '<tr>'+
                      '<td' + inlineCss + '>'+ data.sccoreodd22 +'</td>'+
                  '</tr>'+
              '</table>'+
            '</div>';
      };
    
    

    Didn't test the above so it may not be exactly right but hopefully it will give you an idea of what to do.

    Kevin

  • KonyaKonya Posts: 5Questions: 3Answers: 0

    @kthorngren thank you for the idea

    when there is more than one score ,giving all color

    https://codepen.io/BesiktaS_JK/pen/poJJRyN

    how can I overcome this problem?

  • kthorngrenkthorngren Posts: 21,167Questions: 26Answers: 4,921

    There is nothing Datatables specific about the format() function. Its simply Javascript code. You can use or (||) in the if statement or use switch. What you use depends on what your data is.

    Kevin

This discussion has been closed.