javascript - Select2 load JSON resultset via AJAX and search locally - Stack Overflow

Until now I've been using Select2's normal AJAX method of searching and filtering data server

Until now I've been using Select2's normal AJAX method of searching and filtering data serverside, but now I have a usecase where I want to retrieve all the results via AJAX when the select is opened and then use those results (now stored locally) to search and filter.

I've trawled the web looking for examples of how to do this and all i've found is lots of people saying the Query method should be used rather than the AJAX helper. Unfortunately I haven't found any examples.

So far all I've managed is the basic:

$('#parent').select2({
  placeholder: "Select Parent",
  minimumInputLength: 0,
  allowClear: true,
  query: function (query) {
      //console.log(query);
      query.callback(data);
  }
});

data = {
 more: false,
 results: [
    { id: "CA", text: "California" },
    { id: "AL", text: "Alabama" }
 ]
}

Data is being passed to the select but query filtering is not implemented.

I'm struggling to understand the select2 documentation and would appreciate any assistance or links to examples.

Until now I've been using Select2's normal AJAX method of searching and filtering data serverside, but now I have a usecase where I want to retrieve all the results via AJAX when the select is opened and then use those results (now stored locally) to search and filter.

I've trawled the web looking for examples of how to do this and all i've found is lots of people saying the Query method should be used rather than the AJAX helper. Unfortunately I haven't found any examples.

So far all I've managed is the basic:

$('#parent').select2({
  placeholder: "Select Parent",
  minimumInputLength: 0,
  allowClear: true,
  query: function (query) {
      //console.log(query);
      query.callback(data);
  }
});

data = {
 more: false,
 results: [
    { id: "CA", text: "California" },
    { id: "AL", text: "Alabama" }
 ]
}

Data is being passed to the select but query filtering is not implemented.

I'm struggling to understand the select2 documentation and would appreciate any assistance or links to examples.

Share Improve this question edited Aug 17, 2014 at 0:47 Tim asked Aug 16, 2014 at 7:44 TimTim 3,1319 gold badges50 silver badges65 bronze badges 2
  • You'll have to implement the filter server-side as explained here: stackoverflow./questions/15232476/… – Kristian Hebert Commented Aug 16, 2014 at 7:50
  • Hi Kristian, Thanks for your ment. I already have server filtering working with select2. Unfortunately for this particular use case server filtering is not an option and that is why I am pursuing client filtering. I know it can be done. – Tim Commented Aug 16, 2014 at 12:12
Add a ment  | 

1 Answer 1

Reset to default 5

Try the following - pre-load json data into local variable and when ready bind this to select2 object

<script>
function format(item) { return item.text; }
var jresults;
$(document).ready(function() {
    $.getJSON("http://yoururl./api/select_something.json").done(
        function( data ) {
            $.jresults = data;
            $("#parent").select2(
                {formatResult: format,
                 formatSelection: format,
                 data: $.jresults }
            );
        }
    )
});
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745438476a4627720.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信