GridView搜索过滤器未显示Yii2

时间:2016-05-18 19:01:16

标签: php gridview yii2

我在视图中使用以下参数创建了GridView小部件:

<?= GridView::widget([
        'dataProvider' => $provider,
        'filterModel' => $searchModel,
        'summary' => '<br>',
        'showOnEmpty' => false,
        'columns' => [
            [
                'attribute' => 'name',
                'value' => function ($model) {
                    return substr($model->name, 0, 50);
                },
            ],
            [
                'label' => "Ім'я користувача",
                'attribute' => "user.name",
                'value' => function ($model) {
                    return substr($model->user->name, 0, 50);
                },
            ],
            [
                'label' => 'Назва предмету',
                'attribute' => "subjects.name",
                'value' => function ($model) {
                    return substr($model->subjects->name, 0, 50);
                },
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'template' => '{view} {delete}'
            ]
        ],
    ]) ?>

我的控制器有这样的代码:

        $searchModel = new DocumentsSearch();
    $provider = $searchModel->search(Yii::$app->request->queryParams);

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

此外,我正在使用具有以下描述的DocumentSearch模型:

class DocumentsSearch extends Documents
{
    public $username;
    public $subject_name;

    public function rules()
    {
        return [
            [['username', 'subject_name'], 'safe']
        ];
    }

    public function search($params)
    {
        $query = Documents::find()
            ->joinWith(['user', 'subjects']);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
            'pagesize' => 30,
        ],
    ]);

    $dataProvider->sort->attributes['user.name'] = [
        'asc' => ['user.name' => SORT_ASC],
        'desc' => ['user.name' => SORT_DESC],
    ];

    $dataProvider->sort->attributes['subjects.name'] = [
        'asc' => ['subjects.name' => SORT_ASC],
        'desc' => ['subjects.name' => SORT_DESC],
    ];


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


    $query->andWhere('user.name LIKE "%' . $this->name . '%" ');

    $query->andWhere('user.name LIKE "%' . $this->username . '%" ');

    $query->andWhere('subjects.name LIKE "%' . $this->subject_name . '%" ');


    return $dataProvider;

    }

}

DocumentSearch扩展了文档模型。我接下来的关系是:

public function getUser()
{
    return $this->hasOne(User::className(), ['id' => 'owner_id']);
}

public function getSubjects()
{
    return $this->hasOne(Subjects::className(), ['id' => 'subject_id']);
}

所有这些都给了我下一个结果:

ScreenSh

在询问之前我尝试使用filter param为'filter'=>Html::input('text','DocumentSearch[subjects.name]')等每列创建输入 它触发JS发送请求,但if (!($this->load($params) && $this->validate()))方法明显返回false。

1 个答案:

答案 0 :(得分:1)

尝试在规则

中使用相应的名称
         [
            'label' => "Ім'я користувача",
            'attribute' => "username",
            'value' => function ($model) {
                return substr($model->user->name, 0, 50);
            },
        ],
        [
            'label' => 'Назва предмету',
            'attribute' => "subject_name",
            'value' => function ($model) {
                return substr($model->subjects->name, 0, 50);
            },
        ],

请参阅本指南http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#filtering-data

 // only fields in rules() are searchable

或者您必须为计算或相关字段扩展搜索模型,最终在规则中添加适当的值