Eldhose Elias Eldhose Elias - 1 month ago 5
Ajax Question

Embedd <label> into DataTable based on ajax json value

var table = jQuery('#datatable-responsive').DataTable({
dom: 'Bfrtip',
lengthMenu: [
[ 10, 25, 50, -1 ],
[ '10 rows', '25 rows', '50 rows', 'Show all' ]
],
ajax: {
url: 'getClients.php',
dataSrc: 'aaData',
method:'POST'
},
columns: [
{ data:'client_name'},
{ data:'client_location'},
{ data:'client_region'},
{ data:'client_remarks'},
{ data:'client_status'}
],
select: true
});


The above javascript is used to fill a DataTable. My Requirement is ,i need to add <label> tag into DataTable row cell based on client_status value.
if client_status value is 0 then i need a type label ,if it is 1 i need to add another label.

is it possible to add html into datatable at the time of table filling via ajax json?

Answer
var table = jQuery('#datatable-responsive').DataTable({
     dom: 'Bfrtip',
     lengthMenu: [
                    [ 10, 25, 50, -1 ],
                    [ '10 rows', '25 rows', '50 rows', 'Show all' ]
                 ],
     ajax: {
          url: 'getClients.php',
          dataSrc: 'aaData',
          method:'POST'
     },
     columns: [
          { data:'client_name'},
          { data:'client_location'},
          { data:'client_region'},
          { data:'client_remarks'},
          { data:'client_status',render: function (data, type, full, meta) {
               if (data == 'N' ){
                   return '<label  class="bs-label bg-blue">New</label>';
               }else if (data =='O'){
                   return '<label  class="bs-label bg-green">On Board</label>';
               }else if (data=='I'){
                   return '<label  class="bs-label bg-red">In-Active</label>';   
               }}
           }
      ],
      select: true
 });  
Comments