Home > Web Front-end > JS Tutorial > body text

How to Customize the Result Display Format in the Autocomplete Plugin?

Patricia Arquette
Release: 2024-10-21 08:07:02
Original
643 people have browsed it

How to Customize the Result Display Format in the Autocomplete Plugin?

Customizing Result Display Format in Autocomplete Plugin

The jQuery UI Autocomplete plug-in provides a powerful way to handle user input and suggest relevant options. By default, the drop-down results display the matches of user input within the suggested items. However, you may desire a more customized format, such as highlighting the search characters in the displayed results.

Monkey-Patching the Plugin

To achieve this, you can employ the technique known as "monkey-patching," where you redefine an internal function within the library. In this case, you need to override the _renderItem function responsible for creating each item in the drop-down list.

Creating the Customized _renderItem Function

Here's an example of a customized _renderItem function:

$.ui.autocomplete.prototype._renderItem = function( ul, item) {
  var re = new RegExp("^" + this.term);
  var t = item.label.replace(re,"<span style='font-weight:bold;color:Blue;'>" + this.term + "</span>");
  return $( "<li></li>" )
    .data( "item.autocomplete", item )
    .append( "<a>" + t + "</a>" )
    .appendTo( ul );
};
Copy after login

This function uses a regular expression to isolate the matching characters and wraps them in an HTML span element with specific styling. The span element uses a bold font and a blue color to highlight the matches.

Applying the Patch

Once you have created the customized function, you can apply it to the Autocomplete widget by calling the following function within the document ready event:

monkeyPatchAutocomplete();
Copy after login

This function will replace the default _renderItem function with your customized version.

Preserving Character Case

Note that the above code highlights matches but does not preserve the case of the original input. To preserve the case, modify the replacement line within the _renderItem function as follows:

var t = item.label.replace(re,"<span style='font-weight:bold;color:Blue;'>" + "$&amp;" + "</span>");
Copy after login

Targeted Patching

The above changes affect all Autocomplete widgets on the page. If you want to customize only a specific instance, refer to the question "How to patch just one instance of Autocomplete on a page?"

The above is the detailed content of How to Customize the Result Display Format in the Autocomplete Plugin?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!