2amigos / yii2-selectize-widget

Selectize From Brian Reavis Yii2 Widget
Other
73 stars 41 forks source link

activeTextInput update scenario #11

Closed hesna closed 9 years ago

hesna commented 9 years ago

I'm writing a news module and each news can have multiple categories. using your widget, a user can select multiple categories from the list. code is like this:

                <?= $form->field($model, 'categories')->widget(
                        Selectize::className(),
                        [
                            'options' => ['class' => 'form-control'],
                            'clientOptions' => [
                                'create' => false,
                                'options' =>
                                    Utility::makeReadyForSelectize(
                                        Category::find()->all(),
                                        'title',
                                        'title'
                                    ),
                                'valueField' => 'title',
                                'labelField' => 'title',
                                'searchField' => ['title'],
                                'plugins' => ['remove_button'],
                            ]
                        ]
                    )->label('')
                ?>

It's working fine in create scenario, but in update I expected it to show values that user has already selected. it only shows the first one. from reading original selectize.js documents I noticed that I should set the items option of plugin. I did it by adding the following line:

                            'clientOptions' => [
                                'create' => false,
                                'options' =>
                                    Utility::makeReadyForSelectize(
                                        Category::find()->all(),
                                        'title',
                                        'title'
                                    ),
                                'items' => $model->getCategories(), // returns an array
                                'valueField' => 'title',
                                'labelField' => 'title',
                                'searchField' => ['title'],
                                'plugins' => ['remove_button'],
                            ]

It's okay now, but isn't it something the widget should do by itself? because that's what an active form is about. or am I missing something? Thanks for great work btw.

creocoder commented 9 years ago

Please try new version, it use separated widgets for text inputs and dropdown lists. It also support selected items automatically by using SelectizeDropDownList::$items.