Page 1 of 1
How to implement select multiple listbox
#1
Posted 22 October 2009 - 06:55 AM
Does activeListBox support implementation of a select MULTIPLE listbox?
Sure I can add an htmlOption for 'multiple' to go with activeListBox. This will give me a select multiple listbox in the form. However, the activeRecord class does not seem to support saving the array of selected values that returns with $_POST.
Maybe not suprising because it's not clear how to store this array in MySQL (with a SET column maybe or as different rows in a HASMANY table)
Any hint?
Sure I can add an htmlOption for 'multiple' to go with activeListBox. This will give me a select multiple listbox in the form. However, the activeRecord class does not seem to support saving the array of selected values that returns with $_POST.
Maybe not suprising because it's not clear how to store this array in MySQL (with a SET column maybe or as different rows in a HASMANY table)
Any hint?
#2
Posted 22 October 2009 - 07:10 AM
Simple approach:
1. Save selected values in a varchar column as comma separated string ('15,23,45')
2. Add public virtual attribute to your AR (to be used as attribute for your dropdown):
3. afterFind(): explode your db column and put result into your virtual
4. beforeSave(): implode your selection
Oh, and don't forget to make "selection" a safe attribute, while "dbcolumn" doesn't have to be anymore.
1. Save selected values in a varchar column as comma separated string ('15,23,45')
2. Add public virtual attribute to your AR (to be used as attribute for your dropdown):
public $selection;
3. afterFind(): explode your db column and put result into your virtual
public function afterFind() {
$this->selection=explode(',',$this->dbcolumn);
}4. beforeSave(): implode your selection
public function beforeSave() {
$this->dbcolumn=implode(',',$this->selection);
}Oh, and don't forget to make "selection" a safe attribute, while "dbcolumn" doesn't have to be anymore.
This post has been edited by Mike: 22 October 2009 - 07:12 AM
#3
Posted 22 October 2009 - 09:30 AM
Mike, on 22 October 2009 - 07:10 AM, said:
Simple approach:
1. Save selected values in a varchar column as comma separated string ('15,23,45')
2. Add public virtual attribute to your AR (to be used as attribute for your dropdown):
3. afterFind(): explode your db column and put result into your virtual
4. beforeSave(): implode your selection
Oh, and don't forget to make "selection" a safe attribute, while "dbcolumn" doesn't have to be anymore.
1. Save selected values in a varchar column as comma separated string ('15,23,45')
2. Add public virtual attribute to your AR (to be used as attribute for your dropdown):
public $selection;
3. afterFind(): explode your db column and put result into your virtual
public function afterFind() {
$this->selection=explode(',',$this->dbcolumn);
}4. beforeSave(): implode your selection
public function beforeSave() {
$this->dbcolumn=implode(',',$this->selection);
}Oh, and don't forget to make "selection" a safe attribute, while "dbcolumn" doesn't have to be anymore.
Thanks Mike!
(although it took me some hours to find out that I had to add 'return true' to these functions to actually make it all work, but hey that's how you learn stuff
#6
Posted 17 January 2011 - 06:20 PM
Hello All,
I see that this post has not been visited in over a year. So, I am hoping to assist any future visitors that may come across this. Please view this URL:
LarryUllman.com
It is a fantastic guide for linking your model forms with related objects.
I see that this post has not been visited in over a year. So, I am hoping to assist any future visitors that may come across this. Please view this URL:
LarryUllman.com
It is a fantastic guide for linking your model forms with related objects.
#7
Posted 19 January 2011 - 02:43 AM
Nice post everyone. It seems like everyone wants to win in this post. Everyone trying to best from their sides.
Share this topic:
Page 1 of 1

Help

This topic is locked













