1. Foo Dzio
  2. yii-bootstrap

Commits

Christoffer Niska  committed 11d01d6

improved typeahead (fixes #115)

  • Participants
  • Parent commits 3e7cfbe
  • Branches 1.0.0-wip

Comments (0)

Files changed (2)

File demo/protected/views/site/index.php

View file
 	<div class="well">
 
 		<?php $this->widget('bootstrap.widgets.TbTypeahead', array(
+            'name'=>'typeahead',
 			'options'=>array(
 				'source'=>array('Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'),
 				'items'=>4,
 
 <?php echo $phpLighter->highlight("<?php \$this->widget('bootstrap.widgets.TbTypeahead', array(
 	'options'=>array(
+	    'name'=>'typeahead',
 		'source'=>array('Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'),
 		'items'=>4,
 		'matcher'=>\"js:function(item) {

File widgets/TbTypeahead.php

View file
 /**
  * Bootstrap type-a-head widget.
  */
-class TbTypeahead extends CWidget
+class TbTypeahead extends CInputWidget
 {
 	/**
 	 * @var array the options for the Bootstrap JavaScript plugin.
 	 */
 	public $options = array();
-	/**
-	 * @var array the HTML attributes for the widget container.
-	 */
-	public $htmlOptions = array();
 
 	/**
 	 * Initializes the widget.
 	 */
 	public function run()
 	{
-		$id = $this->id;
+        list($name, $id) = $this->resolveNameID();
 
-		echo CHtml::tag('input', $this->htmlOptions);
+        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);
 
 		$options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
 		Yii::app()->clientScript->registerScript(__CLASS__.'#'.$id, "jQuery('#{$id}').typeahead({$options});");