tonybolzan / yii-select2

Wrapper Widget to use jQuery Select2 in Yii application.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

activeDropdownList() problem?

DiogoRNeves opened this issue · comments

Hi there! First of all I'd like to thank you for the extension, really well done.

I'm having some trouble with the active Dropdown List and I think you can probably help me out.

So I have a CActiveForm and a $user model. When I'm on the create action for that model, the 'playerLevel' attribute in my view as a drop down list. The view code is as follows:

View PHP

``` php
        labelEx($user, 'playerLevel'); ?>
         'really really high', '2' => 'really really low'), array(
            'placeholder' => 'This is a placeholder',
            'select2Options' => array(
                'allowClear' => true,
            ),
        ));
        error($user, 'playerLevel'); ?>
```

This generates:

HTML

``` html

 really really high
 really really low

```

jQuery

``` javascript
$('#User_playerLevel').select2({'placeholder':'This is a placeholder','allowClear':true});
```

My problem is that, since it is a create action, the model's attributes are empty and so, I'd expect "This is a placeholder" to show up straight away, but it doesn't. Instead, the first value on the list is automatically selected. Also, I can't clear the value.

Edit: If I change it to an activeMultiSelect() it behaves perfectly.

Any hints?

Thanks in advance for your time!

I understand ...
I checked the operation of the placeholder Select2

And I noticed the need for a <option> empty.
See http://jsfiddle.net/9ednLacq/

I will adjust the code.
Thank you.

Wow, that's perfect (that didn't even occur to me, well spotted). I'll clone it as soon as you commit it.

Thanks for the quick answer and keep up the good work!

Tony, calling it this way fixes it:

PHP
echo Select2::activeDropDownList($user, 'playerLevel', array('1' => 'really really high', '2' => 'really really low'), array(
            'placeholder' => 'This is a placeholder',
            'empty' => '',
            'select2Options' => array(
                'allowClear' => true,
            ),
        ));