2amigos / yii2-selectize-widget

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

pre-selected/default option #46

Open Dunc80 opened 6 years ago

Dunc80 commented 6 years ago

Is it possible to have a value pre-selected?

When I open a form with this widget on an "update" page I want it to already have the existing data showing, rather than being blank as it would be on the "create" page.

Please could someone advise me on how to achieve this?

I am passing the options to the widget as array $x

'clientOptions' => [ 'valueField' => 'id', 'labelField' => 'name', 'allowEmptyOption' => true, 'preload' => true, 'options'=> $x, ],

patschwork commented 5 years ago

Hi,

just had the same question:

Example:

use app\models\DbTable;

echo SelectizeTextInput::widget([
    'name' => 'tagsGlobal',
    'value' => '162,163',              // <----------------- Thats the IDs
    'loadUrl' => ['tag/listglobal2'],
    'clientOptions' => [
        'plugins' => ['remove_button'],
        'valueField' => 'id',
        'labelField' => 'name',
        'searchField' => ['name'],
        'create' => false,
        'preload' => true,
        'options' =>                                       // use options
            DbTable::find()
                ->select(['id','name'])
                ->where(['like', 'name', 'Dim'])
                ->all(),
        ],
    ]);