Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 106 lines (84 sloc) 2.921 kb
9b4e526 @ianare initial import
ianare authored
1 <?php
2
3 /**
4 * jQuery combobox Yii extension
5 *
6 * Allows selecting a value from a dropdown list or entering in text.
7 * Also works as an autocomplete for items in the select.
8 *
9 * @copyright © Digitick <www.digitick.net> 2011
10 * @license GNU Lesser General Public License v3.0
11 * @author Ianaré Sévi
12 * @author Jacques Basseck
13 *
14 */
15 Yii::import('zii.widgets.jui.CJuiInputWidget');
16
17 /**
18 * Base class.
19 */
20 class EJuiComboBox extends CJuiInputWidget
21 {
22 /**
23 * @var array the entries that the autocomplete should choose from.
24 */
c0bc5b5 @kanyuga Update EJuiComboBox.php
authored
25
9b4e526 @ianare initial import
ianare authored
26 public $data = array();
c0bc5b5 @kanyuga Update EJuiComboBox.php
authored
27
28 /**
29 * @var bool whether the $data is an associative array or not
30 */
31
32 public $assoc=true;
33
9b4e526 @ianare initial import
ianare authored
34 /**
35 * @var string A jQuery selector used to apply the widget to the element(s).
36 * Use this to have the elements keep their binding when the DOM is manipulated
37 * by Javascript, ie ajax calls or cloning.
38 * Can also be useful when there are several elements that share the same settings,
39 * to cut down on the amount of JS injected into the HTML.
40 */
41 public $scriptSelector;
42 public $defaultOptions = array('allowText' => true);
43
44 protected function setSelector($id, $script, $event=null)
45 {
46 if ($this->scriptSelector) {
47 if (!$event)
48 $event = 'focusin';
49 $js = "jQuery('body').delegate('{$this->scriptSelector}','{$event}',function(e){\$(this).{$script}});";
50 $id = $this->scriptSelector;
51 }
52 else
53 $js = "jQuery('#{$id}').{$script}";
54 return array($id, $js);
55 }
56
57 public function init()
58 {
59 $cs = Yii::app()->getClientScript();
60 $assets = Yii::app()->getAssetManager()->publish(dirname(__FILE__) . '/assets');
61 $cs->registerScriptFile($assets . '/jquery.ui.widget.min.js');
62 $cs->registerScriptFile($assets . '/jquery.ui.combobox.js');
63
64 parent::init();
65 }
66
67 /**
68 * Run this widget.
69 * This method registers necessary javascript and renders the needed HTML code.
70 */
94a957b @kanyuga Update EJuiComboBox.php
authored
71
9b4e526 @ianare initial import
ianare authored
72 public function run()
73 {
74 list($name, $id) = $this->resolveNameID();
75
76 if (is_array($this->data) && !empty($this->data)){
94a957b @kanyuga Update EJuiComboBox.php
authored
77 //if $data is not an assoc array make each value its key
c0bc5b5 @kanyuga Update EJuiComboBox.php
authored
78 $data=($this->assoc)?$this->data:array_combine($this->data, $this->data);
79
80 //does the same as array_unshift($data,null) but does not break assoc arrays
81 $data=array(""=>"")+$data;
9b4e526 @ianare initial import
ianare authored
82 }
c0bc5b5 @kanyuga Update EJuiComboBox.php
authored
83
9b4e526 @ianare initial import
ianare authored
84 else
85 $data = array();
94a957b @kanyuga Update EJuiComboBox.php
authored
86
9b4e526 @ianare initial import
ianare authored
87 if ($this->hasModel())
94a957b @kanyuga Update EJuiComboBox.php
authored
88 echo CHtml::activeDropDownList($this->model,$this->attribute,$data);
9b4e526 @ianare initial import
ianare authored
89 else
94a957b @kanyuga Update EJuiComboBox.php
authored
90 echo CHtml::dropDownList($name, $this->value, $data);
91
f08d9c6 @kanyuga Update EJuiComboBox.php
authored
92 echo CHtml::textField(null,($this->hasModel()?$data[CHtml::resolveValue($this->model,$this->attribute)]:$data[$this->value]),array('id'=>$id.'_combobox'));
9b4e526 @ianare initial import
ianare authored
93 $this->options = array_merge($this->defaultOptions, $this->options);
94
95 $options = CJavaScript::encode($this->options);
96
97 $cs = Yii::app()->getClientScript();
98
99 $js = "combobox({$options});";
100
94a957b @kanyuga Update EJuiComboBox.php
authored
101 list($id, $js) = $this->setSelector($id.'_combobox', $js);
9b4e526 @ianare initial import
ianare authored
102 $cs->registerScript(__CLASS__ . '#' . $id, $js);
103 }
104
105 }
Something went wrong with that request. Please try again.