jQueryUI的自动完成 - 如何匹配搜索词与关键字的列表,并显示匹配的结果吗?

问题描述 投票:3回答:1

我试图使用jQueryUI的自动填充功能来实现网站的快速搜索功能,在我的网站的各种功能的网页。我想你可以说这是像谷歌即时搜索,但它的索引我的网站上的网页。

所以,当他们搜索“创造”,将弹出创建用户选项和创建组织选项。当他们搜索“创建用”它只会显示在创建用户的选择。然后,他们可以点击结果,它会加载该页面。这些都只是一些链接。但正如你所看到的,每个页面都会有一些不同的关键字/同义词那会都指向同一个页面。

确定这样的checkSearchWordsMatchKeywords功能的尽头有一定的工作,因为我已经测试过它。什么是不工作的,我不知道我应该从jQueryUI的搜索返回:函数。

此外,如果你知道如何优化checkSearchWordsMatchKeywords()函数,那么我所有的耳朵。 :)

编辑:下面工作液更新(与jQueryUI的1.9.x的工作):

var links = [
{
    keywords: ['create', 'add', 'make', 'insert', 'user'],
    label: "Create user",
    desc: "Create a user in the system",
    url: 'http://mysite.com/user/create/'
},
{
    keywords: ['create', 'add', 'make', 'insert', 'organisation'],
    label: "Create organisation",
    desc: "Create an organisation in the system",
    url: 'http://mysite.com/organisation/create/'
}];

$('#searchTerms').autocomplete(
{
    minLength: 2,
    source: function(request, response)
    {
        var matched = [];
        var numOfLinks = links.length;

        // Get entered search terms (request.term) from user and search through all links keywords
        for (var k = 0; k < numOfLinks; k++)
        {
            // If it matches, push the object into a new array
            if (checkSearchWordsMatchKeywords(request.term, links[k].keywords))
            {
                matched.push(links[k]);
            }
        }

        // Display the filtered results
        response(matched);
    },
    focus: function(event, ui)
    {
        // When the item is selected, put the label text into the search box
        $('#searchTerms').val(ui.item.label);
        return false;
    },
    select: function(event, ui)
    {
        // Put the selected link's label in the text box and redirect to the url
        $('#searchTerms').val(ui.item.label);

        // Redirect to the page using .href so the previous page is saved into the user's browser history
        window.location.href = ui.item.url;
        return false;
    }
})
.data('autocomplete')._renderItem = function(ul, item)
{
    // Show a description underneath the link label. Using the hyperlink here too so that mouse click still works
    return $('<li></li>')
        .data('item.autocomplete', item )
        .append('<a href="' + item.url + '"><b>' + item.label + '</b><br>' + item.desc + '</a>')
        .appendTo(ul);
};

/**
 * Check that each word in a search string matches at least one keyword in an array
 * E.g. searchWords = 'create use'  and  keywords = ['create', 'add', 'make', 'insert', 'user'] will return true
 */
function checkSearchWordsMatchKeywords(searchString, keywords)
{
    var searchWords = searchString.toLowerCase().split(' ');    // Lowercase the search words & break up the search into separate words
    var numOfSearchWords = searchWords.length;                  // Count number of search words
    var numOfKeywords = keywords.length;                        // Count the number of keywords
    var matches = [];                                           // Will contain the keywords that matched the search words

    // For each search word look up the keywords array to see if the search word partially matches the keyword
    for (var i = 0; i < numOfSearchWords; i++)
    {
        // For each keyword
        for (var j = 0; j < numOfKeywords; j++)
        {   
            // Check search word is part of a keyword
            if (keywords[j].indexOf(searchWords[i]) != -1)
            {
                // Found match, store match, then look for next search word
                matches.push(keywords[j]);
                break;
            }
        }
    }

    // Count the number of matches, and if it equals the number of search words then the search words match the keywords
    if (matches.length == numOfSearchWords)
    {
        return true;
    }

    return false;
}

跳转到页

javascript jquery jquery-ui jquery-ui-autocomplete
1个回答
4
投票

我不“搜索”事件是做你追求的地方。你还是落实source选项作为回调:

$("#searchTerms").autocomplete({
    ...
    source: function(request, response) {        
        var matched = [];
        // Search "request.term" through all links keywords
        for (var k = 0; k < links.length; k++) {
            if (checkSearchWordsMatchKeywords(request.term, links[k]['keywords'])) {
                matched.push(links[k]);
            }
        }
        // display the filtered results
        response(matched);
    }
});
  • 所述request对象包含term属性其是在输入输入的文本
  • response参数是回调,你应该打个电话显示结果。

所以基本上,你和筛选数据,并把它传递给response()显示菜单。

© www.soinside.com 2019 - 2024. All rights reserved.