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
Austyn Schinner
1
May 30, 2000, 4:17:32 PM
Candida Wolff
2
Jul 10, 1973, 6:06:06 AM
Shanelle Pouros
3
Feb 5, 2006, 1:44:07 PM

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
Krystal Buckridge
Aug 31, 1977, 4:10:08 PM
2
Pearline Gerlach
Oct 24, 1988, 10:26:05 AM
3
Winnifred Kihn
Oct 23, 2021, 3:00:00 PM

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