Yii Framework Forum: How to implement select multiple listbox - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

How to implement select multiple listbox Rate Topic: -----

#1 User is offline   rickert 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 21-October 09
  • Location:Holland

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?
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

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):
 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

1

#3 User is offline   rickert 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 21-October 09
  • Location:Holland

Posted 22 October 2009 - 09:30 AM

View PostMike, 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):
 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 :) thanks again)
1

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 22 October 2009 - 09:32 AM

View Postrickert, on 22 October 2009 - 09:30 AM, said:

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 :) thanks again)


Heh, sorry, forgot that.
0

#5 User is offline   rickert 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 21-October 09
  • Location:Holland

Posted 22 October 2009 - 09:38 AM

View PostMike, on 22 October 2009 - 09:32 AM, said:

Heh, sorry, forgot that.

No problem at all. I'm glad you were at help.
0

#6 User is offline   aphelio 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 04-October 10

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.
-1

#7 User is offline   Esspweb 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 19-January 11

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.
0

#8 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 17 October 2011 - 08:55 PM

thanks for this useful post!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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