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
Owen Borer
1
Mar 8, 1982, 8:37:23 AM
Joelle O'Conner
2
Dec 5, 2017, 11:10:23 AM
Prof. Mack Shields Jr.
3
Jun 12, 1982, 3:37:29 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
Kaley Schulist
Jan 18, 2010, 1:51:43 AM
2
Avery Kutch
Dec 11, 1973, 6:05:15 PM
3
Mose Heller
Apr 5, 2024, 5:15:39 AM

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