2017 © Pedro Peláez
 

yii2-extension yii2-grid-view-state

Save filters from GridView to session, keep the filter state between pages.

image

thrieu/yii2-grid-view-state

Save filters from GridView to session, keep the filter state between pages.

  • Tuesday, November 14, 2017
  • by thrieu
  • Repository
  • 6 Watchers
  • 7 Stars
  • 200 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 2 Versions
  • 52 % Grown

The README.md

yii2-grid-view-state

Save filters from GridView to session, keep filter state between pages., (*1)

Features

  1. Very flexible. Separate setting and getting.
  2. Setting via behavior.
  3. Determines uniqueness by action's route and a customizable ID.

Installation

To install, either run, (*2)

php composer.phar require --prefer-dist thrieu/yii2-grid-view-state "dev-master"

or add, (*3)

"thrieu/yii2-grid-view-state": "dev-master"

to the require section of your composer.json file and then run composer update., (*4)

Usage

Step 1

Create \app\widgets\Gridview.php, (*5)

Extend GridView class, simply implement FilterStateInterface and FilterStateTrait., (*6)

namespace \app\widgets;

use thrieu\grid\FilterStateInterface;
use thrieu\grid\FilterStateTrait;

class GridView extends \yii\grid\GridView implements FilterStateInterface {
    use FilterStateTrait;
}

Step 2

Attach the filter behavior to your GridView widget., (*7)

use \app\widgets\Gridview;
...

GridView::widget([
...
    'as filterBehavior' => \thrieu\grid\FilterStateBehavior::className(),
...
]);

Step 3

Update \app\models\xxxSearch.php, (*8)

To get the params which is merged with GridView state params and GET query params, and then set it to filter model and DataProvider., (*9)

// DataProvider
$dataProvider = new ActiveDataProvider([
    'query' => $query,
    'pagination' => [
        'params' => \app\widgets\GridView::getMergedFilterStateParams(),
    ],
    'sort' => [
        'params' => \app\widgets\GridView::getMergedFilterStateParams(),
    ],
]);
// Filter model
$this->load(\app\widgets\GridView::getMergedFilterStateParams());

Enjoy it., (*10)

Clear state

Update \app\controllers\xxxController.php, (*11)

Add ClearFilterStateBehavior to behaviors() of your controller., (*12)

    public function behaviors()
    {
        return [
            ...
            'clearFilterState' => \thrieu\grid\ClearFilterStateBehavior::className(),
            ...
        ];
    }

And then add a form to your frontend page., (*13)

        $form = Html::beginForm();
        $form .= Html::hiddenInput('clear-state', '1');
        $form .= Html::hiddenInput('redirect-to', '');
        $form .= Button::widget([
            'label' => Yii::t('app', 'Reset filter'),
        ]);
        $form .= Html::endForm();
        echo $form;

get filter settings by route

$filterData = GridView::getMergedFilterStateParams(null, null, 'delivery/cmd-delivery/index');
$model->load($filterData);

Prev Next model for view screens

for gridview set usepreview = true, (*14)

use \app\widgets\Gridview;
...

GridView::widget([
...
    'as filterBehavior' => \thrieu\grid\FilterStateBehavior::class,
    'usePrevNext' => true,
...
]);

for prev next buttons easy get previousa nd next model keys values, (*15)

        $prevNext = new PrevNextPage('cars/in-way/index');

        if ($prevId = $prevNext->getPrevPage($model->id)) {
            echo ThButton::widget([
                    'tooltip' => Yii::t('blankonthema', 'Previous record'),
                    'link' => [
                        'view',
                        'id' => $prevId,
                    ],
                    'icon' => 'arrow-left',
                    'type' => ThButton::TYPE_DEFAULT
                ]);
        }
        if ($nextId = $prevNext->getNextPage($this->id)) {
            echo ThButton::widget([
                'tooltip' => Yii::t('blankonthema', 'Next record'),
                'link' => [
                    'view',
                    'id' => $nextId,
                ],
                'icon' => 'arrow-right',
                'type' => ThButton::TYPE_DEFAULT
            ]);
        }


The Versions

14/11 2017

dev-master

9999999-dev https://github.com/thrieu/yii2-grid-view-state

Save filters from GridView to session, keep the filter state between pages.

  Sources   Download

BSD 3-Clause

The Requires

 

by Theo Ding

extension filter yii2 widget gridview

19/10 2017

v1.0

1.0.0.0 https://github.com/thrieu/yii2-grid-view-state

Save filters from GridView to session, keep the filter state between pages.

  Sources   Download

BSD 3-Clause

The Requires

 

by Theo Ding

extension filter yii2 widget gridview