JQuery ui autocomplete - renderItem

I use _renderItem to change the list of results

.data( "autocomplete" )._renderItem = function( ul, item ) {
            var temp = item.url.substring(16, item.url.length)
            return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.value + "<br>" + item.url + "<br>" + item.description + "<br>" + "Support URL: " + item.support_url + "<br>" + "Contact: " + "<a href=" + item.contact + ">Test</a>" + "<br />" + "</a>"  )
            .appendTo( ul )

It has a behavior of automatically marking everything that looks like a url like href. I would like to make the whole element a link

in the older autocomplete, which was done as follows:

 .result(function(event, item) {
   location.href = item.url;
  });

But this will not support the seam.

Does anyone know how I can:

1) use something similar to the .result function and just make the whole element a link
or
2) change _renderItem so that it does not automatically turn strings that look like URLs into href's

Thank.

+5
source share
3 answers

, .

$element.data('uiAutocomplete')._renderItem()
+10

, , :

$('selector').autocomplete({
    source: ...,
    select: function(event, ui) { window.location = ui.url; }
});
+3

The best approach to setting jQuery autocomplete is to create your own extended version using widgets .

$.widget( "custom.mySpecialAutocomplete", $.ui.autocomplete, {
  // Add the item value as a data attribute on the <li>.
  _renderItem: function( ul, item ) {
    return $( "<li>" )
      .attr( "data-value", item.value )
      .append( $( "<a>" ).text( item.label ) )
      .appendTo( ul );
  },
  // Add a CSS class name to the odd menu items.
  _renderMenu: function( ul, items ) {
    var that = this;
    $.each( items, function( index, item ) {
      that._renderItemData( ul, item );
    });
    $( ul ).find( "li:odd" ).addClass( "odd" );
  }
});

var availableTags = [
  "ActionScript",
  "AppleScript",
  "Asp",
  "BASIC",
  "C",
  "C++",
  "Clojure",
  "COBOL",
  "ColdFusion",
  "Erlang",
  "Fortran",
  "Groovy",
  "Haskell",
  "Java",
  "JavaScript",
  "Lisp",
  "Perl",
  "PHP",
  "Python",
  "Ruby",
  "Scala",
  "Scheme"
];

$('#myElement').mySpecialAutocomplete({
  source: availableTags
});
+3
source

All Articles