XMultiSelects.php 4.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
<?php
/**
 * XMultiSelects class file
 *
 * This widget is used to transfer options between two select filed
 *
 * Usage
 * <pre>
 * $this->widget('ext.widgets.multiselects.XMultiSelects',array(
 *     'leftTitle'=>'Australia',
 *     'leftName'=>'Person[australia][]',
 *     'leftList'=>Person::model()->findUsersByCountry(14),
 *     'rightTitle'=>'New Zealand',
 *     'rightName'=>'Person[newzealand][]',
 *     'rightList'=>Person::model()->findUsersByCountry(158),
 *     'size'=>20,
 *     'width'=>'200px',
 * ));
 * </pre>
 */
class XMultiSelects extends CWidget
{
	/**
	 * The label for the left mutiple select
	 * option
	 * @var string
	 */
	public $leftTitle;
	/**
	 * The label for the right mutiple select
	 * option
	 * @var string
	 */
	public $rightTitle;
	/**
	 * The name for the left mutiple select
	 * require
	 * @var string
	 */
	public $leftName;
	/**
	 * The name for the right mutiple select
	 * require
	 * @var string
	 */
	public $rightName;
	/**
	 * data for generating the left list options
	 * require
	 * @var array
	 */
	public $leftList;
	/**
	 * data for generating the right list options
	 * require
	 * @var array
	 */
	public $rightList;
	/**
	 * The size for the multiple selects.
	 * option
	 * @var integer
	 */
	public $size=15;

	/**
	 * The width for the multiple selects.
	 * option
	 * @var string
	 */
	public $width;

	/**
	 * register clientside widget files
	 */
	protected function registerClientScript()
	{
		$cs=Yii::app()->getClientScript();
		$cs->registerCoreScript('jquery');
		$cs->registerScriptFile(Yii::app()->getAssetManager()->publish(dirname(__FILE__).'/jquery.multiselects.js'));
	}

	/**
	 * Initializes the widget
	 */
	public function init()
	{
		if(!isset($this->leftName))
		{
			throw new CHttpException(500,'"leftName" have to be set!');
		}
		if(!isset($this->rightName))
		{
			throw new CHttpException(500,'"rightName" have to be set!');
		}
		if(!isset($this->leftList))
		{
			throw new CHttpException(500,'"leftList" have to be set!');
		}
		if(!isset($this->rightList))
		{
			throw new CHttpException(500,'"rightList" have to be set!');
		}
	}

	/**
	 * Run the widget
	 */
	public function run()
	{
		echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
		echo "<tr>\n";
		echo "<td>\n";
		if(isset($this->leftTitle))
		{
			echo "<label for=\"leftTitle\">{$this->leftTitle}</label><br />\n";
		}
		echo "<select name=\"{$this->leftName}\" id=\"select_left\" multiple=\"multiple\" size=\"{$this->size}\" style=\"width:{$this->width}\">\n";
		foreach($this->leftList as $value=>$label)
		{
			echo "<option value=\"{$value}\">{$label}</option>\n";
		}
		echo "</select></td>\n";

		echo "<td style=\"width:60px; text-align:center; vertical-align:middle\">\n";
		echo "<input type=\"button\" style=\"width:40px\" id=\"options_left\" value=\"&lt;\" /><br /><br />\n";
		echo "<input type=\"button\" style=\"width:40px\" id=\"options_right\" value=\"&gt;\" /><br /><br />\n";
		echo "<input type=\"button\" style=\"width:40px\" id=\"options_left_all\" value=\"&lt;&lt;\" /><br /><br />\n";
		echo "<input type=\"button\" style=\"width:40px\" id=\"options_right_all\" value=\"&gt;&gt;\" /><br /><br /></td>\n";

		echo "<td>\n";
		if(isset($this->rightTitle))
		{
			echo "<label for=\"rightTitle\">{$this->rightTitle}</label><br />\n";
		}
		echo "<select name=\"{$this->rightName}\" id=\"select_right\" multiple=\"multiple\" size=\"{$this->size}\" style=\"width:{$this->width}\">\n";
		foreach($this->rightList as $value=>$label)
		{
			echo "<option value=\"{$value}\">{$label}</option>\n";
		}
		echo "</select></td>\n";
		echo "</tr></table>\n";

		$this->registerClientScript();

		echo "<script type=\"text/javascript\"><!--\n";
		echo "\$(function() {\n";
		echo "\$(\"#select_left\").multiSelect(\"#select_right\", {trigger: \"#options_right\"});\n";
		echo "\$(\"#select_right\").multiSelect(\"#select_left\", {trigger: \"#options_left\"});\n";
		echo "\$(\"#select_left\").multiSelect(\"#select_right\", {allTrigger:\"#options_right_all\"});\n";
		echo "\$(\"#select_right\").multiSelect(\"#select_left\", {allTrigger:\"#options_left_all\"});\n";
		echo "});\n";
		echo "// --></script>\n";
		parent::init();
	}

	protected function renderContent()
	{
	}
}