N
N
nepster-web2014-03-20 22:59:53
Yii
nepster-web, 2014-03-20 22:59:53

How to make search filter and sort in GridView Yii2 with modified field?

The situation is this, in the admin panel there is a list of users. I display it using the GridView widget.

echo GridView::widget([
    'id' => 'users-visits-grid',
    'dataProvider' => $dataProvider,
    'filterModel'  => $searchModel,
    
    //'options'      => ['class' => ''],
    'tableOptions' => ['class' => 'table table-bordered'],
    //'rowOptions'   => ['class' => ''],
    
    'layout' => '<div class="GridViewSummary">{summary}</div><div class="panel panel-default"><div class="table-responsive">{items}</div><div class="table-footer">{pager}</div></div>',
    
    'columns' => [
        [
            'attribute' => 'user',
            'format' => 'html',
            'value' => function ($model) {
                return UserColumn::widget([
                    'userId' => $model->id
                ]);
            }
        ],
        'role',
        [
            'attribute' => 'status',
            'format' => 'html',
            'value' => function ($model, $index, $widget) {
                    return ProcessingData::getSelectOptionString($model->status, User::getStatusArray());
                },
            'filterInputOptions' => ['class'=>'form-control', /*'class'=>'select-multiple'*/],
            'filter' => ProcessingData::getSelectOption(User::getStatusArray())
        ],
...

I replaced the username field with after user and process it through my own, that is, I show on the screen not just the user's login, but also the avatar, full name and online / offline icon.
Next, I'd like to add sorting and a search filter, just like I would with username .
However, if I use 'attribute' => 'user', then there is no filter or sort.
If you rename after it to 'attribute' => 'username', there will be another jamb, there will be both a filter and sorting, but I would like the field to be called not Login (as it is written in attributeLabels) but User.
Tell me, please, what do I need to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Gubarev, 2014-03-21
@nepster-web

Waking up, maybe I misunderstood. The solution looks like this:

'attribute' => 'username',
            'format' => 'html',
            'label' => 'Пользователь',
            'value' => function ($model) {
                return UserColumn::widget([
                    'userId' => $model->id
                ]);
            }

Tobezh set `label`.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question