jQuery ui autocomplete - renderItem

时间:2010-09-28 13:13:29

标签: javascript jquery jquery-ui autocomplete

我正在使用_renderItem来修改结果列表

.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 )

这具有自动标记任何看起来像url作为href的行为的行为。我想将整个项目作为链接

在较旧的自动填充功能中执行了这样的操作:

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

但是这不再是支持了。

有谁知道我怎么做:

1)使用类似于.result函数的东西,只需将整个项目作为链接

2)修改_renderItem,使其不会自动将看起来像URL的字符串转换为href的

谢谢。

3 个答案:

答案 0 :(得分:10)

看来,这在以前的版本中已经发生了变化。现在你必须使用

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

答案 1 :(得分:3)

定义自动填充功能时,请使用select功能创建链接:

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

答案 2 :(得分:3)

自定义jQuery自动填充功能的更好方法是创建自己的extended version using widgets

$.widget( "custom.mySpecialAutocomplete", $.ui.autocomplete, {
  // Add the item's 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
});