2amigos / yii2-grid-view-library

Highly enhanced GridView widget and grid components for Yii2
https://2amigos.us
Other
57 stars 16 forks source link

editable column - select #16

Closed BARTlab closed 7 years ago

BARTlab commented 9 years ago

This code show emty value

[
    'class' => \dosamigos\grid\EditableColumn::className(),
    'type' => 'select',
    'url' => ['update'],
    'attribute' => 'category_id',
    'editableOptions'=>[
        'autotext'=>'always',
        'source' => json_encode($section)
    ]
],

This occurs because js plug-in for some reason does not get the right value.

Quick fix:

'editableOptions'=>function($model, $key, $index) use ($section){
    return [
        'value'=>$model->category_id,
        'autotext'=>'always',
        'source' => json_encode($section)
    ];
}

But, may want add to the EditableColumn widget code?

    protected function renderDataCellContent($model, $key, $index)
    {

        ...
        $this->options['data-value'] = $value;

      ...
    }
ybreeze commented 9 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' => [
        ...
    ]
],
tonydspaniard commented 9 years ago

@ybreeze Thank you... excellent answer

BARTlab commented 9 years ago

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

BARTlab commented 9 years ago

And, format = 'raw' autoset: https://github.com/2amigos/yii2-grid-view-library/blob/master/EditableColumn.php#L70

BARTlab commented 9 years ago

and how about this? https://github.com/2amigos/yii2-editable-widget/blob/master/assets/editable/js/bootstrap-editable.js#L490

ybreeze commented 9 years ago

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.

BARTlab commented 9 years ago

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

ybreeze commented 9 years ago

"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',
        ...
    ]
BARTlab commented 9 years ago
 '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

BARTlab commented 9 years ago
[
                '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 s_42

tonydspaniard commented 9 years ago

@BARTlab Will review it personally whenever I have time

gb5256 commented 9 years ago

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.

re1naldo commented 9 years ago

@tonydspaniard Any updates regarding this issue? Does select2 not show the correct option because it uses displayed column value (instead of column ID)?

tonydspaniard commented 7 years ago

new updates: http://yii2-grid-view-library.readthedocs.io/en/latest/