Reputation: 78
I am using selct2 and I am loading some data with ajax. The result is not limited, but I have to make infinite scroll. Some ideas?
I have a json like this
{"len":30,"data":[{"value":"223118","type":1,"name":"Peter","language":3},
{"value":"223118","type":1,"name":"John","language":2},
{"value":"223118","type":2,"name":"Mike","language":1},
{"value":"223118","type":1,"name":"George","language":3}
....
]}
And I am using the standart select2 functionality for ajac loading:
$( selector ).select2({
placeholder: "Search",
minimumInputLength: 1,
id: function(bond){return {id: bond._id};},
ajax: {
//url: $("#area-of-operating_0").attr('data-url'),
url: 'myurl',
dataType: 'json',
quietMillis: 100,
data: function (term, page) {
return {
page_limit: 10, // page size
page: page // page number
//q: term
};
},
results: function (data, page) {
var more = (page * 10) < data.total;
return {results: data.data, more: more}
}
},
formatResult: selectFormatResult, // see example
formatSelection: selectFormatSelection
})
The problem is that the json can be with 1000 elements. I want to make infinite scroll with 10 elements
Upvotes: 3
Views: 4522
Reputation: 4841
This is not possible using just select2
as the mentioned in the demo
In order to enable the remote service must support some sort of a paging mechanism...
it is not possible to enable infinite scrolling if the server side does not support paging.
What you could do is to save the whole JSON in some JS-variable at the first request. Then you can sequentially get some more results out of the local variable when the user is scrolling down. Is this an option for you?
Upvotes: 1