jQuery UI Autocomplete v1.8.14多字搜索+突出显示

jmm*_*jmm 5 regex jquery jquery-ui jquery-ui-autocomplete

我正在尝试使用jQuery UI Autocomplete v1.8.14实现两件事.

1)对所有匹配使用空格分隔词搜索(独立于顺序):(例如,搜索 "某些心脏",匹配 "赢得某人的心脏")

2)选择所有的比赛的:(如搜索 "一些心脏",匹配 "赢得了心脏某些一")

这个问题已被多次询问,但我一直无法找到显示正确实现的简单,可重现的代码.

我已经开始了一个JS小提琴:http://jsfiddle.net/KywMH/2/.如果可能,请以可重现代码的形式回答.

谢谢.

jmm*_*jmm 6

下面的解决方案解决了这两个问题

1)修改自动完成源功能

2)修改.data('autocomplete')._ renderItem

一个工作的JS小提琴可以在这里找到:http://jsfiddle.net/W8MKt/7/

任何对代码或解决方案的批评都将受到赞赏.

谢谢.

<div class="ui-widget">
<label for="tags">Multi-word search: </label>
<input id="tags">
</div>

$(function() {
    var availableTags = [
        "win the day",
        "win the heart of",
        "win the heart of someone"
    ];

    $( "#tags" ).autocomplete({
        source: function(request, response) {

            var aryResponse = [];
            var arySplitRequest = request.term.split(" ");

            for( i = 0; i < availableTags.length; i++ ) {

                var intCount = 0;
                for( j = 0; j < arySplitRequest.length; j++ ) {

                    regexp = new RegExp(arySplitRequest[j]);

                    var test = availableTags[i].match(regexp);

                    if( test ) {

                        intCount++;

                    } else if( !test ) {

                    intCount = arySplitRequest.length + 1;

                    }

                    if ( intCount == arySplitRequest.length ) {

                        aryResponse.push( availableTags[i] );

                    }
                };
            }

            response(aryResponse);

        }

    }).data('autocomplete')._renderItem = function( ul, item ) {

        var srchTerm = $.trim(this.term).split(/\s+/).join ('|');

        var strNewLabel = item.label;

            regexp = new RegExp ('(' + srchTerm + ')', "ig");

            var strNewLabel = strNewLabel.replace(regexp,"<span style='font-weight:bold;color:Blue;'>$1</span>");

      return $( "<li></li>" )
          .data( "item.autocomplete", item )
          .append( "<a>" + strNewLabel + "</a>" )
          .appendTo( ul );

   };

});
Run Code Online (Sandbox Code Playgroud)


Bro*_*ams 6

这是一种方式: 在jsFiddle中查看它.

var availableTags = [
    "win the day",
    "win the heart of",
    "win the heart of someone"
];
var autoCompNodeId  = 'tags';

$( "#" + autoCompNodeId ).autocomplete ( {
    source: function (requestObj, responseFunc) {
                var matchArry   = availableTags.slice (); //-- Copy the array
                var srchTerms   = $.trim (requestObj.term).split (/\s+/);

                //--- For each search term, remove non-matches.
                $.each (srchTerms, function (J, term) {
                    var regX    = new RegExp (term, "i");
                    matchArry   = $.map (matchArry, function (item) {
                        return regX.test (item)  ?  item  : null;
                    } );
                } );

                //--- Return the match results.
                responseFunc (matchArry);
            },

    open:   function (event, ui) {
                /*--- This function provides no hooks to the results list, so we have to trust the
                    selector, for now.
                */
                var resultsList = $("ul.ui-autocomplete > li.ui-menu-item > a");
                var srchTerm    = $.trim ( $("#" + autoCompNodeId).val () ).split (/\s+/).join ('|');

                //--- Loop through the results list and highlight the terms.
                resultsList.each ( function () {
                    var jThis   = $(this);
                    var regX    = new RegExp ('(' + srchTerm + ')', "ig");
                    var oldTxt  = jThis.text ();

                    jThis.html (oldTxt.replace (regX, '<span class="srchHilite">$1</span>') );
                } );
            }
} );
Run Code Online (Sandbox Code Playgroud)


由于旧的突出显示功能不再可用,我们使用open事件突出显示搜索词.请特别注意不要突出显示内部标签.