Yii2 GridView attribute format defined by different attribute value - php

I would like to define the format of an attribute in GridView based on a db field value. I was trying for example like this:
'format' => function ($model) {return $model->format;}, // it should return 'boolean'
but I have tried many other ways also, but it's not working. I'm getting:
PHP Notice – yii\base\ErrorException
Trying to get property of non-object
If I'm simply returning format as an attribute, it's working without any issues. It seems that it's not accepting it in the format section.
Is it possible anyways what I'm trying to achieve? Can you please point me to the right direction?

Closures for format are not supported. You may use raw format and do formatting in Closure for value:
[
'attribute' => 'name',
'type' => 'raw',
'value' => function ($model) {
$format = 'as' . ucfirst($model->format);
return Yii::$app->formatter->$format($model->name);
},
],

Refer Yii2 Formatter
Example:
<?= GridView::widget([
'id' => 'grid-list',
'dataProvider' => $dataProvider,
[
'attribute' => 'format_date',
// 'format' => 'raw',
'value' => function ($model) {
// Here use \Yii::$app->formatter->asDate();
// Ex: return \Yii::$app->formatter->asDate($model->format_date);
return \Yii::$app->formatter->asDate($model->format_date);
},
],
]) ?>

Related

Kartik DateRangePicker with two attributes cause invalidDateFormat and NaN on range date selection

I've a question about the Kartik DateRangePicker Widget that i use in my gridview for filter some results.
in my SearchModel i've created two attributes
public $date_start;
public $date_end;
i use these for filter a field in database 'insert_date'.
In my view, as grid configuration, i've set these options
[
'attribute' => 'insert_date',
'options' => ['class' => 'gridview-date-column'],
'filter' => DateRangePicker::widget([
'model' => $searchModel,
'name' => 'insert_date',
'attribute' => 'insert_date',
'startAttribute' => 'date_start',
'endAttribute' => 'date_end',
'convertFormat'=>true,
'pluginOptions' => [
'opens'=>'right',
'locale' => [
'cancelLabel' => 'Clear',
'format' => 'd-m-Y',
],
]
]),
'format' => ['date', Yii::$app->formatter->datetimeFormat],
'contentOptions'=>['style'=>'min-width: 200px;']
],
By default $date_start and $date_end haven't a value, so , when i enter in my view and try to filter this field i get an 'invalidDate' error and a series of NaN on the calendars.
This is fixed if i set a value for these two fields or if remove them from the configuration ( so i can only use insert_date attribute as string with these two ranges for filtering ).
Looking in the plugin repository i've found the same case and as response of the author
This problem occurs because you have an invalid date format for the data that does not match the plugin's format.
But as empty these fields can never have a correct data format.
Someone had the same problem?
Thanks in advance for all the responses.
I've got the solution:
In Kartik plugin there's a Behavior Model that i can include in my searchModel:
use kartik\daterange\DateRangeBehavior;
Then i can instantiate it overriding behaviors() method:
public $date_start;
public $date_end;
public function behaviors() {
return [
[
'class' => DateRangeBehavior::className(),
'attribute' => 'insert_date',
'dateStartAttribute' => 'date_start',
'dateEndAttribute' => 'date_end',
'dateStartFormat' => 'd-m-Y',
'dateEndFormat' => 'd-m-Y',
]
];
}
and in the search() method:
public function search($params)
{
// Some other filters
if($this->date_start && $this->date_end) {
// filtered query
}
}
Hope this can help someone else.

Same value for all columns fields yii2

I have gridview with some columns. I want to create one colums like
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['header' => 'Manager',
//'value' => 'first'],
Column name is Manager and all fields equels 'first'? How could I do this?
Based on info provided, this is easy to achieve with custom grid column:
<?php
namespace app\components;
class CommonValueColumn extends Column
{
public $commonValue = 'Default value for common value';
protected function renderDataCellContent($model, $key, $index)
{
return $commonValue;
}
}
Then use it in GridView widget like this:
'columns' => [
// ...
[
'class' => 'app\components\CommonValueColumn',
'header' => 'Manager',
'commonValue' => 'First',
],
// ...
],
Note that if the manager is a model attribute and the value needs to be taken from database, this is a wrong way to do it.
Information about GridView widget is available in the official docs.

how to use function in DetailView yii2

**i use model function in detail view how i use function in detail view **
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'title',
'demand',
'sell_for',
'property_category',
'detail',
[
'attribute' => 'dealer_id',
'format'=>'raw',
'value'=> function ($model) {
return Dealer::getName($model->dealer_id);
}
],
],
]) ?>
**i use model function in detail view how i use function in detail view **
In DetailView value don't need anonymous function but only the assign ..
[
'attribute' => 'dealer_id',
'format'=>'raw',
'value'=> Dealer::getName($model->dealer_id);
],
see yii2 doc
return Dealer::getName($model->dealer_id)
Write out the code inside the getName function. If you want to get the name of the dealer using its id, then I think you should use the yii\db\ActiveQuery to sort that out. Lemme see your getName function

Access variable in Gridview function yii2

I am trying to access a variable on view file into Gridview but it is throwing error that it should be array but null given i am declaring $totalDays on top of my view file and i am using it in Gridview like below
[
'attribute' => 'class_id',
'format' => 'raw',
'label' => "Class Date",
'value' => function ($model) {
array_push($totalDays,$model->class->date);
return $model->class->date;
},
'footer'=> '<span>Total Days</span>',
],
But it throws following error
array_push() expects parameter 1 to be array, null given
To explain, $totalDays is not available in the widget because the whole function only gets run when the widget is rendered, $totalDays is no longer declared. As #arogachev hinted above, you will need to make $totalDays in your model, then you can access it. Try this in your model;
public function getTotalDays(){
//Your logic here to generate totalDays
return $totalDays;
}
Then you can use it in your view like this;
[
'attribute' => 'class_id',
'format' => 'raw',
'label' => "Class Date",
'value' => function ($model) {
array_push($model->totalDays, totalDays,$model->class->date);
return $model->class->date;
},
'footer'=> '<span>Total Days</span>',
],
Insert use after function signature in value callable function:
[
'attribute' => 'class_id',
'format' => 'raw',
'label' => "Class Date",
'value' => function ($model) use($totaleDays) {
array_push($totalDays,$model->class->date);
return $model->class->date;
},
'footer'=> '<span>Total Days</span>',
],
As #Fabrizio said. Insert use after function signature in value callable function:
'value' => function ($model) use($totaleDays) {
array_push($totalDays,$model->class->date);
return $model->class->date;
},
For multiple value to pass in to value function you can use as below.
'value' => function ($model) use($var1, $var2....., $varN) {
array_push($totalDays,$model->class->date);
return $model->class->date;
},
Like this use($var1, $var2....., $varN)
While you can definetely pass this variable through use section of closure, it's wrong in your code. MVC principle is violated, because view it's only for display, and you exposing logic such as array_push.
I'd recommend to refactor it and place data calculation in model, so you can simply call return $model->yourMethod(); and it will return desired data.

Yii2 how to anchor the value of the relationship in the GridView

I am beginner. I explain the topic:
there is this relationship in the Ticket model:
public function getTyp()
{
return $this->hasOne(Typology::className(), [ 'id' =>'typ_id']);
}
and in the ticket table there is the typ_id column (it is in relationship with the id of the Typology table).
In the view views/ticket/index.php there is GridView::widgetwith these columns:
[
'attribute' => 'typ_id',
'value' => 'typ.typology'
],
I want to anchor the value of the relationship.
I have tried this:
[
'attribute' => 'typ_id',
'value' => function ($model) {
return Html::a (
'typ.typology',
'/typology/view?id='.$model->typ_id
);
}
]
but it doesn't work
someone can help me?
Html::a() interprets typ.typology as raw string. Use $model in value closure to get necessary property through relation.
Also instead of manually concatenate the url with its parameters, just pass them in array (see Url::to() to understand how link is constructed).
[
'attribute' => 'typ_id',
'value' => function ($model) {
return Html::a($model->typ->typology, ['/typology/view', 'id' => $model->typ_id]);
},
],

Categories