views:

39

answers:

1

By default the jQuery U Autocomplete produces a list of results, upon clicking on a result it will populate the text field with the clicked result text.

I would like to change this behaviour, so that when clicking on a result it will take you to that result's page. To generate the hyperlink I can pass in the ID of the result.

I'm using PHP JSON to bring back the resultset:

$return_arr = array();

while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
    $row_array['id'] = $row['id'];
    $row_array['value'] = $row['name'];

    array_push($return_arr, $row_array);
}

echo json_encode($return_arr);

And here is my current jQuery:

$(function() {
$("#searchcompany").autocomplete( {
    source: "companies.php",
    minLength: 2
});
});
+1  A: 

Think you need to hook into the select event and supply your own function.

See here for more information.

Supply a callback function to handle the select event as an init option.

$("#searchcompany").autocomplete( {
  source: "companies.php",
  minLength: 2,
  select: function(event,ui) { //Do your code here...
                               event.preventDefault();  
                             }
});

or Bind to the select event by type: autocompleteselect.

    $( "#searchcompany" ).bind( "autocompleteselect", function(event, ui) {
      ...
    });

and to change the matching items to include a hyperlink that can be clicked use the Open event :-

open: function(event, ui) { $( 'li.ui-menu-item a').each( function() { 
                                                    var el = $(this); 
                                                    el.attr('href', el.html()); 
                                                    }  
                                                    ); } 

This will add an href="[item value]" to each <a> element.

Edit: The code below will allow you to use the open event to change the items to include a href so they show the link in the window and when clicked they will take you to the specified location :-

open: function(event, ui) { 
      $("ul.ui-autocomplete").unbind("click");

      var data = $(this).data("autocomplete");          

      for(var i=0; i<=data.options.source.length-1;i++)
      {
        var s = data.options.source[i];
        $("li.ui-menu-item a:contains(" + s.value + ")").attr("href", "directory/listing/" + s.id);
      }

    } 

Using this also means that you don't need to use the select event.

Andy Robinson
Cheers mate. I used the select: function and used document.location="/directory/listing/" + ui.item.id;BTW is there any jQuery syntax for document.location? Also how can I make it a "proper" hyperlink, i.e. when you hover over it the url appears in the status bar.
GSTAR
No syntax for document.location just use the DOM like you are. To make it look more like a hyperlink you can add your own css to style it up, something like this should work...ui-menu .ui-menu-item a { text-decoration:underline; cursor:pointer; cursor:hand; }
Andy Robinson
OK just did some more investigation. The menu items generated are in fact <a> tags without the 'href' attribute. So I assume if I can somehow add in the href attribute this will elimate the need for the document.location function and it will give me the url in the status bar. How do I add in the href attribute?
GSTAR
Guess you could use the open event to add the href to all the <a> elements within the <ul>? This seems to work - open: function(event, ui) { $('.ui-autocomplete a').attr('href','<your link here>'); }
Andy Robinson
Cheers I tried this, however it seems to have a problem when I specify ui.item.id as the href parameter - it's saying item.id is null or not an object
GSTAR
Probably something autocomplete is doing to prevent it happening. How about using document.location.href = "/directory/listing/" +ui.item in the select event function?
Andy Robinson
That open event you posted above is ideal, but again it seems to have an issue with ui.item.id (I want to be able to append the id to the URL).
GSTAR
Could this (http://dev.jqueryui.com/ticket/5305) have anything to do with the open event not working properly? I can't find a fix for this anywhere.
GSTAR
I don't think you can get at ui.item in the open event :-( but I've modified my answer to include another way to get it to work. Not sure about the link in previous comment could be that it's beforeopen which is why ui is not populated?
Andy Robinson
Hi if I use el.html() it just inserts the item text in the URL - this is incorrect, I need to insert the numerical ID which I get from the resultset.
GSTAR
Use the other Open: example I've given (after the Edit) that includes the id.
Andy Robinson
I'm afraid that hasn't worked. It doesn't do anything in fact. This is actually getting out of bounds now for what should be a simple functionality addition. Not havin' a dig at you Andy, I just think this particular functionality should be included by default.
GSTAR
Agreed should be simpler to do this. I'm not sure why the code doesn't work for you - it works on my machine :-) - I've copied the complete code listing onto my blog if you want to check it out - http://blog.andyrobinson.com/2010/07/jquery-ui-autocomplete.html
Andy Robinson