dongshi9719 2018-10-23 09:12
浏览 210

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

I want to keep sorting enabled on all columns but disable filtering on few specif fields.

For example, I want to disable filtering on first_name column but enable sorting on the same column. How can I do that?

Following code disables the sorting and filtering both.

My code for GridView is:

<?= 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',

          ],
        ],
    ]); ?>

Also my search function code in the 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;
    }
  • 写回答

1条回答 默认 最新

  • dspvin19712 2018-10-23 10:22
    关注

    Just remove the filter model in the grid view configuration if you'd like to remove all search inputs:

    'filterModel' => null,
    

    You should than be able to remove not required $query->andFilterWhere() declarations in your CustomerSearchModel.

    If you'd like to remove only certain search inputs, tell the search model that an attribute is not 'active' within the rules:

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

    Though 'safe' needs to be used for the declaration, I wrote 'active' since the GridView calls DataColumn::renderFilterCellContent() (source code: here) which checks if an attribute is an active one with $model->isAttributeActive($this->attribute). And those are the ones that are part of the current scenario (the above rule declaration is for the default scenario, attributes marked as 'safe' will be the active ones).

    With this you don't need to add 'filter' => false to the column definition. The standard configuration should be sufficient now:

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

    The filter input for first_name will disappear and the sort option still exists.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办