drcrc28428 2018-06-08 11:49
浏览 90
已采纳

Yii2网格:按计算的数值或公式列排序

I'm using GridView (generated by Gii) with ActiveRecord. Let's say I have a table with a column named "number". I added a function to the model, it's something like this:

public function getCos()
{
return cos($this->number);
}

I can't see how should I sort columns like this one, with numeric calculated values which are not just the concatenation of two columns, like the case described here:

https://www.yiiframework.com/wiki/621/filter-sort-by-calculatedrelated-fields-in-gridview-yii-2-0

Values could be calculated in the view instead of model:

'value'=>function($model, $key, $index, $widget){return cos($model->number);

But the problem is the same, I can't see how to sort this kind of column.

Thank you.

  • 写回答

1条回答 默认 最新

  • dreamMyDream2014 2018-06-08 13:14
    关注

    Follow the guide you mentioned but add an attribute to the dataprovider's sort like this:

    'the_cosine' => [
        'asc' => 'COS(number) ASC',
        'desc' => 'COS(number) DESC',
    ]
    

    That allows you to sort on a database level.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败