How do i Output Two columns from mysql to one data cell

How do i Output Two columns from mysql to one data cell

dannjorogedannjoroge Posts: 6Questions: 4Answers: 0
edited July 2016 in Free community support

Hi am trying to output two columns from a mysql database to one cell in the datatable. how do i do that am using the server side array in php

__$columns = array(
array( 'db' => 'no', 'dt' => '0' ),
array( 'db' => 'name',  'dt' => '1' ),
array( 'db' => 'phoneno',   'dt' => '2' ),
array( 'db' => 'email',     'dt' => '3' ),

array(
    'db'        => 'idno',
    'dt'        => '4',

),
array(
    'db'        => 'idno',
    'dt'        => '5',

),
array( 'db' => 'clientno', 'dt' => '6', 'formatter' => function( $d, $row ) {
            return '<a href="../clients/add_clients.php?clientid='.$row[6].'"><span class="label label-inverse"><i class="fa fa-edit"></i> Edit</span></a>';}, 

        'field' => 'clientno' )

);__

Answers

  • nava1021nava1021 Posts: 9Questions: 0Answers: 1

    You can use the render function to decorate the columns.
    Here I am populating datatable #table with three columns. col1, col2 and "New Column"

    New Column is merged using col3 and col4

    Hope this may help.

    $('#table').DataTable({
          ajax: 
          {
              url: some_url,
              dataSrc : '',                  
              contentType: "application/json",
              type: "POST",
              data : {},
          },
          columns: 
          [
           { data: 'col1'},        
           { data : 'col2',
          
           { sTitle: "New column", 
            render : function(data, type, row){                      
                  new_col = row.col3 + ' ' + row.col4;
                  return new_col;
                }
           },  
       });
    
  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    In addition to @nava1021's excellent answer, you might also find the rendering manual page to be useful.

    Allan

  • dannjorogedannjoroge Posts: 6Questions: 4Answers: 0

    thanks i used a server side script using php works like a charm

This discussion has been closed.