Skip to content
This repository has been archived by the owner on Feb 17, 2018. It is now read-only.

activeDropdownList() problem? #6

Open
DiogoRNeves opened this issue Aug 11, 2014 · 3 comments
Open

activeDropdownList() problem? #6

DiogoRNeves opened this issue Aug 11, 2014 · 3 comments

Comments

@DiogoRNeves
Copy link

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!

@tonybolzan
Copy link
Owner

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.

@DiogoRNeves
Copy link
Author

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!

@DiogoRNeves
Copy link
Author

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,
            ),
        ));

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants