1 <?php
2 3 4 5 6 7 8 9
10
11 12 13 14
15 class TbTypeahead extends CInputWidget
16 {
17 18 19
20 public $options = array();
21
22 23 24
25 public function init()
26 {
27 $this->htmlOptions['type'] = 'text';
28 $this->htmlOptions['data-provide'] = 'typeahead';
29 }
30
31 32 33
34 public function run()
35 {
36 list($name, $id) = $this->resolveNameID();
37
38 if (isset($this->htmlOptions['id']))
39 $id = $this->htmlOptions['id'];
40 else
41 $this->htmlOptions['id'] = $id;
42
43 if (isset($this->htmlOptions['name']))
44 $name = $this->htmlOptions['name'];
45
46 if ($this->hasModel())
47 echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
48 else
49 echo CHtml::textField($name, $this->value, $this->htmlOptions);
50
51 $options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
52 Yii::app()->clientScript->registerScript(__CLASS__.'#'.$id, "jQuery('#{$id}').typeahead({$options});");
53 }
54 }
55