I'm looking to create a predictive search for a textbox on a site.
I want to use ajax calls to go get data from the server.
However, the predictive search I'm creating needs to be "categorised", and I was hoping someone here could guide me on what the best plugin is for predictive search dropdowns. Here's an example of what it should look like:
E.g. when I type in "B":
Countries
B elgium
B razil
Cities
B arcelona
I was struggling to get JQuery's predictive search working and found it nasty (perhaps due to my own incompetence), and I was wondering if there's any others out there which are recommended.
Any example code would be appreciated too if possible!
Great reply thank you, but the search text isn't hitting the API controller, and there's no errors. Here's my code:
C#:
public class NavbarController : UmbracoApiController
{
[System.Web.Http.HttpPost]
public NavbarSearchResult Search(string criteria)
{
var model = new NavbarSearchResult();
return model;
}
}
It doesn't appear that you are passing the search criteria argument into your NavbarSearchResult() object.
There is no [JsonOnlyConfiguration] attribute to tell the api controller to return Json. By default without any attribute you will get back XML.
You can use Chrome Dev inspector and look at Network Tab (XHR) to see the ajax request and response to confirm the data being sent and received.
I would replace the search method first with a call to return a Json object just to make sure the plumbing is working before you get into passing search criteria.
e.g.
using Skybrud.WebApi.Json;
using Umbraco.Web.WebApi;
namespace WebApplication1.Controllers {
[JsonOnlyConfiguration]
public class JsonTestController : UmbracoApiController {
public object GetTest() {
return new {
meta = new {
code = 200
},
data = "Yay! We have some JSON!"
};
}
}
}
With regards to hitting the controller you should be able to set a breakpoint in Visual Studio. If it doesn't get hit then the url path is wrong, you might need a tilda ~ at the start e.g ~/umbraco/api/...
Predictive Search
Hi All
I'm looking to create a predictive search for a textbox on a site.
I want to use ajax calls to go get data from the server.
However, the predictive search I'm creating needs to be "categorised", and I was hoping someone here could guide me on what the best plugin is for predictive search dropdowns. Here's an example of what it should look like:
E.g. when I type in "B":
Countries
B elgium
B razil
Cities
B arcelona
I was struggling to get JQuery's predictive search working and found it nasty (perhaps due to my own incompetence), and I was wondering if there's any others out there which are recommended.
Any example code would be appreciated too if possible!
Cheers
Hi Alex,
I have been looking at building something very similar. I have a PoC working that uses this plugin, it also supports categories like you requested.
http://easyautocomplete.com/examples
HTML:
Json:
Javascript:
You will need to create a UmbracoApiController to serve up your json. I found this library helps create the Json https://github.com/abjerner/Skybrud.WebApi.Json.
Just update the url property to point at your umbraco api controller
Best of luck
Hi David
Great reply thank you, but the search text isn't hitting the API controller, and there's no errors. Here's my code:
C#:
HTML:
JS:
I've also included the AutoComplete JS file so it isn't that.
Any ideas?
Hi Alex,
I have a couple of questions.
It doesn't appear that you are passing the search criteria argument into your NavbarSearchResult() object.
There is no [JsonOnlyConfiguration] attribute to tell the api controller to return Json. By default without any attribute you will get back XML.
You can use Chrome Dev inspector and look at Network Tab (XHR) to see the ajax request and response to confirm the data being sent and received.
I would replace the search method first with a call to return a Json object just to make sure the plumbing is working before you get into passing search criteria.
e.g.
With regards to hitting the controller you should be able to set a breakpoint in Visual Studio. If it doesn't get hit then the url path is wrong, you might need a tilda
~
at the start e.g~/umbraco/api/...
Dave
is working on a reply...