I am new to Select2 and am having trouble integrating AJAX. When I search, the results aren\'t being filtered based on the query.
Here\'s how it looks: http://i.imgu
You will need to write a custom filter on server side to filter the results. What you type in the box is saved in 'term' and then 'q' is sent as a request parameter with the ajax call. So the ajax call to
url:"/concepts/names_for_search?q=deri"
should only return the filtered results and not all the results.
Update
Select2 will make an AJAX call every time you type in the search box. You have to filter the results on server side and then return the results based on the text in search box.
I am using it in my JSP/Servlet application as below
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String searchTerm = request.getParameter("q");
String json = getOptions(searchTerm);
//getOptions method will return a string of in JSON format
//[{"id":"1","name":"Derivatives"}]
response.getWriter().write(json);
}
Your JavaScript code is correct.
I found Select2 is used here. If you open the link http://www.indiewebseries.com/search?q=ind and http://www.indiewebseries.com/search?q=in the results obtained are different and based on the 'q' parameter.
While in you case, the results for calls to url '/concepts/names_for_search?q=d' and '/concepts/names_for_search?q=deri' are the same(i.e. not filtered)