In detailview (yii2) will make the function value?

There is a table of records and table information for each expense report.
You need to view the report table to select the amount of specific field information.
In index.php done
<?= GridView::widget([
 'dataProvider' =--> $dataProvider,
 'filterModel' => $searchModel,
 'columns' => [
....
[
 'label' => 'Amount',
 'value' => function ($model) {
 $sum = 0;
 foreach ($model->infocosttcs as $info) {
 $sum += $info['Cash'];
}
 return $sum;
}
],
....

And view.php does Not work
<?= DetailView::widget([
 'model' =--> $model,
 'attributes' => [
[
 'label' => 'Amount',
 'value' => function ($model) {
 $sum = 0;
 foreach ($model->infocosttcs as $info) {
 $sum += $info['Cash'];
}
 return $sum;
}
],
...

In DetailView as not to push the function value ?
July 9th 19 at 12:55
4 answers
July 9th 19 at 12:57
Solution
No, you can't (and IMHO - this omission).

You can compute the value in advance, and give it in value.

But! In your code go mix presentation logic and business logic. For good the best in a model method to do something like $model->getTotalSum() and in the presentation to make 'value' => $model->getTotalSum()
thanks for the advice, I'll do that ! - watson_Torphy commented on July 9th 19 at 13:00
July 9th 19 at 12:59
<?= DetailView::widget([
 'model' =--> $model,
 'attributes' => [
'id',
'myOwnValue'
...
// In the model
public function getMyOwnValue() {
 //I believe what you want
 return $counted;
}
July 9th 19 at 13:01
To create a new class and inherit from DetailView

class DetailViewext extends \yii\widgets\DetailView
{
public $param;
}

then in the view
$param - there are already can even calculate in advance and pass

'model' => $model,
'param'=>$param,
'attributes' => [
[
'label' => 'new parameter',
'value' =>$param,
'format'=>'raw',

],

...

?>
July 9th 19 at 13:03
Also faced with this problem. Did your widget a little tweak DetailView in analogy to how it is done in GridView.
Create a class app\components\widgets\DetailWidget and slightly change the method renderAttribute:
<?php
namespace app\components\widgets;

use Yii;
use yii\base\Arrayable;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Inflector;
use yii\i18n\Formatter;

class DetailView extends \yii\widgets\DetailView
{
/**
 * @inheritdoc
*/
 protected function renderAttribute($attribute, $index)
{
 if (is_string($this--->template)) {
 $captionOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'captionOptions', []));
 $contentOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'contentOptions', []));
 if ($attribute['value'] instanceof Closure) {
 $value = call_user_func($attribute['value'], $model);
 } else {
 $value = $this->formatter->format($attribute['value'], $attribute['format']);
}
 return strtr($this->template [
 '{label}' => $attribute['label'],
 '{value}' => $value,
 '{captionOptions}' => $captionOptions,
 '{contentOptions}' => $contentOptions,
]);
}
 return call_user_func($this->template, $attribute, $index, $this);
}
}


Now using our new widget app\components\widgets\DetailView instead of yii\widgets\DetailView, as value to set its function such as me, it looks like this:
<?= DetailView::widget([
 'model' =--> $model,
 'attributes' => [
'id',
'title',
'author',
'year',
'description',
'format',
[
 'attribute' => 'categories',
 'format' => 'html',
 'value' => function ($model) {
 /** @var app\models\library\Book $model */
 $items = $model->getCategories()->all();
 $result = ";
 foreach ($items as $item) {
 $result .= "<p>$item->name</p>";
}
 return $result;
}
]
],
 ]) ?>

Find more questions by tags Yii