PHP code example of bupy7 / yii2-grid

1. Go to this page and download the library: Download bupy7/yii2-grid library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bupy7 / yii2-grid example snippets


use bupy7\grid\GridView;

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        ['class' => 'yii\grid\CheckboxColumn'],
        'attribute1',
        'attribute2',
    ],
]);

use bupy7\grid\GridView;

$panelHeadingTemplate = <<<HTML
    <div class="col-md-6">{controls}</div>
    <div class="col-md-6 text-right">{pageSizer}</div>
    <div class="clearfix"></div>
HTML;
echo GridView::widget([
    'customTags' => [
        'controls' => $this->render('_controls'),
    ],
    'panelHeadingTemplate' => $panelHeadingTemplate,
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        ['class' => 'yii\grid\CheckboxColumn'],
        'attribute1',
        'attribute2',
    ],
]);


use yii\helpers\Html;
use yii\helpers\Json;

public function actionDelete($id = null)
{
    if ($id === null) {
        $ids = (array)Yii::$app->request->post('ids');
    } else {
        $ids = (array)$id;
    }
    for ($i = 0; $i != count($ids); $i++) {
        $this->findModel($ids[$i])->delete();
    }
    return $this->redirect(['index']);
}

use bupy7\grid\interfaces\StorageInterface;

/**
 * @inheritdoc
 */
class Session extends \yii\web\Session implements StorageInterface
{

}

'components' => [
    'gridManager' => [
        'class' => 'bupy7\grid\components\Manager',
        'storage' => 'session',
    ],
]

use bupy7\grid\actions\VisibleColumnsAction;
use yii\helpers\Url;

public function actions()
{
    return parent::actions() + [
        'visible-columns' => [
            'class' => VisibleColumnsAction::className(),
            'gridId' => 'example-grid', 
        ],
    ];
}

public function actionIndex()
{
    Url::remember();

    $searchModel = new ExampleSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    $visibleColumns = Yii::$app->gridManager->getVisibleColumns('example-grid');
    if ($visibleColumns === false) {
        $visibleColumns = array_keys($searchModel->gridColumnsList());
    }

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'visibleColumns' => $visibleColumns,
    ]);
}

public function gridColumnsList()
{
    return [
        'attribute1' => 'Label of attribute1',
        'attribute2' => 'Label of attribute2',
    ];
}

use bupy7\grid\GridView;

$panelHeadingTemplate = <<<HTML
    <div class="col-md-6">{controls}</div>
    <div class="col-md-6 text-right">{pageSizer}</div>
    <div class="clearfix"></div>
HTML;
echo GridView::widget([
    'customTags' => [
        'controls' => $this->render('_controls', [
            'visibleColumns' => $visibleColumns,
            'searchModel' => $searchModel,
        ]),
    ],
    'panelHeadingTemplate' => $panelHeadingTemplate,
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'visibleColumns' => $visibleColumns,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'attribute1',
        'attribute2',
    ],
]);

use bupy7\grid\widgets\VisibleColumnsWidget;

<?= VisibleColumnsWidget::widget([
    'gridId' => 'example-grid',
    'modalOptions' => [
        'header' => Yii::t('app', 'SELECT_COLUMNS'),
        'toggleButton' => [
            'label' => Yii::t('app', 'SELECT_COLUMNS'),
            'class' => 'btn btn-default',
        ],
    ],
    'actionForm' => ['visible-columns'],
    'submitBtnLabel' => Yii::t('app', 'APPLY'),
    'columnsList' => $searchModel->gridColumnsList(),
]); 

use bupy7\grid\interfaces\StorageInterface;

/**
 * @inheritdoc
 */
class Session extends \yii\web\Session implements StorageInterface
{

}

'components' => [
    'gridManager' => [
        'class' => 'bupy7\grid\components\Manager',
        'storage' => 'session',
    ],
]

use bupy7\grid\actions\ResizableColumnsAction;
use yii\helpers\Url;

public function actions()
{
    return parent::actions() + [
        'resizable-columns' => [
            'class' => ResizableColumnsAction::className(),
            'gridId' => 'example-grid', 
        ],
    ];
}

public function actionIndex()
{
    Url::remember();

    $searchModel = new ExampleSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    $resizableColumns = Yii::$app->gridManager->getResizableColumns('example-grid');

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'resizableColumns' => $resizableColumns,
        'resizableColumnsUrl' => ['resizable-columns'],
    ]);
}

use bupy7\grid\GridView;

echo GridView::widget([
    'resizableColumns' => $resizableColumns,
    'resizableColumnsUrl' => $resizableColumnsUrl,
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'attribute1',
        'attribute2',
    ],
]);

echo GridView::widget([

    ...

    'pageSizer' => [
        'availableSizes' => [20 => '20', 50 => '50', 100 => '100', -1 => Yii::t('app', 'ALL_PAGES')],
    ],

    ...
]);

$dataProvider = new ActiveDataProvider([
    'query' => $query,
    'pagination' => [
        'pageSizeLimit' => [-1, 100],
    ],
]);

[
    'class' => 'bupy7\grid\TotalColumn',
    'format' => 'currency',
    'attribute' => 'total_cost',
]

echo GridView::widget([
    ...

    'id' => 'unique-id-of-this-grid',
    'keepPageSizer' => true,

    ...
]);