1: <?php
2: /**
3: * CJuiAutoComplete class file.
4: *
5: * @author Sebastian Thierer <sebathi@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: Yii::import('zii.widgets.jui.CJuiInputWidget');
12:
13: /**
14: * CJuiAutoComplete displays an autocomplete field.
15: *
16: * CJuiAutoComplete encapsulates the {@link http://jqueryui.com/autocomplete/ JUI
17: * autocomplete} plugin.
18: *
19: * To use this widget, you may insert the following code in a view:
20: * <pre>
21: * $this->widget('zii.widgets.jui.CJuiAutoComplete',array(
22: * 'name'=>'city',
23: * 'source'=>array('ac1','ac2','ac3'),
24: * // additional javascript options for the autocomplete plugin
25: * 'options'=>array(
26: * 'minLength'=>'2',
27: * ),
28: * 'htmlOptions'=>array(
29: * 'style'=>'height:20px;',
30: * ),
31: * ));
32: * </pre>
33: *
34: * By configuring the {@link options} property, you may specify the options
35: * that need to be passed to the JUI autocomplete plugin. Please refer to
36: * the {@link http://api.jqueryui.com/autocomplete/ JUI AutoComplete API}
37: * documentation for possible options (name-value pairs) and
38: * {@link http://jqueryui.com/autocomplete/ JUI AutoComplete page} for
39: * general description and demo.
40: *
41: * By configuring the {@link source} property, you may specify where to search
42: * the autocomplete options for each item. If source is an array, the list is
43: * used for autocomplete. You may also configure {@link sourceUrl} to retrieve
44: * autocomplete items from an ajax response.
45: *
46: * @author Sebastian Thierer <sebathi@gmail.com>
47: * @package zii.widgets.jui
48: * @since 1.1.2
49: */
50: class CJuiAutoComplete extends CJuiInputWidget
51: {
52: /**
53: * @var mixed the entries that the autocomplete should choose from. This can be
54: * <ul>
55: * <li>an Array with local data</li>
56: * <li>a String, specifying a URL that returns JSON data as the entries.</li>
57: * <li>a javascript callback. Please make sure you wrap the callback with
58: * {@link CJavaScriptExpression} in this case.</li>
59: * </ul>
60: */
61: public $source=array();
62: /**
63: * @var mixed the URL that will return JSON data as the autocomplete items.
64: * CHtml::normalizeUrl() will be applied to this property to convert the property
65: * into a proper URL. When this property is set, the {@link source} property will be ignored.
66: */
67: public $sourceUrl;
68:
69: /**
70: * Run this widget.
71: * This method registers necessary javascript and renders the needed HTML code.
72: */
73: public function run()
74: {
75: list($name,$id)=$this->resolveNameID();
76:
77: if(isset($this->htmlOptions['id']))
78: $id=$this->htmlOptions['id'];
79: else
80: $this->htmlOptions['id']=$id;
81: if(isset($this->htmlOptions['name']))
82: $name=$this->htmlOptions['name'];
83:
84: if($this->hasModel())
85: echo CHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);
86: else
87: echo CHtml::textField($name,$this->value,$this->htmlOptions);
88:
89: if($this->sourceUrl!==null)
90: $this->options['source']=CHtml::normalizeUrl($this->sourceUrl);
91: else
92: $this->options['source']=$this->source;
93:
94: $options=CJavaScript::encode($this->options);
95: Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').autocomplete($options);");
96: }
97: }
98: