Open In App

How to display child row information using jQuery DataTables plugin ?

Last Updated : 27 Jan, 2021
Like Article

DataTables are modern jQuery plugin for adding interactive and advanced controls to HTML tables for our webpage. DataTable is a simple-to-use jQuery plug-in with many options for developer’s custom changes. Some features of DataTables are pagination, searching, sorting and multiple column ordering.

In this article, we will learn to use DataTable API methods to attach child row to parent row and display its information. This feature is useful when the developer wants to show additional information for a row in a data table. 

The pre-compiled files which are needed to implement the codes are as follows. 




Approach: In the following example, employee details like employee_id, name, designation, salary, and address are present in an Ajax text file “nestedData.txt”. An HTML table is used for storing all the details in rows and columns. 

In JavaScript part of the code, the DataTable is initialized using the plugin. On click, events are handled to show and hide more information for a particular data row. This is implemented by using the API’s and row.child.hide() methods. There are other methods as well.

The getChildRow() function in the following code defines the design/display content for the child row clicked by the user. The developer can change the code as per the application’s need.



<!DOCTYPE html>
    <meta content="initial-scale=1, maximum-scale=1,
        user-scalable=0" name="viewport" />
    <meta name="viewport" content="width=device-width" />
    <!--Datatable plugin CSS file -->
    <link rel="stylesheet" href=
    <!--jQuery library file -->
    <script type="text/javascript" 
    <!--Datatable plugin JS library file -->
    <script type="text/javascript" src=
        td.details-control {
            /* Image in the first column to
                indicate expand*/
            background: url('images/more.png')
                no-repeat center;
            cursor: pointer;
        tr.shown td.details-control {
            background: url('images/shrinkdata.PNG') 
                no-repeat center;
        Display child row detailed 
        information using DataTable
    <!--HTML tables with employee data-->
    <table id="tableID" class="display" 
        /* Function for child row details*/
        function getChildRow(data) {
            // `data` is the data object for the row
            return '<table cellpadding="5" cellspacing="0"'
                + ' style="padding-left:50px;">' +
                '<tr>' +
                '<td>Full name:</td>' +
                '<td>' + + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td>Address in detail:</td>' +
                '<td>' + data.address + '</td>' +
                '</tr>' +
                '<tr>' +
                '<td>Extra details like ID:</td>' +
                '<td>' + data.employee_id + '</td>' +
                '</tr>' +
        $(document).ready(function () {
            /* Initialization of datatables */
            var table = $('#tableID').DataTable({
                "ajax": "nestedData.txt",
                "columns": [{
                    "className": 'details-control',
                    "orderable": true,
                    "data": null,
                    "defaultContent": ''
                    { "data": "name" },
                    { "data": "designation" },
                    { "data": "city" },
                    { "data": "salary" }
                "order": [[1, 'asc']]
            // Click events for expanding and
            // closing using up/down arrows
            $('#tableID tbody').on('click', 
            'td.details-control', function () {
                var tr = $(this).closest('tr');
                var row = table.row(tr);
                if (row.child.isShown()) {
                    // Closing the already opened row           
                    // Removing class to hide
                else {
                    // Show the child row for detail
                    // information
                    // To show details,add the below class

nestedData.txt: The following has the data for “nestedData.txt” file used in the Ajax option in the above code.

  "data": [
      "employee_id": "emp1",
      "name": "Teza",
      "designation": "Architect",
      "salary": "Rs.350,800",     
      "city": "Lucknow",
      "address": "54,komal street"
      "employee_id": "emp2",
      "name": "Garima",
      "designation": "Accountant",
      "salary": "Rs.180,050",    
      "city": "Hyderabad",
      "address": "Hitech city,kodapur"
      "employee_id": "emp3",
      "name": "Ashmita",
      "designation": "Technical Author",
      "salary": "Rs.186,000",    
      "city": "Kolkatta",
      "address": "156, new park,chowk"
      "employee_id": "emp4",
      "name": "Celina",
      "designation": "Javascript Developer",
      "salary": "Rs.450,000",     
      "city": "Itanagar",
      "address": "chandni chowk,new lane"
      "employee_id": "emp5",
      "name": "Asha",
      "designation": "Accountant",
      "salary": "Rs.542,700",   
      "city": "Tokyo",
      "address": "54,Japanese colony"
      "employee_id": "emp6",
      "name": "Baren Samal",
      "designation": "Integration Specialist",
      "salary": "Rs.370,000",    
      "city": "New Delhi",
      "address": "48,RK puram"
      "employee_id": "emp7",
      "name": "Hari Om",
      "designation": "Sales Manager",
      "salary": "Rs.437,500",     
      "city": "Raipur",
      "address": "Sector 6,bhilai"
      "employee_id": "emp8",
      "name": "Ranu",
      "designation": "Integration Specialist",
      "salary": "Rs.330,900",     
      "city": "Tokyo",
      "address": "64,indian colony"
      "employee_id": "emp9",
      "name": "Celly",
      "designation": "PHP Developer",
      "salary": "Rs.275,500",    
      "city": "Secunderabad",
      "address": "23,Sainikpuri"
      "employee_id": "emp55",
      "name": "Ama",
      "designation": "Director",
      "salary": "Rs.985,000",    
      "city": "kanpur",
      "address": "63,Narangi lane"
      "employee_id": "emp56",
      "name": "Michael Jackson",
      "designation": "C++ Developer",
      "salary": "Rs.783,000",    
      "city": "Singapore",
      "address": "53,Sweetpark"
      "employee_id": "emp57",
      "name": "Danny",
      "designation": "Customer Support",
      "salary": "Rs.345,000",    
      "city": "Ludhiana",
      "address": "456,Punjab"


  • Before child row display:


  • After child row display 1:


  • After child row display 2:


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads