【问题标题】:Umbraco 7 examine JSON search - autocomplete not workingUmbraco 7 检查 JSON 搜索 - 自动完成功能不起作用
【发布时间】:2015-07-15 10:40:37
【问题描述】:

我有一个有效的搜索框,想添加自动完成功能。它一直显示“没有搜索结果”。

当我在输入中输入单词“Umbraco”时,我确实在控制台中看到了以下内容,但它没有自动完成:

[{"id":"http://localhost:33968/explore/our-umbraco/","label":"Our Umbraco","value":"Our Umbraco"},{"id" :"http://localhost:33968/extend/umbraco-forms/","label":"Umbraco Forms","value":"Umbraco Forms"}]

HTML

<form method="post" action="/results">
    <input type="text" id="searchTerm" name="searchTerm" size="21" maxlength="120">
    <input type="submit" value="Search">
</form>

自动完成.js

 $(function() {
    $( "#searchTerm" ).autocomplete({
        source: "/SearchJSON",
                minLength: 2,
                select: function (event, ui) {
                        //Redirect user when item selected from the id in the JSON
                        window.location.href = ui.item.id;
                }
    });
});

Search_JSON.cshtml

@using Examine
@using Examine.SearchCriteria
@using System.Web.Script.Serialization

@{
    //Get the domain (http://localhost:6436)
    var siteURL = "http://" + Request.Url.Authority;

    //Get the values posted from the form
    var searchTerm = Request["term"];

    //Check if searchTerm is null from the posted form data...
    if (String.IsNullOrEmpty(searchTerm))
    {
        //Stop all other code running in this Macro
        return;
    }

    var searcher = ExamineManager.Instance.SearchProviderCollection["RazorSiteSearcher"];
    var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);
    var query = searchCriteria.GroupedOr(new string[] { "nodeName", "bodyText" }, searchTerm).Compile();
    var searchResults = searcher.Search(query);

    /*
    EXAMPLE JSON
    [{ "id": "http://localhost/about.aspx", "label": "About", "value": "About" }]
    */

    List<dynamic> searchResultKeyVals = new List<dynamic>();

    //Convert the search results as JSON
    foreach(var result in searchResults)
    {
        searchResultKeyVals.Add(new {
            id = siteURL + umbraco.library.NiceUrl(result.Id), 
            label = result.Fields["nodeName"],
            value = result.Fields["nodeName"]
        });
    }

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    var JSONResults = serializer.Serialize(searchResultKeyVals);    
}

@* Ouput the JSON *@
@Html.Raw(JSONResults)

Search_Results.cshtml

@using Examine
@using Examine.SearchCriteria
@{
    //Get the values posted from the form
    var searchTerm = Request.Form["searchTerm"];

    //Check if searchQuery is null from the posted form data...
    if (searchTerm == null)
    {
        //If it is null then the form was not posted and the page was visited directly
        <p>Please use the search box</p>

        //Stop all other code running in this Macro
        return;
    }

    var searcher = ExamineManager.Instance.SearchProviderCollection["RazorSiteSearcher"];
    var searchCriteria = searcher.CreateSearchCriteria(BooleanOperation.Or);

    var query = searchCriteria.GroupedOr(new string[] { "nodeName", "bodyText" }, searchTerm).Compile();
    var searchResults   = searcher.Search(query);
    var noResults       = searchResults.Count();

    <p>You searched for @searchTerm, and found @noResults results</p>

    <ul class="search-results">
        @foreach (var result in searchResults)
        {
            <li>
                <a href="@umbraco.library.NiceUrl(result.Id)">@result.Fields["nodeName"]</a>
            </li>
        }
    </ul>
}

【问题讨论】:

标签: jquery json razor autocomplete umbraco7


【解决方案1】:

自己解决了。我的别名命名不正确!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2020-11-14
    相关资源
    最近更新 更多