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?
Page 1 of 1
Multi Select Lists - Pros And Cons
#2
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...
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)...
And that's it.
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.
#3
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.
#4
Posted 16 February 2013 - 08:37 AM
AustinGeek, 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?
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.
#5
Posted 17 February 2013 - 02:49 PM
Thank you all ! Will look into those extensions and play around a bit.
Share this topic:
Page 1 of 1

Help













