Select2 Ajax not filtering results based on query

前端 未结 2 637
执念已碎
执念已碎 2020-12-17 18:40

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

相关标签:
2条回答
  • 2020-12-17 19:08

    This question was asked on the github project and the answer was: filter on the server side. The default filter function called when AJAX is not used is present in the Select2 documentation on the matcher parameter:

    function(term, text) { return text.toUpperCase().indexOf(term.toUpperCase())>=0; }
    
    0 讨论(0)
  • 2020-12-17 19:24

    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)

    0 讨论(0)
提交回复
热议问题