yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.92k forks source link

GridView guide #14473

Open bscheshirwork opened 7 years ago

bscheshirwork commented 7 years ago

So... GridView have a top-level folder grid into framework like helpers, i18n, db, etc.

But information about advanced usage it is hidden in darkness.

Also so many issue arise on the related topic pjax for similar reason.

How about create a new paragraph in documentation to avoid new similar pjax-not-work-like issue?

example content:

How to add 2 GridView into ListView page and reload only part of content?

In this controller action realization in pjax request can be uses all of 3 models and calc a part of view 'view'

    /**
     * Displays a single main model and two grid of a child models.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);

        return $this->render('view', [
            'model' => $model,
            'params1' => [
                'searchModel' => $searchModel1 = new Model1Search(),
                'dataProvider' => $searchModel1->search(ArrayHelper::merge(Yii::$app->request->queryParams, [
                    $searchModel1->formName() => ['mainModelId' => $model->id],
                ])),
                'context' => $model,
            ],
            'params2' => [
                'searchModel' => $searchModel2 = new Model2Search(),
                'dataProvider' => $searchModel2->search(ArrayHelper::merge(Yii::$app->request->queryParams, [
                    $searchModel2->formName() => ['mainModelId' => $model->id],
                ])),
                'context' => $model,
            ],
        ]);
    }

where view call render another view _model1-index and _model2-index with pjax+GridView widgets each

DetailView::widget([
        'model' => $model,
        'attributes' => [
            [
                'format' => 'raw',
                'label' => Yii::t('part', 'One grid title'),
                'value' => function ($model) use ($params1) {
                    /** @var $this yii\web\View */
                    return $this->render('_model1-index', [
                        'context' => $model,
                        'searchModel' => $params1['searchModel'],
                        'dataProvider' => $params1['dataProvider'],
                    ]);
                }
            ],
            [
                'format' => 'raw',
                'label' => Yii::t('part', 'Two grid title'),
                'value' => function ($model) use ($params2) {
                    /** @var $this yii\web\View */
                    return $this->render('_model2-index', [
                        'context' => $model,
                        'searchModel' => $params2['searchModel'],
                        'dataProvider' => $params2['dataProvider'],
                    ]);
                }
            ],
        ]),
    ]);

Change controller action like in a next example for call only called content

    /**
     * Displays a single main model and two grid of a child models.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);

        $showPart1 = true;
        $showPart2 = true;
        if (Yii::$app->request->isAjax && isset(Yii::$app->request->queryParams['_pjax'])) {
            switch (Yii::$app->request->queryParams['_pjax']) {
                case '#p0' :
                    $showPart1 = true;
                    $showPart2 = false;
                    break;
                case '#p1' :
                    $showPart1 = false;
                    $showPart2 = true;
                    break;
            }
        }

        if ($showPart1) {
            $params1 = [
                'searchModel' => $searchModel1 = new Model1Search(),
                'dataProvider' => $searchModel1->search(ArrayHelper::merge(Yii::$app->request->queryParams, [
                    $searchModel1->formName() => ['mainModelId' => $model->id],
                ])),
                'context' => $model,
            ];
        }
        if ($showPart2) {
            $params2 = [
                'searchModel' => $searchModel2 = new Model2Search(),
                'dataProvider' => $searchModel2->search(ArrayHelper::merge(Yii::$app->request->queryParams, [
                    $searchModel2->formName() => ['mainModelId' => $model->id],
                ])),
                'context' => $model,
            ];
        }

        if ($showPart1 && !$showPart2) {
            //pjax widget call Yii::$app->end();
            $this->render('_model1-index', $params1 ?? []);
        } elseif (!$showPart1 && $showPart2) {
            $this->render('_model2-index', $params2 ?? []);
        }
        return $this->render('view', [
            'model' => $model,
            'params1' => $params1 ?? [],
            'params2' => $params2 ?? [],
        ]);
    }
dynasource commented 7 years ago

IMHO we should first start with a discussion about whether we want to keep supporting PJAX.

cebe commented 7 years ago

PJAX is definitively not going to be a core thing in 2.1

antick commented 7 years ago

Are you going to replace PJAX with something else or drop the idea entirely?

cebe commented 7 years ago

It's going to move to a separate repo, not being bound to the core, so development can be done by people who are using it and progress does not depend on core team.

bologer commented 7 years ago

@cebe why isn't it part of the core anymore?

samdark commented 7 years ago

@bologer multiple reasons:

  1. There is a goal for 2.1 to move all the JavaScript out of the core so there will be no need to install jQuery to create a pure REST API w/o any frontend.
  2. Core team members aren't using PJAX and there are too many issues with both original PJAX library code and integrations with Yii. It takes too much time to solve these while we can focus on more PHP things.
samdark commented 7 years ago

So no, we aren't going to document PJAX more than it's documented now and yes, it's a good idea to expand GridView docs with examples.