Created
October 13, 2017 14:44
-
-
Save kelvearagao/f9e5f0107ca77457e00cc1aa785b2ec2 to your computer and use it in GitHub Desktop.
Select2: Infinite Scroll with Local Array Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's working with select2 version 3.5.1