Select2 Ajax不基于查询过滤结果 [英] Select2 Ajax not filtering results based on query

查看:79
本文介绍了Select2 Ajax不基于查询过滤结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Select2的新手,无法集成AJAX.当我搜索时,结果不会根据查询进行过滤.

I am new to Select2 and am having trouble integrating AJAX. When I search, the results aren't being filtered based on the query.

外观如下: http://i.imgur.com/dAPSSDH.png -结果中带下划线的是正确的字符,但没有过滤掉任何内容.在我的非ajax Select2和示例中,过滤似乎是自动进行的,所以我犹豫是否要编写自定义过滤器,因为可能已经内置了一个更好的过滤器.

Here's how it looks: http://i.imgur.com/dAPSSDH.png - The right characters are underlined in the results, but nothing is filtered out. In my non-ajax Select2 and in the examples I've seen, the filtering seems to happen somewhat automatically, so I am hesitant to write a custom filter as there is probably a better one built in already.

这是我的代码:

<script>
  $("#search_bar").select2({
    placeholder: "Search for another Concept",
    minimumInputLength: 1,
    ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
      url: "/concepts/names_for_search",
      dataType: 'json',
      data: function (term, page) {
        return {
        q: term, // search term
        page: page
         };
      },
      results: function (data, page) {
        return { results: data};
      }
    },
  });
</script>

此外,这是我的JSON的示例:

Also, here is an example of my JSON:

[{"id":1,"text":"Limits"},{"id":2,"text":"Derivatives"},{"id":3,"text":"One-Sided Limits"},{"id":4,"text":"Formal Definition of a limit"}]

有什么想法吗?希望我只是在做一些愚蠢的事情,这是一个快速的解决方法.预先感谢您的帮助.

Any ideas? Hopefully I am just doing something stupid and it is a quick fix. Thanks in advance for any help.

推荐答案

您将需要在服务器端编写一个自定义过滤器以过滤结果.您在框中输入的内容将保存在'term'中,然后使用ajax调用将'q'作为请求参数发送.因此,对
的ajax调用 url:"/concepts/names_for_search?q = deri"
应该只返回过滤后的结果,而不是所有结果.

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.

更新
每次您在搜索框中键入时,Select2都会进行AJAX调用.您必须在服务器端过滤结果,然后根据搜索框中的文本返回结果.
我在我的JSP/Servlet应用程序中使用它,如下所示

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);
}

您的JavaScript代码正确.

Your JavaScript code is correct.

我发现此处使用了Select2.如果您打开链接 http://www.indiewebseries.com/search?q=ind http://www.indiewebseries.com/search?q=in 结果获得的结果是不同的,并且基于'q'参数.
在这种情况下,对url'/concepts/names_for_search?q = d'和'/concepts/names_for_search?q = deri'的调用结果相同(即未过滤)

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)

这篇关于Select2 Ajax不基于查询过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆