Answer the question
In order to leave comments, you need to log in
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;
}
],
....
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'label' => 'Сумма',
'value' => function ($model) {
$sum = 0;
foreach ($model->infocosttcs as $info) {
$sum += $info['Cash'];
}
return $sum;
}
],
...
Answer the question
In order to leave comments, you need to log in
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()
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'myOwnValue'
...
// В модели
public function getMyOwnValue() {
//Я считаю что хочу
return $counted;
}
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',
],
...
?>
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);
}
}
<?= 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 questionAsk a Question
731 491 924 answers to any question