7

I want to add html element within td after get response from ajax.

Result will be somethings like:

<tr>
<td class="mycus-class" data-title="abc"><span class="mycus-class2">XYZ</span></td>
<td class="mycus-class" data-title="ghi"><span class="mycus-class2">GKL</span></td>
.....
</tr>
1
  • 1
    Far too broad. We don't know if the ajax is bound to the plugin or not. Need to put forth more effort outlining your issues especially when it involves a complex plugin Commented Oct 7, 2015 at 13:51

2 Answers 2

34

Very easy with a render() function, here a little demo :

var data = [
    { firstName: 'john', lastName : 'doe' }
]

var table = $('#example').DataTable({
    data : data,
    columns : [
       {  data : 'firstName',
          render : function(data, type, row) {
              return '<span class="mycus-class2">'+data+'</span>'
          }    
       },
       {  data : 'lastName' }
   ]        
})  

http://jsfiddle.net/e9be48oq/


You can target multiple columns in one call :

columnDefs : [
   { targets : [0,3,4,5],
     render : function(data, type, row) {
        return '<span class="mycus-class2">'+data+'</span>'
     }     
   }
]
Sign up to request clarification or add additional context in comments.

1 Comment

@chotesah, consider accepting the answer if it helped you out.
1

  "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
                console.log(aData[1]);
                if (aData[1] == "Imported")
                {
                    // $('td').css('background-color', '#FBE9E7');
                     $(nRow).find('td:eq(1)').addClass('label label-success');
                } else if (aData[1] == "Inactive") {
                    $(nRow).find('td:eq(1)').addClass('label label-danger');
                } else if(aData[1] == "Exported") {
                   $(nRow).find('td:eq(1)').addClass('label label-primary');
                }else{
                  $(nRow).find('td:eq(1)').html('<span class="label label-default">'+aData[1]+'</span>');
                    // $.addClass('label label-default');
                }

            },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.