L
L
Lev K2016-05-19 18:18:57
Yii
Lev K, 2016-05-19 18:18:57

How in detailview (yii2) I will make a function in the value?

There is a table of reports and a table of information for each report expense.
It is necessary to select the sum of a certain field of information in the view of the reporting table.
In index.php is done like this

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
....
[
                'label' => 'Сумма',
                'value' => function ($model) {
                    $sum = 0;
                    foreach ($model->infocosttcs as $info) {
                        $sum += $info['Cash'];
                    }
                    return $sum;
                }
            ],
....

And in view.php It doesn't work
<?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            [
                'label' => 'Сумма',
                'value' => function ($model) {
                    $sum = 0;
                    foreach ($model->infocosttcs as $info) {
                        $sum += $info['Cash'];
                    }
                    return $sum;
                }
            ],
...

In DetailView, how not to shove a function into value ?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
mitaichik, 2016-05-19
@Leffken

No, you can not (and IMHO - this is an omission).
You can calculate the value in advance, and give it to value.
But! In your code, we are going to mix presentation logic and business logic. For good, it's better to make a method in the model like $model->getTotalSum() and in the view do 'value' => $model->getTotalSum()

N
Nikita, 2016-05-19
@bitver

<?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'myOwnValue'
...
// В модели
public function getMyOwnValue() {
  //Я считаю что хочу
  return $counted;
}

Z
zobnin, 2017-11-01
@zobnin

Create a new class and inherit from DetailView
<?namespace frontend\expanding;
class DetailViewext extends \yii\widgets\DetailView
{
public $param;
}
further in the
$param view - here you can already calculate something in advance and pass
<?= DetailView::widget([
'model' => $model,
'param'=>$param,
'attributes' => [
[
'label ' => 'parameter new',
'value' =>$param,
'format'=>'raw',
],
...
?>

A
Alexander Lykasov, 2017-12-13
@lykasov-aleksandr

Also faced this problem. I made my widget by slightly tweaking the DetailView by analogy with how it is done in the GridView.
We create our own class app\components\widgets\DetailWidget and slightly change the renderAttribute method:

<?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, you can set your own function as value, for example, it looks like this for me:
<?= 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;
                }
            ]
        ],
    ]) ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question