Monday, March 12, 2012

Yii Multi Select

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











Installation download http://www.yiiframework.com/extension/multiselect/files/multiselect.zip
and extract to extensions folder
























/**
 * JMultiSelect class file.
 *
 * PHP Version 5.1
 *
 * @package  Widget
 * @author   Segoddnja
 * @license  http://www.opensource.org/licenses/mit-license.php MIT
 * @since    1.0
 */

Yii::import('zii.widgets.jui.CJuiInputWidget');

/**
 * JMultiSelect displays a multiselect.
 *
 * JMultiSelect encapsulates the {@link http://abeautifulsite.net/blog/2008/04/jquery-multiselect/
 * jQuery MultiSelect} plugin.
 *
 * To use this widget, you may insert the following code in a view:
 *

 * $this->widget('zii.widgets.jui.CJuiDatePicker', array(
 *     'name'=>'publishDate',
 *     // additional javascript options for the date picker plugin
 *     'options'=>array(
 *         'showAnim'=>'fold',
 *     ),
 *     'htmlOptions'=>array(
 *         'style'=>'height:20px;'
 *     ),
 * ));
 * $this->widget('ext.multiselect.JMultiSelect',array(
 *      'model'=>$model,
 *      'attribute'=>'attribute',
 *      'data'=>$data,
 *      // additional javascript options for the MultiSelect plugin
 *      'options'=>array()
 * ));
 *

 *
 * By configuring the {@link options} property, you may specify the options
 * that need to be passed to the MultiSelect plugin. Please refer to
 * the {@link http://abeautifulsite.net/blog/2008/04/jquery-multiselect/#configuring MultiSelect plugin} documentation
 * for possible options (name-value pairs).
 *
 * @author segoddnja
 */

class JMultiSelect extends CJuiInputWidget
{
   
    /**
     * @var array data for generating the list options (value=>display)
     */
    public $data  = array();
   
    /**
     * @var array selected data for list
     */
    public $selected  = array();
   
    /**
     * @var array additional options for multiselect script
     */
    public $options = array();

    /**
     * Run this widget.
     * This method registers necessary javascript and renders the needed HTML code.
     */
    function run()
{
        list($name, $id) = $this->resolveNameID();
       
if (isset($this->htmlOptions['id']))
{
            $id = $this->htmlOptions['id'];
} else {
            $this->htmlOptions['id'] = $id;
}

        if (isset($this->htmlOptions['name']))
{
            $name = $this->htmlOptions['name'];
} else {
            $this->htmlOptions['name'] = $name;
}
       
if ($this->hasModel())
{
            //echo CHtml::activeDropDownList($this->model, 'citysFilter', $this->data, $this->htmlOptions);
            echo CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions);
} else {
            echo CHtml::dropDownList($name, $this->selected, $this->data, $this->htmlOptions);
}
        $options = CJavaScript::encode($this->options);
        $js = "jQuery('#{$id}').multiselect($options);";
        Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id, $js);
    }

    /**
     * Initializes the widget.
     */
    public function init() {
        parent::init();
        $this->registerScripts();
        $this->htmlOptions['multiple'] = 'multiple';
        $this->options = array_merge(array(
            'header' => false,
            'selectedList' => 4,
            'noneSelectedText' => 'Select an option',
            'selectedText' => '# selected',
        ), $this->options);
    }

    /**
     * Registers the JS and CSS Files
     */
    protected function registerScripts() {
        parent::registerCoreScripts();
        $assets = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('ext.multiselect') . '/assets');
        $cs = Yii::app()->getClientScript();
        $cs->registerCoreScript('jquery.ui');
        $cs->registerScriptFile($assets . '/jquery.multiselect.min.js');
        $cs->registerCssFile($assets . '/jquery.multiselect.css');
    }

}

?>

No comments:

Post a Comment

About

Blogger templates