kartik-v / yii2-widget-select2

Enhanced Yii2 wrapper for the Select2 jQuery plugin (sub repo split from yii2-widgets).
http://demos.krajee.com/widget-details/select2
Other
323 stars 145 forks source link

Select2 with multiple options, loading ajax with initialValue set (in a FormGrid) #177

Closed eothein closed 7 years ago

eothein commented 8 years ago

Dear Kartik

Something is going wrong in the following use case. I want a a select2, with multiple selections, with a default value selected when the page is loaded. In your documentation you mention to use "initValueText" but something is going wrong here.

I get an exception: array_combine(): Both parameters should have an equal number of elements

I have debugged and have come to the following line of code in the Select2.php file (starting from line code 184):

    $this->initPlaceholder();
    if (!isset($this->data)) {
        if (!isset($this->value) && !isset($this->initValueText)) {
            $this->data = [];
        } else {
            $key = isset($this->value) ? $this->value : ($multiple ? [] : '');
            $val = isset($this->initValueText) ? $this->initValueText : $key;
            $this->data = $multiple ? array_combine($key, $val) : [$key => $val];
        }
    }

It seems that the number of elements of $key and $val are not the same. The array for $val ( which equals $this->initValueText)- is set, but the $val (corresponding with $this->value) is not set.

I have tried adding 'value => [theValue] in the array for the Select2 but it seems it gets skipped somehow.

The code I have (partially) for the select2 in the formgrid is as follows: ` 'attributes' => [

                        'klanten' => [ 'type' => Form::INPUT_WIDGET,
                            'widgetClass' => '\kartik\select2\Select2',
                            'model' => $model,
                            'attribute' => 'klanten',

                            'options' => [

                                'value'=> [$klant->klant_naam], 
                                'initValueText' => [$klant->klant_naam], 
                                'pluginOptions' => [
                                    'multiple' => 'multiple',
                                    'ajax' => [
                                        'url' => 'http:///horizonmanager.test/klant/getklanten',
                                        'dataType' => 'json',
                                        'data' => new JsExpression('function(params) { return { q: params.term, page: params.page}; }'),

                                    ],
                                    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                    'templateResult' => new JsExpression('function(klant) { return klant.text; }'),
                                    'templateSelection' => new JsExpression('function (klant) { return klant.text; }'),

                                ],
                                //'data' => $klanten,
                                'options' => [ 'placeholder' => 'Selecteer ...',
                                    'multiple' => 'multiple'],
                            ]
                        ],
                    ],`
serjgamover commented 7 years ago

+1

kartik-v commented 7 years ago

For model attributes you do not set the value property of the widget, instead you must set

$model->attribute = <your-attribute-value-to-assign>;