Yii2:在GridView小部件的顶部显示一些记录

时间:2017-11-23 10:41:09

标签: php yii yii2

在Yii2应用程序中,我有可以属于用户的模型Document。属性设置为owner_id字段。 在视图中,我使用GridView小部件显示文档列表。默认排序是document_id

每个用户都能看到所有文件(如果特定文件不属于他,则为活动)。

但我需要在GridView的顶部显示属于当前登录用户的文档。我怎么能这样做?

我想我应该对Document::search()方法进行一些更改,但不能找出我应该做的事情。

这是我的控制器动作:

public function actionIndex()
{
    $modelFullName = $this->modelFullName;

    $model = new $modelFullName();
    $dataProvider = $model->search(Yii::$app->request->queryParams);

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

部分观点:

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel'  => $model,
    'columns' => [
        [
            'attribute' => 'document_id',
        ],
        [
            'attribute' => 'status',
            'format' => 'raw',
            'value' => function($model) {
                return $model->statusString;
            },
        ],
        [
            'attribute' => 'title',
        ],
        [
            'attribute' => 'date_created'
        ],
        [
            'attribute' => 'client_id',
            'value' => function($model) {
                return $model->client ? $model->client->title : '';
            },
            'filter' => ArrayHelper::map(Clients::find()->all(), 'client_id', 'title')
        ],
        [
            'attribute' => 'project_id',
            'value' => function($model) {
                return $model->project ? $model->project->title : '';
            },
            'filter' => ArrayHelper::map(Projects::find()->all(), 'project_id', 'title')
        ],
        [
            'class' => yii\grid\ActionColumn::className(),
            'template' => '{view} {delete}',
            'buttons' => [
                'view' => function ($url, $model) {
                    return $this->context->getBtn('view', $model);
                },
                'delete' => function ($url, $model) {
                    if (Yii::$app->user->can('deletePrsSum')) {
                        return $this->context->getBtn('delete', $model);
                    }
                },
            ],
            'visibleButtons' => [
                'update' => Yii::$app->user->can('updateDocument'),
                'delete' => Yii::$app->user->can('deleteDocument'),
            ]
        ],
    ],
]);

Current Document :: search()实现:

public function search($params)
{
    $query = self::find();

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => [
            'defaultOrder' => ['document_id' => SORT_ASC]
        ]
    ]);

    $this->load($params);

    if (!$this->validate()) {
        return $dataProvider;
    }

    if ($this->client_id) {
        $query->andFilterWhere([
            'client_id' => $this->client_id,
        ]);
    }

    if ($this->project_id) {
        $query->andFilterWhere([
            'project_id' => $this->project_id,
        ]);
    }

    return $dataProvider;
}

更新 所以我设法做到了这一点:

public function search($params)
    {
        $userId = Yii::$app->user->identity->user_id;
        $query = self::find()->select(
            "*, IF(owner_id={$userId},(0),(1)) as sort_order"
        )->orderBy([
            'sort_order' => SORT_ASC,
            'document_id' => SORT_ASC
        ]);
        //further code...

1 个答案:

答案 0 :(得分:0)

在搜索功能中添加过滤器,以使用登录的人/用户的ID进行过滤:  $query->andFilterWhere(['owner_id' => Yii::$app->user->identity->id]);