Php

How to parse JSON data in jQuery Datatable

This Datatable jquery tutorial help to parse JSON data which was received from server side.I got huge response from reader on datatable tutorials,i am sharing next level datatable tutorial with php and mysql,I am extending Datatable Pagination, Sorting and Search – Server Side (PHP/MySQl) Using Ajax.

Sometimes we need to add some text,process json data or filter JSON object data before to send datatable jQuery table.I am using Datatable in-build call back dataSrc() function which will parse JSON data as your requirement and send to datatable.

This tutorial will help to achieve following milestone,

  • Added Hyperlink(Anchor HTML tag) on column in jquery datatable.
  • Added img HTML tag in table td column.
  • Added edit/delete and view action button.

Also Checkout other tutorial of jQuery Datatable,

I am are using below Files in this tutorial

Index.php - This file will responsible to create html and instance datatable using jquery code.
response.php - This file responsible to create database connection string and convert records into json string and returns data to Ajax method as response.
connection.php - This file responsible to create mysql database connection.

I already mentioned in this post i am extending Datatable Pagination, Sorting and Search – Server Side (PHP/MySQl) Using Ajax, so i assumed you have read and understand that tutorial.

How to parse JSON data in jQuery Datatable

Step 1: we will create new profile_image column in employee table. This column will store profile image path.

Related Post
  ALTER TABLE `employee` ADD `profile_image` VARCHAR(255) NOT NULL DEFAULT 'images/default_profile.png' AFTER `employee_age`; 

Step 2: Added profile_image column in $column array in response.php file.

  //define index of column
    $columns = array( 
    0 =>'id',
    1 =>'employee_name', 
    2 => 'employee_salary',
    3 => 'employee_age',
    4 =>'profile_image'
    );

  

Step 3: Added profile_image and Action column in HTML table header and footer heading in index.php file.

  <thead>
            <tr>
                <th>Empid</th>
                <th>Name</th>
         <th>Salary</th>
                <th>Age</th>
        <th>Image</th>
        <th>Action</th>
            </tr>
        </thead>
 
        <tfoot>
            <tr>
               <th>Empid</th>
                <th>Name</th>
        <th>Salary</th>
                <th>Age</th>
                <th>Image</th>
        <th>Action</th>
            </tr>
        </tfoot>

  

Step 4: We will user dataSrc callback jquery datatable function to parse JSON object.This function will take JSON data as a parameter which will returned from Ajax request.

$( document ).ready(function() {
  $('#employee_grid').DataTable({
     .....
      
         "ajax":{
            .....
        "dataSrc": function (jsonData) {
        for ( var i=0, len=jsonData.data.length ; i<len ; i++ ) {
        
        jsonData.data[i][4] = '<img src="https://phpflow.com/php/parse-json-data-jquery-datatable/"/>';
        jsonData.data[i][1] = '<a href="https://phpflow.com" target="_blank">'+jsonData.data[i][1]+'</a>';
        jsonData.data[i][5] = '<div class="btn-group" data-toggle="buttons"><a href="#" target="_blank" class="btn btn-primary btn-xs">Edit</a><a href="#" target="_blank" class="btn btn-primary btn-xs">Delete</a><a href="#" target="_blank" class="btn btn-primary btn-xs">View</a></div>';
        }
        
        return jsonData.data;
        }
       ....
          }
        });   
});

as you can see above code, I am adding following functionality,

  1. Adding img html tag with profile_image column value
  2. Adding href attribute on each name column value
  3. Adding new column Action column which will take edit/delete and view anchor tag

Conclusion

We have learn how to parse received server JSON object in client side and send to jQuery datatable.This jQuery tutorial help to add or modified JSON object before render datatable in HTML table format.We have added Image and anchor HTML tag with column value in td.

You can download source code and Demo from below link.

View Comments

  • Thanks Parvez for share with img and action tag
    but i have some queries,

    I need to use if condition on data
    i have a filed that name gender and values are 0 and 1 (0=male,1=female)

    i want to display male for 0 and female for 1 rather then 0 and 1

    please help me to solve out this.

    Thank You.

    • you can check condition in json looop and set value based on coloumn value,if(colval == 0) colkey=female else colkey = male

  • what if i have 20 fields and i just want to shown all records from 6 fields.. it will located at the first field, middle field and in the end field.. how do i do that.. please help.. thanks in advance.

  • in js side,
    You need to unset those index fields in ajax
    "dataSrc": function (jsonData) {
    for ( var i=0, len=jsonData.data.length ; i<len ; i++ ) {
    delete(jsonData.data)
    jsonData.splice( i, 1 );
    }
    Server side:
    You can send response of those col values/index which u need to show in table

  • Hello, first of all thanks a lot! this is awesome! I would like to know how to replace de # link in your example for an URL that is in a 5th hidden column. should I just do this? jsonData.data[i][1] = '<a href="jsonData.data[i][5]" target="_blank">'+jsonData.data[i][1]+'</a>';

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories