Sunday, February 12, 2012

Using emultiselect in yii framework

In this sections I wanna explain the extension in yiiframework, If you have to enable multiple selections in your website, you can use emultiselect extensions in yiiframework. This extension is useful for you for enabling the multiple options of dropdownlist interface.

Lets take a look a sample of application that using this extension.

http://www.yiiframework.com/extension/emultiselect/

That's the look of emultiselect applications. And now we have to download this extensions in the url above. And then extract this to your site in protected/extensions folder. 

To use this great multiselect options just simple use this code


<?php $this->widget('application.extensions.emultiselect.EMultiSelect',
            array('sortable'=>true/true, 'searchable'=>true/true));?>

And then add class to the existing dropdownlist in your form view. for example :

echo $form->dropDownList(new TProjectHasTUnit(),'work_id_FK',$list,
       array('multiple'=>'multiple',
       'key'=>'unit_id', 'class'=>'multiselect')
     );


Note :


if you are using this for update action, pay attention for keeping the selected options to be selected. And to fulfill this purpose, you have to set the dropdownlist from your database.

To create this use have to take the value from your database first and for the example I use this :


<?php $this->widget('application.extensions.emultiselect.EMultiSelect',
            array('sortable'=>true/true, 'searchable'=>true/true));
    $m=TUnit::model()->findAll();
    $list=CHtml::listData($m,'unit_id','name');
    if($model->isNewRecord){
     echo $form->dropDownList(new TProjectHasTUnit(),'work_id_FK',$list,
       array('multiple'=>'multiple',
       'key'=>'unit_id', 'class'=>'multiselect')
     );
    }else{
     
     $selected_scope=TProjectHasTUnit::model()->findAllByAttributes(array('project_id_FK'=>$model->project_id));
     $selected_key_scope=array();
     foreach ($selected_scope as $val){
      $selected_key_scope[]=$val->work_id_FK;
     }
     echo $form->dropDownList(new TProjectHasTUnit(),'work_id_FK',$list,
       array('multiple'=>'multiple',
             'key'=>'unit_id', 'class'=>'multiselect','options'=>$this->createOptions($selected_key_scope))
     );
    }
  ?>

Don't forget to create the funtions $this->createOptions() in the protected/components/Controller.php for later use.



public function createOptions($arraySelectedValue){
  $selected_attr=array('selected'=>'selected');
  $all=array();
  foreach($arraySelectedValue as $val){
   $all["$val"]=$selected_attr;
  }
  return $all;
 }
That's simple and just follow that you can see the result.. Keep use this Yii is rock..

3 comments:

  1. Thank you!, Thats exactly what I was looking for. Nice Blog!

    ReplyDelete
  2. please help me..im always getting n error
    'mb_strlen() expects parameter 1 to be string, array given'

    and u dont know what to do..im sorry im new to yii..i hope you could help find the answer..thank you..

    ReplyDelete
  3. thanks for share ;) how can I keep the choices (emultiselect) after a validated false?

    ReplyDelete