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,
img
HTML tag in table td
column.Also Checkout other tutorial of jQuery Datatable,
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.
Step 1: we will create new profile_image
column in employee
table. This column will store profile image path.
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,
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.
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
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
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
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>';
need to change assigned record data index, jsonData.data[i][5] = ''+jsonData.data[i][1]+'';