This extension is a wrapper to use my own creation of Ajax Powered Autocomplete. Please, refer to my website to get a detailed description of this plugin.
Requirements ¶
I have created this extension using the latest Yii but can be used from Yii 1.0 and above.
Usage ¶
Unzip the file and:
copy jqAutocomplete.php and jqautocomplete folder to your application's protected/extensions folder.
- copy test.php to your application's protected/views folder
- copy TestController.php to your application's protected/controllers folder
- copy test_layout.php to your application's protected/layouts folder
to run the test just do http://yourapplicationurl/index.php?r=test/autocomplete
Look at the view and you will see something like this:
Yii::import('ext.jqAutocomplete');
$json_options = array(
'script'=> $this->createUrl('test/json',array('json'=>'true','limit'=>6)) . '&',
'varName'=>'input',
'shownoresults'=>true,
'maxresults'=>16,
'callback'=>'js:function(obj){ $(\'#json_info\').html(\'you have selected: \'+obj.id + \' \' + obj.value + \' (\' + obj.info + \')\'); }'
);
// #test_json is your text field id
// (please refer to the example view page for the field wrap)
jqAutocomplete::addAutocomplete('#test_json',$json_options);
Does not work with 2 or more fields
I have tried with two fields (as belown) with two script url and it still make request to the first url. Please help to fix this.
Yii::import('ext.jqautocomplete.jqAutocomplete'); $json_options = array( 'json' => true, 'script'=> $this->createUrl('/organization/autocomplete',array('json'=>'true','limit'=>6)) . '&', 'varName'=>'input', 'shownoresults'=>true, 'maxresults'=>16, 'callback'=>'js:function(obj){ $(\'#Person_organization_id\').val(obj.id); }' ); jqAutocomplete::addAutocomplete('#Person_orgName',$json_options); $json_options = array( 'json' => true, 'script'=> $this->createUrl('/person/autocomplete',array('json'=>'true','limit'=>6)) . '&', 'varName'=>'input', 'shownoresults'=>true, 'maxresults'=>16, 'callback'=>'js:function(obj){ $(\'#Person_father_id\').val(obj.id); }' ); jqAutocomplete::addAutocomplete('#Person_fatherName',$json_options);
@ tpthanx
Trying to fix it. Thanks for your report.
question
looks like only knows to search sequential ?
@Bibi40k
The search server PHP script is just an example, you can modify it to suit your needs.
example
could you please help me with an example to search for letters no matter their position inside a word and how to search for a name and submit id instead ?
Thank you
array merge 2 times
i noticed that there was 2 times merge, i think that it will be wise to have like this.
[code]
protected static function create($selector, $options = array(), $position = CClientScript::POS_END){
self::registerScript(); $options = CJavaScript::encode(CMap::mergeArray(self::defaultOptions(),$options)); Yii::app()->clientScript->registerScript(__CLASS__.$selector, 'jQuery.autoComplete("'.$selector.'",'. $options.');', $position); } public function addAutocomplete($selector, $opts = array()) { self::create($selector, $opts); }
[/code]
array merge 2 times
fix for comment of code.
protected static function create($selector, $options = array(), $position = CClientScript::POS_END){ self::registerScript(); $options = CJavaScript::encode(CMap::mergeArray(self::defaultOptions(),$options)); Yii::app()->clientScript->registerScript(__CLASS__.$selector, 'jQuery.autoComplete("'.$selector.'",'. $options.');', $position); } public function addAutocomplete($selector, $opts = array()) { self::create($selector, $opts); }
error
I followed the instructions but get:
require(C:\xampp\htdocs\protected\controllers\TestController.php) [function.require]: failed to open stream: Permission denied
@tomwittkower
"Permission denied", that is not an issue with the widget but with your security settings.
Thank you for sharing!
Thank you for sharing this.. took me a bit to get it working but it works well.
Though i'm still looking to figure out why it breaks when there are 2 on a page.
If you need multiple input field...
then you must create multiple instances.
use
protected static function create($selector, $options = array(), $position = CClientScript::POS_END){ self::registerScript(); $options = CJavaScript::encode(CMap::mergeArray(self::defaultOptions(),$options)); Yii::app()->clientScript->registerScript(__CLASS__.$selector, 'var jq = new $.autoComplete("'.$selector.'",'. $options.');', $position); }
instead
protected static function create($selector, $options = array(), $position = CClientScript::POS_END){ self::registerScript(); $options = CJavaScript::encode(CMap::mergeArray(self::defaultOptions(),$options)); Yii::app()->clientScript->registerScript(__CLASS__.$selector, 'JQuery.autoComplete("'.$selector.'",'. $options.');', $position); }
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.