[Solved] Dropdown redirect broken

This worked fine in Yii 1.1.4, but doesn’t now:


<?php


/**

 * @brief This widget is a dropDown which can redirect to  an url base on selected value via javascript

 */

class DropDownRedirect extends CWidget {

	public $name; // name attribute of the dropdownlist

	

	public $select; // selected value

	

	public $data; // data of the dropdownlist

	

	public $htmlOptions = array(); // options of the dropdownlist

	

	public $url; // url with the string $replacement somewhere, wich will be replaced by the current value

	

	public $replacement = '__value__'; // will be replaced by the value

	

	protected function registerScript() {

		$script = '$("#'.$this->id.'").change(function(){'

		.'$(location).attr("href", "'.$this->url.'".replace("'.$this->replacement.'", $(this).val()));'

		.'});';

		Yii::app()->clientScript->registerScript(__CLASS__.$this->id, $script);

	}

	

	public function init() {

		if (! isset($this->name))

			$this->name= $this->id;

		$this->registerScript();

	}

	

	public function run() {

		if (!isset($this->htmlOptions['id'])) $this->htmlOptions['id'] = $this->id;

		echo CHtml::dropDownList($this->name, $this->select, $this->data, $this->htmlOptions);

	}

}



Can any of you spot the error ?

Hi jacmoe,

If you display your widget when page loads do the CClientScript::POS_READY… otherwise it will render without errors and the event binding wont occur.

Also, I will change the $(location).attr( — is pretty cool but why not the good old way? document.location.href=

It doesn’t need to replace any attribute nor search for… i think is faster. Don’t you?

Also, why not to simply to attach directly the event?




 $this->htmlOptions['onChange']=$this->url.'".replace("'.$this->replacement.'", $(this).val()));

 echo CHtml::dropDownList($this->name, $this->select, $this->data, $this->htmlOptions);




Yeah… and even more… why using a widget if you can simply do it with CHtml object?

echo CHtml::dropDownList(‘name’,‘selectwhat’,array(‘url-language’=>‘language’), array(‘onchange’=>‘javascript: document.location.href=$(this).val();’));

(‘not tested’)

Thanks, guys! for pointing me in the right direction. :D

I solved it by doing this:


/**

 * @brief This widget is a dropDown which can redirect to  an url base on selected value via javascript

 */

class DropDownRedirect extends CWidget {

	public $name; // name attribute of the dropdownlist

	

	public $select; // selected value

	

	public $data; // data of the dropdownlist

	

	public $htmlOptions = array(); // options of the dropdownlist

	

	public $url; // url with the string $replacement somewhere, wich will be replaced by the current value

	

	public $replacement = '__value__'; // will be replaced by the value

	

	public function init() {

		if (! isset($this->name))

			$this->name= $this->id;

	}

	

	public function run() {

		if (!isset($this->htmlOptions['id'])) $this->htmlOptions['id'] = $this->id;

            	$script = 'window.location = "'.$this->url.'".replace("'.$this->replacement.'", $(this).val());';

            	if (!isset($this->htmlOptions['onChange'])) $this->htmlOptions['onChange'] = $script;

            	echo CHtml::dropDownList($this->name, $this->select, $this->data, $this->htmlOptions);

	}

}



I guess the jQuery upgrade broke it. :)

This is my initial, stand-alone version:


$script = 'window.location = "'.$this->createUrl($this->route, array_merge($_GET, array('identifier' => '__value__'))).'".replace("__value__", $(this).val());';

echo CHtml::dropDownList('projectSwitcherFilter',

isset($_GET['identifier'])?$_GET['identifier']:'empty',

Yii::app()->controller->getProjects(),

array('onChange'=> $script, 'class' => 'floatright'));



I will probably go with the stand-alone, since it’s only used in one place.

I need it to replace just one parameter in my URL, namely: ‘identifier’ which is used to determine which project the app is ‘in’.

The rest of the URL should be kept as-is.

So that one can switch to viewing issues for one project to view issues for another - ie the view doesn’t change, the context does.

Works great now - thanks. :lol:

Congrats man :)

hi, a would like to change appearence of the dropDownList element.

I use DropDownRedirect and ui-selectmenu.js . It work BUT

I would like to add a flag icon near the language.

And to do it i must add a class to


<option selected="selected" value="en">english</option>

like


<option selected="selected" value="en" class="en">english</option>

with this code :

DropDownRedirect :


<?php


/**

 * @brief This widget is a dropDown which can redirect to  an url base on selected value via javascript

 */

/**

 * @brief This widget is a dropDown which can redirect to  an url base on selected value via javascript

 */

class DropDownRedirect extends CWidget {

        public $name; // name attribute of the dropdownlist


        public $select; // selected value


        public $data; // data of the dropdownlist


        public $htmlOptions = array(

            'id'=>'languageselect',

            'class'=>'customicons'

        ); // options of the dropdownlist


        public $url; // url with the string $replacement somewhere, wich will be replaced by the current value


        public $replacement = '__value__'; // will be replaced by the value


        public function init() {

                if (! isset($this->name))

                        $this->name= $this->id;

        }


        public function run() {

                if (!isset($this->htmlOptions['id']))

                        $this->htmlOptions['id'] = $this->id;

                $script = 'window.location = "'.$this->url.'".replace("'.$this->replacement.'", $(this).val());';

                if (!isset($this->htmlOptions['onChange'])) $this->htmlOptions['onChange'] = $script;

                echo CHtml::dropDownList($this->name, $this->select, $this->data, $this->htmlOptions);

        }

}

myuser :


public function getAvalaibleLanguages() {

        return array('en' => Yii::t('site', 'english'), 'fr' => Yii::t('site', 'français'));

    }

Myview :


        <?php $this->widget('DropDownRedirect', array(

    'data' => Yii::app()->user->avalaibleLanguages, // data od my dropdownlist

    'url' => $this->createUrl($this->route, array_merge($_GET, array('lang' => '__value__'))), // the url (__value__ will be replaced by the selected value)

    'select' => Yii::app()->user->language, //the preselected value

));?>

Thanks for your help :D