1

I'm using jQuery DataTables with server side processing for my list. But I have a problem: I want to display more than one column data from database in the same column (header). So, how can I define my column?

<?php
$columns = array(
    array( 'db' => 'ass_id', 'dt' => 0 ),
    array( 'db' => 'ass_customer_id',  'dt' => 1 ),
    array( 'db' => 'ass_customer_id',   'dt' => 2 ),
    array( 'db' => 'ass_id', 'dt' => 3 ),
    array( 'db' => 'ass_id', 'dt' => 4 ),
    array( 'db' => 'ass_id', 'dt' => 5 ),
    array( 'db' => 'ass_id', 'dt' => 6 ),
    array( 'db' => 'ass_id', 'dt' => 7 ),
    array( 'db' => 'ass_rec_letter_sent', 'dt' => 8 )
);
?>

In above code, I defined 8-th column as ass_rec_letter_sent but in this same column I have 2 more data fields to display, so I can't understand how can I do this.

1 Answer 1

1

You may employ columns.render option for that purpose. It accepts your cell data, cell data type, entire row data source and row number/column number as arguments.

So, for your target column you may make up combined field that will contain as many fields as needed referred by corresponding property name.

For instance, if the source data for each row of your table is an object, like:

{id: 'someid', name: 'somename', phone: 'somephone', mail: 'somemail'}

And you would like to merge phone and mail into combined column contacts, you may simply do:

$('#example').DataTable({
  ...
  columns: [
    {title: 'contacts', data: null, render: (data, type, row, meta) => `phone: ${row.phone}, e-mail: ${row.mail}`}
  ]
});

Complete demo of this concept you may find below:

//data sample that mimic response from your back-end PHP script
const srcData = [
  {id: 1, name: 'Steve Rogers', phone: '+1987270123512', mail: '[email protected]'},
  {id: 2, name: 'Tony Stark', phone: '+1987987235143', mail: '[email protected]'},
  {id: 3, name: 'Peter Parker', phone: '+1698701245121', mail: '[email protected]'}
];

//datatables initialization
const dataTable = $('#example').DataTable({
  dom: 't',
  data: srcData,
  columns: [
    {title: 'id', data: 'id'},
    {title: 'name', data: 'name'},
    {title: 'contacts', data: null, render: (data, type, row, meta) => `phone: ${row.phone}, e-mail: ${row.mail}`}
  ]
});
<!doctype html>
<html>
<head>
  <script type="application/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script type="application/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
</head>
<body>
  <table id="example"></table>
</body>
</html>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks i will apply this but before that now i got another issue that it is showing "Showing 1 to 25 of 5,004 items (filtered from 5,006 total items)" but all data ( 5,006) load in one page. and no any error showing in console and checked parameter and length is 25 so why load all data?
If your original problem is solved by above answer, you may upvote and accept that, then post another question with another issue and I will gladly look into that
Now could you please tell me how I can search in this field? since I merge the first and last names, I cannot search. either I can use first_name to search or last_name

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.