How to change one attribute display in yii2 DetailView widget? - yii2

I am using yii\widgets\DetailView to display some single object informations like this:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'username',
'email:email',
[
'attribute'=>'status',
'value' => $model->status,
],
'created_at:date',
'updated_at:date',
],
]) ?>
I want to display status in different color. So basically I would like to either add class of .red to the wrapping <td> tag, or even better to wrap the status with <span class="red">{value}</span>
GridView has contentOptions which is really easy to use. But with DetailView I have tried with options and template, but I couldn't make it work. Maybe I did something wrong, or I need to use some other attribute.
So this is not working for me:
[
'attribute'=>'status',
'value' => $model->status,
'template' => '<tr><th>{label}</th><td><span class="red">{value}</span></td></tr>'
],
Can someone explain how to achieve this ?
Thanks in advance.

You should simply try :
[
'attribute'=>'status',
'value' => '<span class="red">'.$model->status.'</span>',
'format' => 'raw',
],
Read more.

Related

ExportMenu Kartik doesn't work in yii2 GridView widget

The export dropdown and toggle dropdown do not appear on click. I can't see any errors in console of developer tool.
I have used exactly same code in another project and that seems to work, the only difference is in the column set.
<?php
$exportColumns=[
'name',
'email',
'username',
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' =>$exportColumns,
'dropdownOptions' => [
'label' => 'Export All',
'class' => 'btn btn-secondary'
]
]);
?>
I had a similar issue with my own Yii2 project. In my project the dropdown menu worked, but the buttons that appeared were unresponsive and the entire page froze without any console error.
My own mistake was that I required two bootstrap versions in my composer.json (I left in yiisoft/yii2-bootstrap in my case, removed the other).
Double Bootstrap

Yii2 Gridview CheckboxColumn, background color of the check box cell

Is there any way to set a background color only for the checkbox column?
Thanks.
for styling column properties you can use options
......
'columns' => [
// ...
[
'class' => 'yii\grid\CheckboxColumn',
/ you may configure additional properties here
'options' => ['class'=> 'your_check_box_class' , style =>'background-color: #ff0000;'],
],
],
for checkbox content you can use contentOptions
http://www.yiiframework.com/doc-2.0/yii-grid-checkboxcolumn.html

yii2 AutoComplete position

It was working perfectly, I have changed nothing, and I have realized that my AutoComplete widget is showing up not there where it was before (it was in the gridView, right underneath the filter textinput where it is expected). Now it shows up in the left upper corner of the page, with the first 2-3 options covered by the navbar. Is there a change in AutoComplete, or in Yii2 now? I have found an option appendTo that has maybe something to do with the problem, and I have done experiments with it but no luck, either it doesn't show up, or still on the wrong place. I have found nothing else relevant.
Here is my code in the gridview:
[
'attribute' => 'name',
'contentOptions' => ['nowrap' => 'nowrap'],
'filter' => AutoComplete::widget([
'model' => $searchModel,
'attribute' => 'name',
'clientOptions' => [
'source' => Pl::find()->allAutoCompleteName(),
'autoFill' => true,
],
'options' => ['class' => 'form-control']
]),
],
The id of the search field is plsearch-name
The js:
jQuery('#plsearch-name').autocomplete({"source":[{"value":"PE 150","label":"PE 150"}...],"autoFill":true});
If I'm setting the appendTo in clientOptions to '#plsearch-name', then it doesn't show up at all, however it is correct, isn't it? I don't see any styling in it.
What also quite interesting is:
<input
type="text"
id="plsearch-name"
class="form-control ui-autocomplete-input"
name="PlSearch[name]"
autocomplete="off"/>
What is this autocomplete="off" doing there?
Can you please point me to the right direction? Thanks a lot!
This Problem is fixed here: https://github.com/yiisoft/yii2-jui/commit/36468550d72bce9d963149abe85b13ea2f3a8c18
The JQuery UI Version should be updated to 1.12
"require": {
"yiisoft/yii2": "~2.0.4",
- "bower-asset/jquery-ui": "1.11.*#stable"
+ "bower-asset/jquery-ui": "~1.12.1"
},

yii2 pjax inside other pjax

i have form with two pjax. (one inside two) when clicked link in second PJAX send query to server but in field "X-PJAX-Container" write ID first container.
Pjax::begin([
'id' => 'register-form'
]);
...
Pjax::begin([
'id' => 'que'
]);
echo yii\helpers\Html::a('update', ['/portal/que/gr-usl/');
Pjax::end();
...
Pjax::end();
location after click update : /portal/que/gr-usl?_pjax=%23register-form
request header
X-PJAX:true
X-PJAX-Container:#register-form
X-Requested-With:XMLHttpRequest
if set data-pjax for link #que request header not changed.
for all link in inside pjax disabled data-pjax an href change to onclick with function
$.pjax({url: $(el).attr('link'), container: '#pjax-grusl'});
links has code
echo yii\helpers\Html::a('update', null,[
'data-pjax' => 'false',
'link' => \yii\helpers\Url::to(['/portal/que/gr-usl/',
'ids' => $ids,
'filter' => $filter,
'gr' => $gr
]),
'onclick' => 'selGr(this)'
]);
but its should work from box :( i don't understand why click by link don't find first pjax container from dom tree.

yii2 inserting a value, when 'other' option is choosen, in dropdown list

I have a dropdown list in my form, and I would like to enable the user to an option of 'other', which would enable him to insert a value in to a text box, instead of a value from the dropdownlist.
Is that possible?
<?= $form->field($model, 'institution')->dropDownList(ArrayHelper::map(Institution::find()->all(), 'iId', 'name'), ['class'=>'form-control inline-block']); ?>
The same thing I would like to do with a radioList.
<?= $form->field($model, 'selfDescription')->radioList(array('good'=>'good','very good'=>'very good','best'=>'best')); ?>
thanks.
First. Add "other" option to dropDownList. And handle it in your frontend with JS
$form
->field($model, 'institution')
->dropDownList(
ArrayHelper::map(Institution::find()->all(),
'iId',
'name'
) + ['other' => 'Other'],
[
'class'=>'form-control inline-block',
// next code disables text field when any but "other" option is selected
// works for jQuery 1.6+
'onchange' => new JsExpression('
$("#textFieldSelector").prop("disabled", $(this).find("option:selected").val() != "other");'),
]
);
Then you need to set properly your validators. Just add
['textField', 'required', 'when' => function($model) {
return $model->institution === 'other';
}]