xoops forums

zyspec

Module Developer
Posted on: 2011/2/9 15:19
zyspec
zyspec (Show more)
Module Developer
Posts: 1042
Since: 2004/9/21
#1

jQueryUI autocomplete local data source problem

I'm trying to implement jQueryUI autocomplete widget using a local datasource and I can't figure out what value I'm suppose to use for the 'source:' attribute.

If I reference a var then it works fine but if I reference a file (say suggest.php) then the code inside suggest.php is never executed. It appears that jqueryUI autocomplete is looking at a specific location (directory) but I can't figure out where to locate the suggest.php file.

The code inside suggest.php works fine if I call it directly
http://mysite/modules/mymodule/suggest.php?term=search_value

I've tried locating the suggest.php file in the module directory, the main Xoops directory, the /Frameworks/jquery directory, the theme directory...

The javascript 'snippet' looks like this:

$( "#termbox" ).autocomplete({
    
source"suggest.php",
             
minLength3,
       });
"


Ideas?

kerkyra

Just can't stay away
Posted on: 2011/2/9 16:51
kerkyra
kerkyra (Show more)
Just can't stay away
Posts: 553
Since: 2005/2/14
#2

Re: jQueryUI autocomplete local data source problem

try
Quote:

$( "#termbox" ).autocomplete({
source: "<{$xoops_url}>/modules/yourmodule/path_to_file/suggest.php",
minLength: 3
});"


also if minLength is your last option you dont need the comma ',' at the end of the line

zyspec

Module Developer
Posted on: 2011/2/9 18:05
zyspec
zyspec (Show more)
Module Developer
Posts: 1042
Since: 2004/9/21
#3

Re: jQueryUI autocomplete local data source problem

Thanks for the suggestion kerkyra, but it didn't make any difference.

According to the jQueryUI Autocomplete it should work with a 'relative' file name, not a URL. I also tried using XOOPS_ROOT_PATH instead of $xoops_url, no difference.

I've seen plenty of examples where this works outside of XOOPS but I can't seem to find the magic file location necessary inside of XOOPS.

Maybe I have to treat the file like it's a remote JSON source and use a $.ajax callback function instead...

kerkyra

Just can't stay away
Posted on: 2011/2/14 16:55
kerkyra
kerkyra (Show more)
Just can't stay away
Posts: 553
Since: 2005/2/14
#4

Re: jQueryUI autocomplete local data source problem

the url not being relative should work fine since it is in the same domain that you are making the ajax call. (i know because i do the same thing) :)

BUT you should indeed return your data as json.

json_encode() on your array and echo it at the end of your function. DO NOT return it.

the array should have at least the following structure
Quote:

$vals[] = array(
"id"=>your data id,
"label"=>your data label (what the drop down will show),
"value"=>the value that will assign in your input field, this could be the same as your label for example
);

apart from that you can add anything alse into the array but autocomplete NEEDS those values.
www.guidemap.gr - Beta is out...

zyspec

Module Developer
Posted on: 2011/2/14 21:54
zyspec
zyspec (Show more)
Module Developer
Posts: 1042
Since: 2004/9/21
#5

Re: jQueryUI autocomplete local data source problem

Thanks kerkyra,

I was able to get it to work using something similar to this:

<script>
$(function() {
$( 
"#termbox" ).autocomplete({
    
source: function( requestresponse ) {
        $.
getJSON("myURL",
            { 
term: $("#termbox").val(),
              
categoryID: $("#categoryID").val(),
              
pos: $("#pos").val(),
              
type: $("#type").val()
            },
            function(
data) {
                
response(data);
            });
        },
        
minLength5
    
});
});
</
script>

kerkyra

Just can't stay away
Posted on: 2011/2/15 15:57
kerkyra
kerkyra (Show more)
Just can't stay away
Posts: 553
Since: 2005/2/14
#6

Re: jQueryUI autocomplete local data source problem

if it works for you its all good :)
www.guidemap.gr - Beta is out...