GridView filter

Общие вопросы по использованию второй версии фреймворка. Если не знаете как что-то сделать и это про Yii 2, вам сюда.
Ответить
Аватара пользователя
mrbighokage
Сообщения: 28
Зарегистрирован: 2014.02.21, 10:33
Откуда: ua

GridView filter

Сообщение mrbighokage »

Привет. Не могу понять как включить простой фильтр в GridView.

Изображение

Код: Выделить всё

public function actionIndex()
    {
        $query = User::find();
        $models = $query->all();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => ['defaultOrder' => ['username' => SORT_ASC]],
            'pagination' => new Pagination([
                    'pageSize' => $this->page_size
                ])
        ]);
        return $this->render('index', [
            'models' => $models,
            'dataProvider' => $dataProvider,
        ]);
    }

Код: Выделить всё

echo GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $models,
            'responsive' => true,
            'hover' => true,
            'showPageSummary' => false,
            'showFooter' => false,
            'export' => false,
            'panel' => [
                'heading' => '<h3 class="panel-title">' . Icon::show('users') . ' Users</h3>',
                'type' => 'default',
                'before' => Html::a(Icon::show('plus') . ' Create', ['create'], ['class' => 'btn btn-success']) . ' ' .
                    Html::a(Icon::show('trash-o') . ' Delete', ['delete'], ['class' => 'btn btn-danger']),
                'after' => Html::a(Icon::show('repeat') . ' Reset', ['index'], ['class' => 'btn btn-info']),
            ],
            'columns' => [
                [
                    'class' => 'kartik\grid\SerialColumn'
                ],
                [
                    'attribute' => 'username',
                    'vAlign' => 'middle',
                ],
                [
                    'attribute' => 'email',
                    'vAlign' => 'middle',
                ],
                [
                    'class' => 'kartik\grid\BooleanColumn',
                    'attribute' => 'status',
                    'vAlign' => 'middle',
                ],
                [
                    'class' => 'kartik\grid\ActionColumn',
                    'dropdown' => false,
                    'vAlign' => 'middle',
                    'urlCreator' => function ($action, $model, $key, $index) {
                            return '#';
                        },
                    'viewOptions' => ['title' => 'Details'],
                    'updateOptions' => ['title' => 'Edit page'],
                    'deleteOptions' => ['title' => 'Delete action'],
                ],
                ['class' => 'kartik\grid\CheckboxColumn']
            ],
        ]);
lynicidn
Сообщения: 2222
Зарегистрирован: 2014.05.24, 15:12

Re: GridView filter

Сообщение lynicidn »

там же где ты нашел эти параметры есть такое ;)

Код: Выделить всё

    /**
     * @var \yii\base\Model the model that keeps the user-entered filter data. When this property is set,
     * the grid view will enable column-based filtering. Each data column by default will display a text field
     * at the top that users can fill in to filter the data.
     *
     * Note that in order to show an input field for filtering, a column must have its [[DataColumn::attribute]]
     * property set or have [[DataColumn::filter]] set as the HTML code for the input field.
     *
     * When this property is not set (null) the filtering feature is disabled.
     */
    public $filterModel;
    /**
     * @var string|array the URL for returning the filtering result. [[Url::to()]] will be called to
     * normalize the URL. If not set, the current controller action will be used.
     * When the user makes change to any filter input, the current filtering inputs will be appended
     * as GET parameters to this URL.
     */
    public $filterUrl;
    /**
     * @var string additional jQuery selector for selecting filter input fields
     */
    public $filterSelector;
    /**
     * @var string whether the filters should be displayed in the grid view. Valid values include:
     *
     * - [[FILTER_POS_HEADER]]: the filters will be displayed on top of each column's header cell.
     * - [[FILTER_POS_BODY]]: the filters will be displayed right below each column's header cell.
     * - [[FILTER_POS_FOOTER]]: the filters will be displayed below each column's footer cell.
     */
    public $filterPosition = self::FILTER_POS_BODY;
    /**
     * @var array the HTML attributes for the filter row element.
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
     */
    public $filterRowOptions = ['class' => 'filters'];
    /**
     * @var array the options for rendering the filter error summary.
     * Please refer to [[Html::errorSummary()]] for more details about how to specify the options.
     * @see renderErrors()
     */
    public $filterErrorSummaryOptions = ['class' => 'error-summary'];
    /**
     * @var array the options for rendering every filter error message.
     * This is mainly used by [[Html::error()]] when rendering an error message next to every filter input field.
     */
    public $filterErrorOptions = ['class' => 'help-block'];
 
Аватара пользователя
mrbighokage
Сообщения: 28
Зарегистрирован: 2014.02.21, 10:33
Откуда: ua

Re: GridView filter

Сообщение mrbighokage »

В filterModel я указал базовую модель Users.
Может в этом проблема. Я непонимаю как работает filterModel!
lynicidn
Сообщения: 2222
Зарегистрирован: 2014.05.24, 15:12

Re: GridView filter

Сообщение lynicidn »

у тебя должна быть модель типа UserSearch
Аватара пользователя
mrbighokage
Сообщения: 28
Зарегистрирован: 2014.02.21, 10:33
Откуда: ua

Re: GridView filter

Сообщение mrbighokage »

Спасибо vova07 за демку https://github.com/vova07/yii2-start
Тут нашол пример UserSearch и понял как это должно работать.
lynicidn
Сообщения: 2222
Зарегистрирован: 2014.05.24, 15:12

Re: GridView filter

Сообщение lynicidn »

ну хоть гуглить умеешь :)
Karrahahu1
Сообщения: 1
Зарегистрирован: 2016.02.09, 08:21

Re: GridView filter

Сообщение Karrahahu1 »

Я непонимаю как работает filterModel!???
Аватара пользователя
Akulenok
Сообщения: 437
Зарегистрирован: 2014.05.05, 18:32
Откуда: localhost

Re: GridView filter

Сообщение Akulenok »

Кто нибудь знает как в фильтр иконку воткнуть?
как здесь с мылом и паролем http://fontawesome.io/examples/#custom
ភាសាខ្មែរ Yii2 - это кайф!
Ответить