dsgw8802 2019-05-02 07:41
浏览 298
已采纳

在Yii中进行gridview查询后操作数据

I have a Gridview with data from a table which normally is ok as it is. But in some cases i have to manipulate the data, and i can't just do it from the query as there are many different cases and requires complex calculations.

Example: base data loaded from db:

date        price   buy_price
2019-05-01  15.75   10
2019-05-02  20.15   10.50

same data which passed through complex calculations

date        price   buy_price
2019-05-01  3.75   3
2019-05-02  4.70   3.10

I would like to use GridView cause the ajax filtering and sorting are really useful.

How can i manipulate the data AFTER the query the Gridview executes based on the DataProvider?

  • 写回答

1条回答 默认 最新

  • doucheng7808 2019-05-02 09:23
    关注

    As an example in Yii2 Documentation about GridvViews says, you can manipulate any column to your need in the following way:

    echo GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        // Simple columns defined by the data contained in $dataProvider.
        'data',
        [
            'attribute' => 'price',
            'value' => function ($model) {
                return $model->getValue();  // Here you can manipulate the data as you wish.
            },
        ],
        [
            'attribute' => 'buy_price',
            'value' => function ($model) {
                return $model->buy_price * 2;  //Here you can manipulate the data as you wish.
            },
        ],
    ],]); 
    

    After manipulating data, you can still use the sort or the ajax filtering.

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

报告相同问题?

悬赏问题

  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现