HI I am having an issue with paging, cant seem to get it working with 4.0.0, the same way as for 3.5.2. Right now I have the following code but the page param is missing don´t know from where to get it. Also there are no examples on the page, any suggestions?
$(document).ready(function() {
$(".js-example-basic-single").select2();
// It requires this type of function in order to make the correct callback
$.fn.select2.amd.require(
['select2/data/array', 'select2/utils'],
function (ArrayData, Utils) {
function CustomData ($element, options) {
CustomData.__super__.constructor.call(this, $element, options);
}
// need to extend the utils functionallity in order to pas our customData function
Utils.Extend(CustomData, ArrayData);
// CustomData.prototype.current = function (callback) {
// var data = [];
// var currentVal = this.$element.val();
// for (var v = 0; v < currentVal.length; v++) {
// data.push({
// id: currentVal[v],
// text: currentVal[v]
// });
// }
// callback(data);
// };
CustomData.prototype.query = function (params, callback) {
// var data = { results: []};
// data.results = this.options.options.data;
// var filtered = _.filter(data.results, function(dati){
// if(dati.text.indexOf(params.term)>-1)
// {
// return dati;
// }
// });
// data = {results: filtered};
// callback(data);
var pageSize,
results;
pageSize = 250; // or whatever pagesize
results = [];
if (params.term && params.term !== "") {
results = _.filter(this.options.options.data, function (e) {
return (e.text.toUpperCase().indexOf(params.term.toUpperCase()) >= 0);
});
}
else if (params.term === "") {
results = this.options.options.data;
}
callback({
results: results.slice((
this.page - 1) * pageSize,
this.page * pageSize),
more : results.length >=
this.page * pageSize
});
};
// Shuffles a string to create different data
var shuffle = function (str) {
return str.split('').sort(function () {
return 0.5 - Math.random();
}).join('');
};
// creates 201 ddl items
var mockData = function () {
var array = _.map(_.range(1, 2000), function (i) {
return {
id : i,
text: shuffle('a')+ i
};
});
return array;
};
// create demo data
var dummyData = mockData();
$("#JobPosition").select2({
data: dummyData,
dataAdapter: CustomData, params: {this}
});
});
});