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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵