Skip to content
Advertisement

how to specific response in ajax datatable using codeigniter?

This my code for datatable in view file (Codeigniter 3)

function trade_Datatable()
{
    $.ajax({
        url:'<?php echo base_url('user/get_trade_manage'); ?>',
        type:'POST',

        success:function(data)
        {
            var dataTable=$('#trade_management').DataTable({
            'searching' : false,
            'ordering'  : false,
            'processing': true,
            'serverSide': true,
            'ajax':'<?php echo base_url('user/get_trade_manage'); ?>',
            
            "rowCallback": function( row,response) {

                $('td', row).css('background-color', '#00b0f0');
                
                },

            });
        }

    });
}
trade_Datatable();

Controller code

......
$output['draw']=$draw;
$output['recordsTotal']=$counts;
$output['recordsFiltered']=$counts;
$output['data']=$data;
$output['colors']=$bgcolor;
echo json_encode($output);

i need to echo $bgcolor variable inplace #00b0f0 of to apply style dynamically.Anyone suggest best answer ?

Advertisement

Answer

User createdRow for apply styles in ajax datatables

$.ajax({
        url:'<?php echo base_url('user/get_trade_manage'); ?>',
        type:'POST',
        success:function(data)
        {
            var dataTable=$('#trade_management').DataTable({
            'searching' : false,
            'ordering'  : false,
            'processing': true,
            'serverSide': true,
            'ajax':'<?php echo base_url('user/get_trade_manage'); ?>',
    
            columns:[
                {data: 'sno'},
                {data: 'created'},
                {data: 'updatedon'},
                {data: 'tip_type'},
                {data: 'tip_for'},
                {data: 'script_name'},
                {data: 'tip_date'},
                {data: 'tip_position'},
                {data: 'tip_price'},
                {data: 'action'},
                {data: 'tip_exitat'},
                {data: 'photo'},
                {data: 'profit_loss'},  
                ],
    
                createdRow: function(row, rowColors, dataIndex)
                {
                    var colorRow = rowColors.colors;
                    $(row).css('background-color',colorRow);
                }
    
                });
            }
        });
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement