【问题标题】:Yii2 gridview custom column valueYii2 gridview自定义列值
【发布时间】:2016-10-12 10:34:22
【问题描述】:

我想在一列中显示我的交易表中的一个/所有帐户的总余额。余额列应显示加上前一行总余额的余额。 我的gridview代码是

<?php
     $gridColumns = [
        ['class' => 'yii\grid\SerialColumn'],
                        'account_no',
                        'credit',
                        'debit',
                        [
                            'label' => 'Balance',

                            'value' => function ($model) {
                                return $model::Balance();
                            }
                        ],
                        'created_date:date',
     ];
?>

我的模型中的代码如下所示。我可以通过硬编码 Deptransaction::findOne(1) 来获得第一行的值。

public static function Balance()
    {

            $data = DepTransaction::find();

                if($data->credit != 0){ 
         $cap_bal = $cap_bal +($data->credit - $data->debit);            
                }

                if($data->debit != 0){  
                    $int_bal = $int_bal + ($data->credit - $data->debit);  

                }
            $total = $cap_bal+$int_bal;

        return $total;
    }

我想这样显示结果

我在我的 gridview 中尝试了以下代码,但它仅显示单个行的余额

'value' => function($data) {
                                if($data['head_type']=="CAP"){ 
                                    $cap_bal = $cap_bal +($data['credit']-$data['debit']);           
                                }

                                if($data['head_type']=="INT"){  
                                    $int_bal = $int_bal+($data['credit']-$data['debit']);  
                                }
                                $total = $total + $cap_bal+$int_bal;
                                return $total;
                            },

【问题讨论】:

  • 您找到解决问题的方法了吗?
  • 运气好能得到答案吗?

标签: gridview yii2


【解决方案1】:

在 GridView 中:

<?php
 $gridColumns = [
    ['class' => 'yii\grid\SerialColumn'],
    'account_no',
    'credit',
    'debit',
    [
       'label' => 'Balance',
       'value' => function ($model) {
           return $model->Balance();
       }
     ],
     'created_date:date',
 ];
?>

型号:

public function Balance()
{

    $data = DepTransaction::findOne($this->id);

    if($data->credit != 0){ 
       $cap_bal = $cap_bal +($data->credit - $data->debit);            
    }

    if($data->debit != 0){  
       $int_bal = $int_bal + ($data->credit - $data->debit);  
    }

    $total = $cap_bal+$int_bal;

    return $total;
}

【讨论】:

  • 谢谢兄弟对我的帮助。
【解决方案2】:

试试这个

public static function Balance()
{

        $data = DepTransaction::find($this->id);

            if($data->credit != 0){ 
     $cap_bal = $cap_bal +($data->credit - $data->debit);            
            }

            if($data->debit != 0){  
                $int_bal = $int_bal + ($data->credit - $data->debit);  

            }
        $total = $cap_bal+$int_bal;

    return $total;
}

【讨论】:

  • 收到“不在对象上下文中使用 $this”错误。
  • 从 Balance 函数中移除 static 关键字
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多