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
Dr. Desiree Rolfson
1
Sep 8, 2011, 2:59:23 AM
Roman Metz
2
Oct 14, 2002, 6:24:47 PM
Dr. Chandler Nikolaus IV
3
Sep 28, 2012, 4:47:45 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
Lue Predovic
Jul 13, 1988, 8:53:40 AM
2
Myrna Anderson
Mar 9, 2021, 7:55:56 AM
3
Joanie Kessler Sr.
Jan 3, 1975, 2:56:59 AM

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