22 Yii::import(
'booster.widgets.TbBaseInputWidget');
43 if(!isset($this->datasets[
'source']))
44 $this->datasets[
'source'] = array();
50 if(empty($this->options))
51 $this->options[
'minLength'] = 1;
54 if(!isset($this->htmlOptions[
'class']) || empty($this->htmlOptions[
'class']))
55 $this->htmlOptions[
'class'] =
'typeahead';
57 $this->htmlOptions[
'class'] .=
' typeahead';
65 public function run() {
67 list($name, $id) = $this->resolveNameID();
69 if (isset($this->htmlOptions[
'id'])) {
70 $id = $this->htmlOptions[
'id'];
72 $this->htmlOptions[
'id'] = $id;
75 if (isset($this->htmlOptions[
'name'])) {
76 $name = $this->htmlOptions[
'name'];
79 if ($this->hasModel()) {
80 echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
82 echo CHtml::textField($name, $this->
value, $this->htmlOptions);
85 $this->datasets[
'source'] =
'js:substringMatcher(_'.$this->id.
'_source_list)';
87 $options = CJavaScript::encode($this->options);
88 $datasets = CJavaScript::encode($this->datasets);
90 Yii::app()->clientScript->registerScript(__CLASS__ .
'#' . $id,
"jQuery('#{$id}').typeahead({$options}, {$datasets});");
101 $booster->registerPackage(
'typeahead');
103 if(empty($this->datasets) || !isset($this->datasets[
'source']) || !is_array($this->datasets[
'source']))
106 Yii::app()->clientScript->registerScript(__CLASS__ .
'#substringMatcher',
'
107 var substringMatcher = function(strs) {
108 return function findMatches(q, cb) {
109 var matches, substringRegex;
111 // an array that will be populated with substring matches
114 // regex used to determine if a string contains the substring `q`
115 substrRegex = new RegExp(q, "i");
117 // iterate through the pool of strings and for any string that
118 // contains the substring `q`, add it to the `matches` array
119 $.each(strs, function(i, str) {
120 if (substrRegex.test(str)) {
121 // the typeahead jQuery plugin expects suggestions to a
122 // JavaScript object, refer to typeahead docs for more info
123 matches.push({ value: str });
130 ', CClientScript::POS_HEAD);
132 $source_list = !empty($this->options) ? CJavaScript::encode($this->datasets[
'source']) :
'';
133 Yii::app()->clientScript->registerScript(__CLASS__ .
'#source_list#'.$this->
id,
'
134 var _'.$this->
id.
'_source_list = '.$source_list.
';
135 ', CClientScript::POS_HEAD);