【问题标题】:YII2 gridview sort and filter sum of values of two columnsYII2 gridview排序和过滤两列值的总和
【发布时间】:2015-08-07 12:50:34
【问题描述】:

我成功地在 gridview 中显示了 3 列。 attr1attr2 和 2 个属性的总和。 attr1attr2 来自数据库,第三列是它们的总和。如何使排序和过滤工作。

这是网格视图。

[
  'attribute'=>'total',
  'label'=>'TOTAL',
  'value'=>function($model){
  return $model->attr1+ $model->attr2;
  },
],

我已经在搜索模型中尝试过这个

$dataProvider->sort->attributes['total'] = [
        'asc' => [$this->attr1 + $this->attr1 => SORT_ASC],
        'desc' => [$this->attr1 + $this->attr2 => SORT_DESC],
        ];

$query->andFilterWhere([
            'total' => $this->total,
        ]);

【问题讨论】:

    标签: gridview yii2


    【解决方案1】:

    你可以试试这个,

    $query->andFilterWhere(['like', 'ADD(table_name.column1, table_name.column2)', $this->total])
    

    可以工作吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 1970-01-01
      • 2011-01-31
      相关资源
      最近更新 更多