简体   繁体   English

Select2 - Ajax搜索 - 记住最后的结果

[英]Select2 - Ajax search - remember last results

I am using Select2 3.5.1. 我正在使用Select2 3.5.1。 With this plugin I can successfully load remote data. 有了这个插件,我可以成功加载远程数据。 However I am here today to ask a question to improve this search. 但是我今天在这里提出一个问题来改进这种搜索。 Here is the step-by-step to understand what I would like to do: 以下是逐步了解我想要做的事情:

  1. Setup a Select2 with remote data loading (using ajax). 设置带有远程数据加载的Select2(使用ajax)。
  2. Click on the Select2 input and search for something. 单击Select2输入并搜索某些内容。
  3. The loading will appear and after some seconds you will see a list of results. 将显示加载,几秒钟后您将看到结果列表。
  4. Click on one of the results listed - the box of results will then disappear. 单击列出的结果之一 - 结果框将消失。
  5. If you click again on the search box, the list will be empty and you will need to type some new text again to have a list of results. 如果再次单击搜索框,列表将为空,您需要再次键入一些新文本以获得结果列表。

Is it possible that when we click again on the search box, that the list of results previously searched re-appear without any ajax call? 是否有可能当我们再次点击搜索框时,先前搜索过的结果列表重新出现而没有任何ajax调用? Then if the user delete a character or change his search criteria it would then trigger the ajax search again. 然后,如果用户删除字符或更改其搜索条件,则它将再次触发ajax搜索。

If it is possible, how would we code that? 如果有可能,我们将如何编码呢?

I hope my question is clear, please let me know if you have any questions. 我希望我的问题很清楚,如果您有任何问题,请告诉我。 Thank you. 谢谢。

Here is a very simple code where we do a search that return a list of results. 这是一个非常简单的代码,我们进行搜索,返回结果列表。 It doesn't really search, but it does return a list when you type something. 它并不真正搜索,但它会在您输入内容时返回一个列表。 I am not sure how to use the initSelection that is mentionned in one of the response. 我不知道如何使用其中一个响应中提到的initSelection。

<html>
<head>
    <title>
        Test page for ajax cache
    </title>
    <script type='text/javascript' src='../../resources/javascript/jquery/jquery-1.9.1.min.js'></script>
    <link type='text/css' href='../../resources/javascript/select2/select2.css' rel='stylesheet' />
    <script type='text/javascript' src='../../resources/javascript/select2/select2.js'></script>

    <script>
    $(document).ready(function(){
        $('#select').select2({
            ajax: {
                type: 'POST',
                url: 'ajax.php',
                dataType: 'json',
                data: function(term, page){
                    return {
                        autoc: 'country',
                        term: term
                    }
                },
                results: function(data, page){
                    console.log(data);

                    return( {results: data.results} );
                }
            },
            placeholder: 'Search something',
            minimumInputLength: 3,
            width: '333'
        });
    });
    </script>
</head>

<body>
    <input type='text' name='inputdata' id='select' />
</body>
</html>

The very simple ajax.php called: 非常简单的ajax.php叫:

<?php
$results2['more'] = false;
$results2['results'][0] = array('id'=> "1", 'text'=> "California");
$results2['results'][1] = array('id'=> "2", 'text'=> "Canada");
$results2['results'][2] = array('id'=> "2", 'text'=> "Someword");
$results2['results'][3] = array('id'=> "2", 'text'=> "Alberta");
$results2['results'][4] = array('id'=> "2", 'text'=> "New York");

echo json_encode($results2);

I did read your post once again. 我再次阅读了你的帖子。 I misunderstood you last time. 我上次误解了你。

The solution is here. 解决方案就在这里。

   $(document).ready(function () {
        $('#select').select2({
            // this part is responsible for data caching
            dataCache: [],
            query: function (q) {
                var obj = this,
                        key = q.term,
                        dataCache = obj.dataCache[key];

                //checking is result in cache
                if (dataCache) {
                    q.callback({results: dataCache.results});
                } else {
                    $.ajax({
                        url: 'ajax.php',
                        data: {q: q.term},
                        dataType: 'json',
                        type: 'POST',
                        success: function (data) {
                            //copy data to 'cache'
                            obj.dataCache[key] = data;
                            q.callback({results: data.results});
                        }
                    })
                }
            },
            placeholder: 'Search something',
            width: '333',
            minimumInputLength: 3,
        });
        // this part is responsible for setting last search when select2 is opening
        var last_search = '';
        $('#select').on('select2-open', function () {
            if (last_search) {
                $('.select2-search').find('input').val(last_search).trigger('paste');
            }
        });
        $('#select').on('select2-loaded', function () {
            last_search = $('.select2-search').find('input').val();
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM