I am using select2
with custom data adapter. All of the data provided to select2
is generated locally in web page (so no need to use ajax). As query
method can generate a lot of results (about 5k) opening select box is quite slow.
As a remedy, I wanted to use infinite scroll. Documentation for custom data adapter says that query
method should receive page
parameter together with term
:
@param params.page The specific page that should be loaded. This is typically provided when working with remote data sets, which rely on pagination to determine what objects should be displayed.
But it does not: only term
is present. I tried to return more: true
or more: 1000
, but this didn't help. I guess this is because, by default, infinite scroll is enabled iff ajax is enabled.
I am guessing that enabling infinite scroll will involve using amd.require
, but I am not sure what to do exactly. I tried this code:
$.fn.select2.amd.require(
["select2/utils", "select2/dropdown/infiniteScroll"],
(Utils, InfiniteScroll) =>
input.data("select2").options.options.resultsAdapter =
Utils.Decorate(input.data("select2").options.options.resultsAdapter, InfiniteScroll)
)
This is coffee script, but I hope that it is readable for everyone. input
is DOM
element containing select box - I earlier did input.select2( //options )
My question is basically, how do I enable infinite scroll without ajax
?
Create a project structure First, create a new folder called infinite-scroll . Inside that folder, create two subfolders css and js . Second, create the style. css in the css folder and app.
Infinite scrolling is a technique that allows users to scroll through a massive chunk of content with no finishing-line in sight. This technique simply keeps refreshing a page when you scroll down it. No matter how good it sounds, the technique isn't a one-size-fits-all solution for every site or app.
Infinite scrolling is best for a “browsing” mindset They do this for two key reasons: it boosts user engagement and is better for mobile devices. Users don't have to click buttons to load up the next batch of results: they can scroll, which is an easy action for smartphones.
Infinite Ajax Scroll is a javascript infinite scrolling plugin. It works by reading the next (and previous) links of your existing server-side pagination and load these pages via AJAX when the visitor scrolls to the end of the page.
My solution for angular 9
this.$select2 = this.$element.select2({
width: '100%',
language: "tr",
ajax: {
transport: (params, success, failure) => {
let pageSize = 10;
let page = (params.data.page || 1);
let results = this.options
.filter(i => new RegExp(params.data.term, "i").test(i.text))
.map(i => {
return {
id: i.value,
text: i.text
}
});
let paged = results.slice((page - 1) * pageSize, page * pageSize);
let options = {
results: paged,
pagination: {
more: results.length >= page * pageSize
}
};
success(options);
}
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With