Is there a way to pass custom data to DataColumn

Hi there,

Is there something like this:




[

      'class' => 'yii\grid\DataColumn',

      'value' => function ($xxx){

            // Do something with $xxx

       }

],



Obviously, it doesn’t work. I know for $model, $key, $index, $column, but I need to pass some custom data not saved in model’s table.

Thanks.

Ok, I figured out:




[

      'class' => 'yii\grid\DataColumn',

      'value' => function ($model){

           return $model->{"someProperty"};

       }

],



someProperty is a property in Model.

great:)

Yes. But there is a small problem with passing data to the property.




    $searchModel = new VehicleSearch();        

    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);



I can pass it to VehicleSearch, but its needed to pass to Vehicle. Anyway, I tried this:




    $searchModel = new VehicleSearch();        

    $searchModel->setCustomProperty("some value");

    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);



And in my VehicleSearch I’ve created setter function because VehicleSearch extends Vehicle:




public function setCustomProperty($value){

    $this->customProperty=$value;

    return true;

}



And it does not work…

Hi, do you want to change property in searchModel? if yes, this $searchModel->search(Yii::$app->request->queryParams); will reset all values, so you have to change property from Yii::$app->request->queryParams

Hi there. Thanks for the answer.

No. I don’t want to pass data to searchModel. Because when I execute:

print_r($model) in my DataColumn it shows Model object not searchModel. So I need to pass data to my Model (not searchModel). in my controller’s action there is searchModel, so I don’t know how to do that.

Ok. I figured out that I need to find a way to pass some custom data to ActiveDataProvider. Otherwise, I could not do what I want or I should add a new column in my database table, and I would not like to do that, because my case is really specific.

Anyone?

Another solution is to do something like this:


$session = new \yii\web\Session;

$session->open();

$session->set("x", $y);

$session->close();

And access it in DataColumn…

But I don’t know how smart is this way.