Yii Framework Forum: jui.autocomplete: use function as source - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

jui.autocomplete: use function as source Rate Topic: -----

#1 User is offline   rall0r 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 181
  • Joined: 11-November 10
  • Location:Bln

Posted 05 May 2011 - 06:25 AM

Hi,

i'm using zii.widgets.jui.CJuiAutoComplete widget, where autocompletion works fine, as log as there is an URL as source for the suggestions.
        $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
                        'model'=>$model,
                        'source'=>$this->createUrl('ajax/autoComplete'),
[...]


But now I need the suggestions coming from an javascript function, like this:

'source'=>'js: autoCompleteTags();'


But it seems not to be working.

Is it possible to use an function as source? How should the javascript function be designed? I tried something like this:

function autoCompleteTags{

    var Tags= [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++"
    ];
    return Tags;
    }


(This is just an simple example. The JS function code will be replaced by some more complex code. But the situation will be the same)

Thank you for any kind of help!
0

#2 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 05 May 2011 - 08:21 AM

I don't think you can have a comma after the last array element in JS.

If nothing else helps
$( ".selector" ).autocomplete( "option", "source", callback);

http://jqueryui.com/.../#option-source

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   rall0r 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 181
  • Joined: 11-November 10
  • Location:Bln

Posted 05 May 2011 - 08:57 AM

Hi tri,


View Posttri, on 05 May 2011 - 08:21 AM, said:

I don't think you can have a comma after the last array element in JS.

Sorry, the comma is just an smal error from cutting the large list into a small example ;-)

View Posttri, on 05 May 2011 - 08:21 AM, said:

If nothing else helps
$( ".selector" ).autocomplete( "option", "source", callback);

http://jqueryui.com/.../#option-source



I still have looked at the jQuery documentation. Sorry, I'm a little bit of helpless, how to write an callback function. Or is it possible simple to put the function name there?
Like this:
$( ".selector" ).autocomplete( "option", "source", "autoCompleteTags();");

0

#4 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 05 May 2011 - 09:36 AM

There's another possible typo
function autoCompleteTags{
// should be 
function autoCompleteTags() {


I erroneously used the term callback here
$( ".selector" ).autocomplete( "option", "source", callback);

Actually this should be the return value from the function call (that is the parenthesis should be there). My first guess would be that you should use just the name of the function to specify a callback in the source property of CJuiAutoComplete.

Edit: or perhaps not. On second thought, passing a string defining a js function call would give the same result as a string containing a js array.

/Tommy

This post has been edited by tri: 05 May 2011 - 09:55 AM

Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   rall0r 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 181
  • Joined: 11-November 10
  • Location:Bln

Posted 06 May 2011 - 07:46 AM

to keed it simple, i tried the following:

at the view:
			$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
    							'model'=>$model,
    							'attribute'=>'PHRASE',
    							'id'=>'tags',
    							'source'=>'js:m();',
    							'options'=>array( 'showAnim'=>'fold'),
								));

On an central Javascript file there is the following code:
function m() {
	d=new Array("a","A");
	return d;
}


If you look at this: http://www.yiiframew...e#source-detail an JS Funktion should be work. But it does not. There is no JS Error but also no drop-down box with suggestion "a" or "AA" by typing a.

:-(

maybe I have an completely wrong understanding of this topic?
0

#6 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 06 May 2011 - 09:01 AM

View Postrall0r, on 06 May 2011 - 07:46 AM, said:

to keed it simple, i tried the following:

at the view:
			$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
    							'model'=>$model,
    							'attribute'=>'PHRASE',
    							'id'=>'tags',
    							'source'=>'js:m();',
    							'options'=>array( 'showAnim'=>'fold'),
								));

...


I did some testing and found out it will work if you pass a string containing a js function call, but only if you omit the trailing semicolon.

 'source'=>'js:m()',

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#7 User is offline   rall0r 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 181
  • Joined: 11-November 10
  • Location:Bln

Posted 06 May 2011 - 09:37 AM

great - that's it!
Thank you so much!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users