How i can add space in Yii2 kartik gridview? - yii2

I'm using Yii2 kartik gridview for displaying orders, i need to add CURRENCY field like
AED 10 but now its showing without space like AED10
Here is my code :
[
'class' => 'kartik\grid\DataColumn',
'attribute' => 'sub_total',
'hAlign' => 'right',
'vAlign' => 'middle',
'format' => ['AED', $currency],
'width' => '12%',
'pageSummary' => true
]
I need space between currency code and price

Related

Why the view don't read the <br> element?

I have this code for the column grid Descrizione in my view file:
[
'attribute' => 'Descrizione',
'format' => 'html',
'label' => 'Descrizione',
],
The problem is that for the field Descrizione don't read the <br> element. For example, in the database the field is set like Today is<br>a better day, meanwhile in the view show the field without <br> (Today is a better day). I want that the field in the view respect the format when there is a <br>. Is already set with 'format' => 'html' but this don't works.
Any suggestion?
You can get the same with ntext
[
'attribute' => 'Descrizione',
'format' => 'ntext',
'label' => 'Descrizione',
],
If you need some extra markup to be displayed, you can use this:
'format' => 'raw',

data-* attributes do not work with Html::a() in Yii 2

I have the following:
Html::a('Link', ['some/route'], [
'class' => 'btn btn-lg btn-primary', // WORKS
'style' => 'padding: 100px;', // WORKS
'data-id' => 123, // DOES NOT WORK
'data' => [
'id' => 123, // DOES NOT WORK
],
]);
As per docs, both of the specified data-* attributes in Html::a helper should render their respective attributes in the HTML output, but they do not, and I do not understand why.
Yii 2 documentation on renderTagAttributes also states the following:
Renders the HTML tag attributes.
Attributes whose values are of boolean type will be treated as boolean
attributes.
Attributes whose values are null will not be rendered.
The values of attributes will be HTML-encoded using encode().
The "data" attribute is specially handled when it is receiving an
array value. In this case, the array will be "expanded" and a list
data attributes will be rendered. For example, if 'data' => ['id' =>
1, 'name' => 'yii'], then this will be rendered: data-id="1"
data-name="yii". Additionally 'data' => ['params' => ['id' => 1,
'name' => 'yii'], 'status' => 'ok'] will be rendered as:
data-params='{"id":1,"name":"yii"}' data-status="ok".
EDIT: I am trying to do this inside GridView column.
Okay, since I have used Html::a inside a GridView column, you will have to change the output format of that column. html will not work for data attributes, so you will need to switch to raw:
[
'label' => 'Actions',
'format' => 'raw',
'value' => function($model) {
return Html::a('Link', ['some/route'], [
'class' => 'btn btn-lg btn-primary', // WORKS
'style' => 'padding: 100px;', // WORKS
'data-id' => 123, // WORKS
'data' => [
'id-second' => 123, // WORKS
],
]);
},
]

Yii2 select2 widget selected value does not appear when I click edit

This is my form. I have save value when I create but then when I update the value wont appear.
$form->field($model, 'unit_id')->widget(Select2::classname(), [
'data' => $getunit,
'language' => 'en',
'value'=>$model->unit_id, //cant work
'initValueText'=>$model->unit_id //cant work
'options' => ['placeholder' => 'Select','multiple'=>true,'value'=>$model->unit_id' //cant work ],
'pluginOptions' => [
'allowClear' => true,
'tags'=>true,
'maximumInputLength'=>10,
],
])->label(false)
Anyone know why? I search through online but I still cant solve it

How to display values according to it's name in DetailView yii2?

I'm trying to display category values according to it's name, not id. I'm trying to do like so:
[
'attribute' => 'category_id',
'value' => 'category.name',
],
But then attribute doesn't get displayed. It displays: name instead of Category. Category name is displayed correctly.
Also tried 'category.name' ,but it's displaying same values, and 'category_id' is displaying label correctly, but the name - according to id's.
How should I solve that?
Are you define relation for category, like this?
public function getCategory()
{
return $this->hasOne(Category::className(),['id'=>'category_id']);
}
If you want to display in GridView, you can do it like you
[
'attribute' => 'category_id',
'value' => 'category.name',
],
If you want to display in DetailView, you can do it like this
[
'attribute' => 'category_id',
'value' => $model->category ? $model->category->name : '',
],
seems you need a label.
the simplest way is add the label direcly in the detail view item
[
'label' => 'Category',
'attribute' => 'category_id',
'value' => ....,
],

Yii2 kartik detailview hide label

If I'm doing 'label' => false, it is still showing the empty cell of the label. Is it possible completely not to render label cell somehow? Many thanks!
UPDATE: 'labelColOptions' => ['hidden' => true] works!
You can use visible property :
For example,
[
'attribute' => 'name',
'value' => whatever,
'visible' => (!empty($model->name)),
]