如何在 Yii2 的 GridView 小部件列中禁用对几列的搜索,但保持启用排序

D55*_*555 2 php yii2

我想在所有列上保持启用排序,但在少数特定字段上禁用过滤。

例如,我想disablefirst_name列进行过滤,但enable对同一列进行排序。我怎样才能做到这一点?

以下代码禁用排序和过滤。

我的 GridView 代码是:

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            [
                'attribute'=>'First Name',
                'value'=> 'first_name',
                'filter'=>false,
                //'enableSorting'=>true
            ],
            //'first_name',
            'last_name',
            'street',
            'zipcode',
            'company',
            'created_at',

          ],
        ],
    ]); ?>
Run Code Online (Sandbox Code Playgroud)

还有我search在 CustomerSearchModel:: 中的函数代码

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

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            //'sort' => ['attributes' => ['first_name','last_name','street','zipcode','company','created_at']],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'discount' => $this->discount,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'first_name', $this->first_name])
            ->andFilterWhere(['like', 'last_name', $this->last_name])
            ->andFilterWhere(['like', 'street', $this->street])
            ->andFilterWhere(['like', 'zipcode', $this->zipcode])
            ->andFilterWhere(['like', 'company', $this->company]);

        return $dataProvider;
    }
Run Code Online (Sandbox Code Playgroud)

rob*_*sch 6

如果您想删除所有搜索输入,只需删除网格视图配置中的过滤器模型即可:

'filterModel' => null,
Run Code Online (Sandbox Code Playgroud)

您应该能够删除$query->andFilterWhere()CustomerSearchModel 中不需要的声明。

如果您只想删除某些搜索输入,请告诉搜索模型某个属性在规则中不是“活动的”:

class CustomerSearchModel {
...
public function rules() {
    return [
        // removed 'first_name' from the safe attributes:
        [['last_name','street','zipcode','company','created_at'], 'safe'], // <--- 'safe'
    ];
}
...
}
Run Code Online (Sandbox Code Playgroud)

虽然声明需要使用“safe”,但我写了“active”,因为 GridView 调用DataColumn::renderFilterCellContent()(源代码:此处)检查某个属性是否为活动属性$model->isAttributeActive($this->attribute)。这些是当前场景的一部分(上述规则声明适用于默认场景,标记为“安全”的属性将是活动的)。

这样您就不需要添加'filter' => false列定义。现在标准配置应该足够了:

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'first_name',
            'last_name',
            'street',
            'zipcode',
            'company',
            'created_at',
          ],
        ],
    ]); ?>
Run Code Online (Sandbox Code Playgroud)

的过滤器输入first_name将消失,排序选项仍然存在。