Datatables Expand/Collapse Columns

Websolutionstuff | Jun-05-2022 | Categories : Laravel PHP jQuery

In this article, we will see how to expand/collapse columns in datatable. The Datatables API has a number of methods for attaching child rows to a parent row in the Datatable. This can be used to show additional information about a row, useful for cases where you wish to convey more information about a row than there is space for in the host table.

So, let's see parent-child rows in jquery datatable, datatables expand/collapse columns, collapse/expand click groups, how to expand/collapse all child rows, datatable collapse column, jquery datatable nested table expand/collapse, child row above parent row on datatable, datatables multiple child rows, jquery datatable expand/collapse rows.

HTML:

<table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th></th>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th></th>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>

 

 

Javascript:

/* Formatting function for row details - modify as you need */
function format(d) {
    // `d` is the original data object for the row
    return (
        '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">' +
        '<tr>' +
        '<td>Full name:</td>' +
        '<td>' +
        d.name +
        '</td>' +
        '</tr>' +
        '<tr>' +
        '<td>Extension number:</td>' +
        '<td>' +
        d.extn +
        '</td>' +
        '</tr>' +
        '<tr>' +
        '<td>Extra info:</td>' +
        '<td>And any further details here (images etc)...</td>' +
        '</tr>' +
        '</table>'
    );
}
 
$(document).ready(function () {
    var table = $('#example').DataTable({
        ajax: '../ajax/data/objects.txt',
        columns: [
            {
                className: 'dt-control',
                orderable: false,
                data: null,
                defaultContent: '',
            },
            { data: 'name' },
            { data: 'position' },
            { data: 'office' },
            { data: 'salary' },
        ],
        order: [[1, 'asc']],
    });
 
    // Add event listener for opening and closing details
    $('#example tbody').on('click', 'td.dt-control', function () {
        var tr = $(this).closest('tr');
        var row = table.row(tr);
 
        if (row.child.isShown()) {
            // This row is already open - close it
            row.child.hide();
            tr.removeClass('shown');
        } else {
            // Open this row
            row.child(format(row.data())).show();
            tr.addClass('shown');
        }
    });
});

 

 

AJAX:

{
  "data": [
    {
      "id": "1",
      "name": "Tiger Nixon",
      "position": "System Architect",
      "salary": "$320,800",
      "start_date": "2011/04/25",
      "office": "Edinburgh",
      "extn": "5421"
    },
    {
      "id": "2",
      "name": "Garrett Winters",
      "position": "Accountant",
      "salary": "$170,750",
      "start_date": "2011/07/25",
      "office": "Tokyo",
      "extn": "8422"
    },
    {
      "id": "3",
      "name": "Ashton Cox",
      "position": "Junior Technical Author",
      "salary": "$86,000",
      "start_date": "2009/01/12",
      "office": "San Francisco",
      "extn": "1562"
    },
    {
      "id": "4",
      "name": "Cedric Kelly",
      "position": "Senior Javascript Developer",
      "salary": "$433,060",
      "start_date": "2012/03/29",
      "office": "Edinburgh",
      "extn": "6224"
    },
    {
      "id": "5",
      "name": "Airi Satou",
      "position": "Accountant",
      "salary": "$162,700",
      "start_date": "2008/11/28",
      "office": "Tokyo",
      "extn": "5407"
    }
  ]
}

 


You might also like :

Recommended Post
Featured Post
Laravel 8 One To Many Relationship Example
Laravel 8 One To Many Relation...

In this example we will see laravel 8 one to many relationship example. Also you can use one to many relationship in lar...

Read More

Nov-03-2021

How To Restrict User Access From IP Address In Laravel 9
How To Restrict User Access Fr...

Imagine this: You've made a super cool website, and now you want to make sure only the right people can use it. That...

Read More

Jan-03-2023

How To File Upload With Progress Bar Angular 15
How To File Upload With Progre...

In this article, we will explore how to implement a file upload feature in Angular 15 with a progress bar. We will guide...

Read More

Jun-23-2023

How To Integrate Email Template Builder In Laravel
How To Integrate Email Templat...

In this article, we will see how to integrate an email template builder in laravel. Also, you can integrate an emai...

Read More

Feb-22-2023