Yii Framework Forum: Multi Select Lists - Pros And Cons - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Multi Select Lists - Pros And Cons Rate Topic: -----

#1 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 15 February 2013 - 12:44 PM

I have a few attributes in my model that require the ability to select multiple values.
Is there a good way to do this or a bad way I should avoid?

I have seen some references to checkbox lists.

How does this affect the ability to search in CGridView and other screen widgets?
0

#2 User is offline   mikewalen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 27-August 11
  • Location:Hamilton, New Zealand

Posted 15 February 2013 - 05:08 PM

Hi AustinGeek,

I had to do this exact same thing on my web app just the other day, so I thought I'd share with you what worked for me. In my case, I wanted to be able to search my user records for users that were in the cities I selected. The key is knowing that a multiple selection drop down list submits the result as an array.

So, first, I changed the drop down list in my search form to a multiple select list...

echo $form->dropDownList($model, 'city_id', CHtml::listData(City::model()->findAll(array('select'=>'id, title'), 'id', 'title'), array(
	'prompt'=>Yii::t('main', 'All'),
	'multiple'=>true,
	'size'=>6,
));


Then, you just need to edit the search() method in your model and change the attribute you're searching on to an IN condition (rather than a 'compare' condition)...

$criteria->addInCondition('city_id', $this->city_id);


And that's it.
0

#3 User is offline   waitforit 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 382
  • Joined: 09-February 11

Posted 15 February 2013 - 09:49 PM

To make it even prettier / user friendly you can dress it up by using ASMselect or ECHmultiselect extensions.
1

#4 User is offline   clapas 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 21-February 11

Posted 16 February 2013 - 08:37 AM

View PostAustinGeek, on 15 February 2013 - 12:44 PM, said:

I have a few attributes in my model that require the ability to select multiple values.
Is there a good way to do this or a bad way I should avoid?

I have seen some references to checkbox lists.

How does this affect the ability to search in CGridView and other screen widgets?


That looks more like a HAS_MANY relation to me; if I was you, I would create a new table for each of such attributes that hold multiple values.

Besides that, solutions from mikewalen and waitforit are correct, although aiming at a different problem.
0

#5 User is offline   AustinGeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 173
  • Joined: 22-September 12

Posted 17 February 2013 - 02:49 PM

Thank you all ! Will look into those extensions and play around a bit.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users