Reputation: 2966
following is a detail view widget
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'name',
'entity_name',
'voucher_category',
'credit',
'debit',
'remarks:ntext',
'posting_date',
'payment.method',
[
'label' => 'Reference Date',
'value' => $model->reference_date !=NULL ? $model->reference_date: 'Not Defined',
],
'voucher_no',
],
]) ?>
what i want is to check that
if($model->voucher_category ==0)
{
return "Income Voucher";
}
elseif($model->voucher_category ==1)
{
return "Exepense Voucher";
}
else
{
return "General Voucher";
}
ie, i want to check a condition based on which a value should be displayed in the view. How can i do this in a detail view widget?
Upvotes: 3
Views: 7387
Reputation: 3390
worked for me
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'title',
[
'attribute' => 'availability',
'value' => (($model->availability ==1) ? "Available":'Not Available'),
],
],
]) ?>
Upvotes: 0
Reputation: 2966
Incognito Skull's answer is great, but i have found another way to do it. With the help of function inside model. Inside View
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
...
array(
'format' => 'text',
'value'=>$model->getvoucher(),
'attribute'=>'voucher_category_id',
),
],
]) ?>
Inside Model
public function getvoucher()
{
if($this->voucher_category ==0)
{
return "Income Voucher";
}
elseif($this->voucher_category ==1)
{
return "Exepense Voucher";
}
else
{
return "General Voucher";
}
}
i don't know if this method is right or should be used, but you can do this to get the desired result. Personally i used Insane skull's answer.
Another way to do it
[
'label' => 'Vocuher Category',
'attribute' => function( $model )
{
if( $model->voucher_category == 0 )
return "Income Voucher";
if( $model->voucher_category == 1 )
return "Expense Voucher";
else
return "General Voucher";
}
],
Upvotes: 1
Reputation: 9368
You can add condition using ternary. For Example,
[
'attribute' => 'voucher_category',
'value' => (($model->voucher_category ==0) ? "Income Voucher": (($model->voucher_category ==1)? "Exepense Voucher" : "General Voucher")),
],
Upvotes: 7