2017-09-13 115 views
0

如何过滤gridview结果的列大于指定值? 我的gridview有一个列的数量。当用户搜索为1000时,我想过滤结果为大于1000的数量。我该怎么做? 现在我的搜索模式是这样的。Yii2比较运算符的gridview过滤器

public function search($params) { 
      $query = Service::find(); 
      $dataProvider = new ActiveDataProvider([ 
       'query' => $query, 
       'sort' => ['defaultOrder' => ['id' => SORT_DESC, 
        ]] 
      ]); 

      $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, 
       'patient_id' => $this->patient_id, 
       'service' => $this->service, 
       'duty_type' => $this->duty_type, 
       'staff_manager' => $this->staff_manager, 
       'from_date' => $this->from_date, 
       'to_date' => $this->to_date, 
       'branch_id' => $this->branch_id, 
       'status' => $this->status, 
       'CB' => $this->CB, 
       'UB' => $this->UB, 
       'DOC' => $this->DOC, 
       'DOU' => $this->DOU, 
      ]); 


      $query->andFilterWhere(['like', 'estimated_price', $this->estimated_price]) 
        ->andFilterWhere(['like', 'amount', $this->amount]) 
        ->andFilterWhere(['like', 'days', $this->days]) 
        ->andFilterWhere(['like', 'service_id', $this->service_id]); 

      return $dataProvider; 
    } 
+0

在'searchModel'中更改您的查询。 –

+0

你能给我举个例子吗? – SaabzCoder

+0

你总是想比较大于输入的值吗? –

回答

0
query->andFilterWhere(['like', 'estimated_price', $this->estimated_price]) 
    ->andFilterWhere(['>', 'amount', $this->amount]) 
    ->andFilterWhere(['like', 'days', $this->days]) 
    ->andFilterWhere(['like', 'service_id', $this->service_id]); 

如果你想在搜索结果中输入量,使用>=

+0

是的。这工作正常。 – SaabzCoder

+0

我应该再问一个问题。假设用户想要搜索的数量大于1000,并且还希望搜索数量少于1000或数量在500和1000之间。我可以通过>,<,在同一个字段之间执行此操作吗? – SaabzCoder

+0

@sabithavarghese是的,你可以。但它也取决于所需输出的类型。记住之间需要四个操作数。 –