CJuiAutoComplete.php 3.1 KB

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