Yii2 Components - Data tables

Data tables

DataTable is inherited from yii\grid\GridView. It is used to display data using MDC data table.
vip9008\MDC\components\DataTable

Usage

A basic usage looks like the following:

<?php
echo DataTable::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'name',
        'id',
        'created_at:datetime',
    ],
]);
?>

The above code will render a data table like the following:

Name ID Created at
Misty Wilkinson
1
May 21, 2023, 11:13:03 AM
Blanca Macejkovic
2
Dec 25, 2022, 12:24:18 PM
Eda Wunsch
3
Jul 19, 1994, 9:10:03 AM

Customization options

Data table can be further customized by adding a header and action buttons.

<?php
echo DataTable::widget([
    'dataProvider' => $dataProvider,
    [
        'title' => 'Customers list',
        // Alternative header title configuration
        // 'title' => [
        //     'content' => 'Customers list',
        //     'options' => [...], HTML attributes for header title
        // ],
        'actions' => [
            '<button class="material-icon">filter_list</button>',
            '<button class="material-icon">more_vert</button>',
        ],
        // 'options' => [...], HTML attributes for table header container
    ],
    'columns' => [
        'id',
        'name',
        'created_at:datetime',
    ],
]);
?>
Customers list
ID Name Created at
1
Prof. Nickolas Gutkowski MD
Sep 4, 2014, 3:14:12 AM
2
Dr. Hope Hudson
Nov 26, 2002, 5:44:45 AM
3
Miss Elisabeth Ratke I
Jul 6, 1988, 12:34:29 AM

Other options are inherited from the parent class. see yii\grid\GridView.