【问题标题】:Yii2 gridview filter with comparison operators带有比较运算符的 Yii2 gridview 过滤器
【发布时间】:2017-09-13 04:17:55
【问题描述】:

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

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

【问题讨论】:

  • searchModel中更改您的查询。
  • 你能给我举个例子吗?
  • 您总是想比较大于输入的值?
  • 是的。我如何更改我的搜索模型来做到这一点,我的归档名称是金额
  • 添加搜索模型。

标签: yii2 yii2-advanced-app


【解决方案1】:
query->andFilterWhere(['like', 'estimated_price', $this->estimated_price])
     ->andFilterWhere(['>', 'amount', $this->amount])
     ->andFilterWhere(['like', 'days', $this->days])
     ->andFilterWhere(['like', 'service_id', $this->service_id]);

如果您想在搜索结果中包含输入的金额,请使用>=

【讨论】:

  • 我再问一个问题。假设用户想要搜索大于 1000 的金额,并且还想要搜索小于 1000 的金额或 500 到 1000 之间的金额。我可以通过 >,
  • @sabithavarghese 是的,你可以。但这也取决于所需的输出类型。记住 between 需要四个操作数。
猜你喜欢
  • 2017-02-12
  • 1970-01-01
  • 2010-12-02
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
相关资源
最近更新 更多