Skip to content

Instantly share code, notes, and snippets.

@kelvearagao
Created October 13, 2017 14:44
Show Gist options
  • Save kelvearagao/f9e5f0107ca77457e00cc1aa785b2ec2 to your computer and use it in GitHub Desktop.
Save kelvearagao/f9e5f0107ca77457e00cc1aa785b2ec2 to your computer and use it in GitHub Desktop.
Select2: Infinite Scroll with Local Array Data
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="select2.css" rel="stylesheet">
</head>
<body>
<div>
<input type="text" id="select-teste" style="width: 300px;"/>
</div>
<script src="jquery.js"></script>
<script src="select2.js"></script>
<script>
var HOW_MANY = 9002;
var testdata = [];
for (var i = 0; i < HOW_MANY; i++){
testdata.push({
id: i,
text: "Item #" + i
})
}
function contains(str1, str2) {
return new RegExp(str2, "i").test(str1);
}
$("#select-teste").select2({
data: testdata,
query: function(q) {
var pageSize = 10,
results = this.data.filter(function(e) {
return contains(e.text, q.term);
});
// Get a page sized slice of data from the results of filtering the data set.
var paged = results.slice((q.page - 1) * pageSize, q.page * pageSize);
q.callback({
results: paged,
more: results.length >= q.page * pageSize
});
}
});
// https://stackoverflow.com/questions/32756698/how-to-enable-infinite-scrolling-in-select2-4-0-without-ajax
</script>
</body>
</html>
@kelvearagao
Copy link
Author

kelvearagao commented Oct 13, 2017

It's working with select2 version 3.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment