views:

475

answers:

1

I've been using this tutorial to make an autocomplete function. (UPDATE: I ditched the jQuery UI Autocomplete route because there isn't much documentation for it.) The following code works (FINALLY). I also managed to get the suggestion links to work as well. I plan to make some type of blog post to help other noobs like myself figure it out. :)

$("#q").result(function(event, data, formatted) {
document.location.href = row.url;
});

$("#q").autocomplete("/a_complete.php", {
    dataType: 'json',
    parse: function(data) {
        var rows = new Array();
        for(var i=0; i<data.length; i++){
            rows[i] = { data:data[i], value:data[i].pos, value:data[i].team, value:data[i].url, result:data[i].value };
        }
        return rows;
    },
    formatItem: function(row, i, n) {
        return '<a href="' + row.url + '">' + row.value + ' <span style="float: right; font-size: 11px; color: gray; padding-right: 10px;"><strong style="">' + row.pos + '</strong> ' + row.team  +' </span></a>';
    },
    extraParams: {
        q: '',
        limit: '',
        sport: '<?=$sport?>',
        featureClass: 'P',
        style: 'full',
        maxRows: 15,
        term: function () { return $("#q").val() }
    },
    max: 25,
    scrollHeight: 300,
    width: 200
});             
A: 

I've been meaning to play around with the brand new autocomplete too, but havent had chance to as of yet... however have you seen this tutorial? http://net.tutsplus.com/tutorials/javascript-ajax/how-to-use-the-jquery-ui-autocomplete-widget/. There might be something in that?

Ooo also spotted this? http://stackoverflow.com/questions/737453/what-does-formatresult-and-formatitem-options-do-in-jquery-autocomplete from a previous poster, looks like you have to do a little bit of tweaking to get it to work, failing this you could concatenate the data into one chunk and pass it through that way?

Hope that helps :)

Rob
Thanks for the reply Rob.I haven't come across that tutsplus tutorial before, it does look helpful. My plan is to give up on it today and take a fresh look at it tomorrow morning. My brain needs a reboot =PIf anyone else has come across anything helpful, please do post as well. I'm sure it can help me and others as well. :)
brant
hehe i know the feeling brant, let me know how you get on with that tuts+ tutorial, i fancy having a go myself! if i come up with anything good ill repost on here for you all to see :)
Rob
Rob!! Good news, I've managed to get a lot further. Though I am stuck on trying to get the turned output to actually go to the link. Any ideas?
brant
hmm, not sure... you could try just printing the output just to make sure what u are outputting is correct, also i'm not sure whether in your extra params: sport: '<?=$sport?>' should be sport: "<?=$sport?>" ?
Rob