简体   繁体   中英

Yii2 Gridview current row values

I am new to Yii2. I have a gridview where 2 columns are auto-generated (not in model class). Every row has a button which when clicked, I want to access the current row cells values and do some calculation.

I am unable to use $data as it only refers to current model row. below is my code:

<?php 

echo GridView::widget([
    'dataProvider' => $model->getInsuredCompanyVehiclesArrayDataProvider(),
    'filterModel' => $model->getInsuredCompanyVehiclesArrayDataProvider(),
    'columns' => [                                          
        [
            'label' => 'Amount',
            'format' => 'raw',
            'value' => function ($data) {
                return $data->rate * Yii::$app->session->get('motorInsuranceEntry')->sumInsured/100;
            },
        ],                      
        [
            'label'=>'Total',
            'format' => 'raw',
            'value' => function ($model, $key, $index, $column) {
                //return $data->rate * Yii::$app->session->get('motorInsuranceEntry')->sumInsured/100 + $model->amount;

                return '' . $column ;                       
            },
        ],                                                  
    ],
]); ?>

The amount and Total colum values are dynamically calculated but I am unable to access these values in other cells :(

As you might already know due to that the columns are dynamically you couldn't access them from other columns.

I would create functions for both dynamically columns (Amount and Total) in the model class to calculate your dynamically columns. And after you click your button in the row refresh the grid via pajax.

You could also add additional data to the gridview like metioned here

by either extend your gridview model or use inline functions (req. PHP 5.3+). But then you also have to calculate each column value again if they are completely dynamically by each row.

But i guess your simple mistake is that you use one time

'value' => function ($data) {

and the other time

'value' => function ($model, $key, $index, $column) {

Both functions are correct. One time you pass the model to the function by the parameter "name" $data and the other time with the "name" $model. Inside the function you only have access to the parameters which are passed to the function.

Your second inline function code should use $model instead of $data (first parameter name is the model).

 return $model->rate * Yii::$app->session->get('motorInsuranceEntry')->sumInsured/100 + $model->amount;

But as i mentioned i would create a function in your Model class

public function getAmount() {
    return $this->rate * Yii::$app->session->get('motorInsuranceEntry')->sumInsured/100;
}

What i don't understand is that your Total is just the amount * 2 ? But i guess this was just some testing code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM