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;
}