E
E
EvgMul2015-10-26 10:25:13
Yii
EvgMul, 2015-10-26 10:25:13

Multiple GridViews on the same page?

The problem is the following. There are 2 gridViews on one page. If I start looking for something in one grid, then this filter works on both. I can't figure out what the problem is. dataProvider and filterModel are different for each grid.
Below is the code. Please tell me how to set up a separate search from each other. I suspect that this is due to the fact that they just have the same GET, or maybe not. In general, I have little understanding of this, I hope for your help.
index.php view page

echo GridView::widget([
            'dataProvider' => $newProvider,
            'filterModel' => $newSearch,
            'showHeader' => true,
            'showFooter' => false,
            'tableOptions' => [
                'class' => 'table table-striped table-bordered table-condensed'
            ],
            'columns' => [
                ['class' => 'yii\grid\SerialColumn'],
                'title',
                'text',
                'date',
                'status' => [
                    'attribute' => 'status',
                    'format' => 'text',
                    'content' => function ($data) {
                        return Yii::t('ticket', $data->status);
                    },
                ],
                $viewed => [
                    'attribute' => $viewed,
                    'label' => Yii::t('ticket', 'field_viewed'),
                    'content' => function ($data) {
                        if ($data->viewed_user == '1')
                            return Yii::t('ticket', 'viewed_ticket');
                        else
                            return Yii::t('ticket', 'not_viewed_ticket');
                    }
                ],
                [
                    'class' => 'yii\grid\ActionColumn',
                    'template' => '{view} {delete}',
                    'header'=>Yii::t('ticket','title_actions_on_ticket'),
                    'buttons' => [
                        'delete' => function ($url, $model, $key) {
                            return Html::a('<span class="glyphicon glyphicon-remove"></span>', Url::to(['default/close','id' => $model->id]), [
                                'data-confirm' => Yii::t('ticket', 'confirm_closed_ticket'),
                            ]);
                        },
                        'view' => function ($url, $model, $key) {
                            return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', Url::to(['default/view','id' => $model->id]));
                        },
                    ],
                ],
            ],
        ]);

Controller Page
public function actionIndex() {
        $searchModel = new TicketSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        //Если простой пользователь, формируем ещё один провайдер для отображения тикетов от админа пользователю
        if (!Yii::$app->user->isAdmin()) {
            $newSearch = new TicketSearch();
            $newProvider = $newSearch->search(Yii::$app->request->queryParams, true);

            return $this->render('index', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,

                'newSearch' => $newSearch,
                'newProvider' => $newProvider,
            ]);
        }

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

TicketSearch.php model page
class TicketSearch extends Ticket
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'text', 'date','priority'] , 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params, $from = false)
    {
        //Если пользователь не администратор, отбираем только его тикеты
        if (!Yii::$app->user->isAdmin()) {

            //Если ищем тикеты от админа, адресованные конкретномю пользователю
            if ($from) {
                $condition = "to_users_id = ".Yii::$app->user->getId();
            }
            //Если ищем все тикеты авторизованного пользователя
            else {
                $condition = 'users_id = '.Yii::$app->user->getId();
            }

            $query = Ticket::find()->where($condition);
        }
        //Если админ, забираем все тикеты за исключение тикетов от админов
        else {
            $query = Ticket::find()->where('to_users_id is null');
        }

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'forcePageParam' => false,
                'pageSizeParam' => false,
                'pageSize' => 5
            ]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            return $dataProvider;
        }
        $filter = ['title' => $this->title,
                   'text' => $this->text,
                   'date' => $this->date,
                   'status' => $this->status,
                   'viewed_user' => $this->viewed_user,
                   'viewed_admin' => $this->viewed_admin,
                   'priority' => $this->priority];

        $query->andFilterWhere($filter);

        //$query->andFilterWhere(['like', 'name', $this->name]);

        return $dataProvider;
    }
}

There is an assumption that does not work, because the SearchTicket class is the same, but the search logic in grids is exactly the same. Really it is necessary to do the second same class to solve a problem?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
P
padlyuck, 2015-11-05
@EvgMul

You are right, this is due to the fact that the field names of the filters are the same. Look towards the filterUrl of the GridView. This way you can send filters to different actions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question