CJuiAutoComplete.php
3.11 KB
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
<?php
/**
* CJuiAutoComplete class file.
*
* @author Sebastian Thierer <sebathi@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
Yii::import('zii.widgets.jui.CJuiInputWidget');
/**
* CJuiAutoComplete displays an autocomplete field.
*
* CJuiAutoComplete encapsulates the {@link http://jqueryui.com/demos/autocomplete/ JUI
* autocomplete} plugin.
*
* To use this widget, you may insert the following code in a view:
* <pre>
* $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
* 'name'=>'city',
* 'source'=>array('ac1', 'ac2', 'ac3'),
* // additional javascript options for the autocomplete plugin
* 'options'=>array(
* 'minLength'=>'2',
* ),
* 'htmlOptions'=>array(
* 'style'=>'height:20px;'
* ),
* ));
* </pre>
*
* By configuring the {@link options} property, you may specify the options
* that need to be passed to the JUI autocomplete plugin. Please refer to
* the {@link http://jqueryui.com/demos/autocomplete/ JUI
* autocomplete} documentation for possible options (name-value pairs).
*
* By configuring the {@link source} property, you may specify where to search
* the autocomplete options for each item. If source is an array, the list is
* used for autocomplete. You may also configure {@link sourceUrl} to retrieve
* autocomplete items from an ajax response.
*
* @author Sebastian Thierer <sebathi@gmail.com>
* @version $Id$
* @package zii.widgets.jui
* @since 1.1.2
*/
class CJuiAutoComplete extends CJuiInputWidget
{
/**
* @var mixed the entries that the autocomplete should choose from. This can be
* <ul>
* <li>an Array with local data</li>
* <li>a String, specifying a URL that returns JSON data as the entries.</li>
* <li>a javascript callback. Please make sure you wrap the callback with
* {@link CJavaScriptExpression} in this case.</li>
* </ul>
*/
public $source = array();
/**
* @var mixed the URL that will return JSON data as the autocomplete items.
* CHtml::normalizeUrl() will be applied to this property to convert the property
* into a proper URL. When this property is set, the {@link source} property will be ignored.
*/
public $sourceUrl;
/**
* Run this widget.
* This method registers necessary javascript and renders the needed HTML code.
*/
public 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'];
if($this->hasModel())
echo CHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);
else
echo CHtml::textField($name,$this->value,$this->htmlOptions);
if($this->sourceUrl!==null)
$this->options['source']=CHtml::normalizeUrl($this->sourceUrl);
else
$this->options['source']=$this->source;
$options=CJavaScript::encode($this->options);
$js = "jQuery('#{$id}').autocomplete($options);";
$cs = Yii::app()->getClientScript();
$cs->registerScript(__CLASS__.'#'.$id, $js);
}
}