Closed BARTlab closed 7 years ago
BARTlab, add 'format' and 'value' to the column options but not to the editableOptions.
[
'class' => \dosamigos\grid\EditableColumn::className(),
'type' => 'select',
'url' => ['update'],
'attribute' => 'category_id',
'format' => 'raw',
'value' => function($data) { return $data->category_id},
'editableOptions' => [
...
]
],
if you want to display category name instead of category_id just add new methods to model class,
public function getCategories()
{
return [
1 => 'Small',
2 => 'Medium',
3 => 'Big',
4 => 'Large',
];
}
public function getCategoryName()
{
return $this->categories[$this->category_id];
}
and change 'value' to function -
[
'class' => \dosamigos\grid\EditableColumn::className(),
'type' => 'select',
'url' => ['update'],
'attribute' => 'category_id',
'format' => 'raw',
'value' => function($model) {
return $model->getCategoryName();
},
'editableOptions' => [
...
]
],
@ybreeze Thank you... excellent answer
You have written a very bad example. In the first case - we do not know the variable $model, in the second - when you open the editing popup window - do not select the correct option
And, format = 'raw' autoset: https://github.com/2amigos/yii2-grid-view-library/blob/master/EditableColumn.php#L70
You are right about 'format', it's 'raw' by default and can be skipped.
You are not right about $model. Please read official guide for GridView - http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html - "Grid columns" section. $model (or $data in guide) is know and will be passed by GridView to 'value' function for each grid row from $dataProvider.
you can specify type of $model like -
'value' => function(Company $model) {
return $model->getCategoryName();
},
for IDE autocomplete etc.
I have looked to the source code, and once again I can tell you - your way is not correct, read the bootstrap-editable.js code
"your way is not correct" - It is works perfect in my project.
"For select type should be defined (as it is ID of shown text)." -
'editableOptions' => [
...
'source' => [
[
'value' => 1,
'text' => 'Active',
],
[
'value' => 0,
'text' => 'Inactive',
]
],
'placement' => 'bottom',
...
]
'editableOptions' => [
...
'source' => [
[
'value' => 1,
'text' => 'Active',
],
[
'value' => 0,
'text' => 'Inactive',
]
],
...
]
htmlspecialchars() expects parameter 1 to be string, array given in /var/www/common/lib/vendor/yiisoft/yii2/helpers/BaseHtml.php at line 96
[
'class' => \dosamigos\grid\EditableColumn::className(),
'type' => 'select',
'url' => ['update'],
'attribute' => 'group_id',
'value' => function ($model) {
return 'Active';
},
'editableOptions' => [
'source' => json_encode(
[
[
'value' => 1,
'text' => 'Active',
],
[
'value' => 2,
'text' => 'Inactive',
]
]
),
]
],
when you open the editing popup window - do not select the correct option
@BARTlab Will review it personally whenever I have time
I have the same issue. The field does not show the current value as default. If somebody has a solution for this it would be highly appreciated to post an example.
@tonydspaniard Any updates regarding this issue? Does select2 not show the correct option because it uses displayed column value (instead of column ID)?
This code show emty value
This occurs because js plug-in for some reason does not get the right value.
Quick fix:
But, may want add to the EditableColumn widget code?