Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • MB 273 posts 936 karma points
    Mar 13, 2017 @ 19:13
    MB
    0

    Examine with autocomplete

    Another day, another Examine question ;)

    I'm trying to display products with autocomplete. If I add this query to my URL: http://localhost:52242/json?searchTerm=epic it renderes out the products:

    [{"id":"localhost:52242/mountain-bikes/specialized/epic-hardtails/epic-hardtail/","label":"Epic Hardtail","value":"Epic Hardtail"}

    But I need to grap that searchTerm value on the run inside Autocomplete.

    Master.cshtml

    <form method="post" action="/soegning" name="searchTerm" autocomplete="on">
                        <input type="text" id="searchTerm" placeholder="Søg produkter" name="searchTerm" size="21" maxlength="120" autocomplete="on">
                            <input type="submit" value="">
                    </form>
    
          $(function () {
            $("#searchTerm").autocomplete({
                source: "/json",
                minLength: 2,
                select: function (event, ui) {
                    //Redirect user when item selected from the id in the JSON
                    window.location.href = ui.item.id;
                }
            });
        });
    

    json.cshtml

    located in Views folder looks like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Json>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using Examine;
    @using Examine.SearchCriteria;
    @using System.Web.Script.Serialization;
    @{
        Layout = "Master.cshtml";
    }
    
    @{
        //Get the domain (http://localhost:6436)
        var siteURL =  Request.Url.Authority;
    
        //Get the values posted from the form
        var searchTerm = Request["searchTerm"];
    
        //Check if searchTerm is null from the posted form data...
        if (String.IsNullOrEmpty(searchTerm))
        {
            //Stop all other code running in this Macro
            <p>Nope</p>
            return;
        }
    
        var searcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
        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)
    

    `

Please Sign in or register to post replies

Write your reply to:

Draft