Added pagination support for AJAX results
I'm still not quite sure how this should be handled for the general case, but for the special case we have this for infinite scrolling, only on AJAX data.
This commit is contained in:
parent
f1e86470ca
commit
09e3a76bf9
22
dist/js/select2.amd.full.js
vendored
22
dist/js/select2.amd.full.js
vendored
@ -2494,7 +2494,7 @@ define('select2/data/ajax',[
|
||||
var $request = $.ajax(options);
|
||||
|
||||
$request.success(function (data) {
|
||||
var results = self.processResults(data);
|
||||
var results = self.processResults(data, params);
|
||||
|
||||
callback(results);
|
||||
});
|
||||
@ -2860,14 +2860,18 @@ define('select2/dropdown/infiniteScroll',[
|
||||
|
||||
InfiniteScroll.prototype.append = function (decorated, data) {
|
||||
this.$loadingMore.remove();
|
||||
this.loading = false;
|
||||
|
||||
if ($.isArray(data)) {
|
||||
decorated.call(this, data);
|
||||
|
||||
if (data.length > 0) {
|
||||
this.$results.append(this.$loadingMore);
|
||||
return;
|
||||
}
|
||||
|
||||
this.loading = false;
|
||||
decorated.call(this, data.results);
|
||||
|
||||
if (this.showLoadingMore(data)) {
|
||||
this.$results.append(this.$loadingMore);
|
||||
}
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.bind = function (decorated, container, $container) {
|
||||
@ -2886,12 +2890,12 @@ define('select2/dropdown/infiniteScroll',[
|
||||
});
|
||||
|
||||
this.$results.on('scroll', function () {
|
||||
var loadMoreVisible = $.contains(
|
||||
var isLoadMoreVisible = $.contains(
|
||||
document.documentElement,
|
||||
self.$loadingMore[0]
|
||||
);
|
||||
|
||||
if (self.loading || !loadMoreVisible) {
|
||||
if (self.loading || !isLoadMoreVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -2916,6 +2920,10 @@ define('select2/dropdown/infiniteScroll',[
|
||||
this.trigger('query:append', params);
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.showLoadingMore = function (_, data) {
|
||||
return data.pagination && data.pagination.more;
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.createLoadingMore = function () {
|
||||
var $option = $(
|
||||
'<li class="option load-more" role="treeitem"></li>'
|
||||
|
22
dist/js/select2.amd.js
vendored
22
dist/js/select2.amd.js
vendored
@ -2494,7 +2494,7 @@ define('select2/data/ajax',[
|
||||
var $request = $.ajax(options);
|
||||
|
||||
$request.success(function (data) {
|
||||
var results = self.processResults(data);
|
||||
var results = self.processResults(data, params);
|
||||
|
||||
callback(results);
|
||||
});
|
||||
@ -2860,14 +2860,18 @@ define('select2/dropdown/infiniteScroll',[
|
||||
|
||||
InfiniteScroll.prototype.append = function (decorated, data) {
|
||||
this.$loadingMore.remove();
|
||||
this.loading = false;
|
||||
|
||||
if ($.isArray(data)) {
|
||||
decorated.call(this, data);
|
||||
|
||||
if (data.length > 0) {
|
||||
this.$results.append(this.$loadingMore);
|
||||
return;
|
||||
}
|
||||
|
||||
this.loading = false;
|
||||
decorated.call(this, data.results);
|
||||
|
||||
if (this.showLoadingMore(data)) {
|
||||
this.$results.append(this.$loadingMore);
|
||||
}
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.bind = function (decorated, container, $container) {
|
||||
@ -2886,12 +2890,12 @@ define('select2/dropdown/infiniteScroll',[
|
||||
});
|
||||
|
||||
this.$results.on('scroll', function () {
|
||||
var loadMoreVisible = $.contains(
|
||||
var isLoadMoreVisible = $.contains(
|
||||
document.documentElement,
|
||||
self.$loadingMore[0]
|
||||
);
|
||||
|
||||
if (self.loading || !loadMoreVisible) {
|
||||
if (self.loading || !isLoadMoreVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -2916,6 +2920,10 @@ define('select2/dropdown/infiniteScroll',[
|
||||
this.trigger('query:append', params);
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.showLoadingMore = function (_, data) {
|
||||
return data.pagination && data.pagination.more;
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.createLoadingMore = function () {
|
||||
var $option = $(
|
||||
'<li class="option load-more" role="treeitem"></li>'
|
||||
|
22
dist/js/select2.full.js
vendored
22
dist/js/select2.full.js
vendored
@ -12029,7 +12029,7 @@ define('select2/data/ajax',[
|
||||
var $request = $.ajax(options);
|
||||
|
||||
$request.success(function (data) {
|
||||
var results = self.processResults(data);
|
||||
var results = self.processResults(data, params);
|
||||
|
||||
callback(results);
|
||||
});
|
||||
@ -12395,14 +12395,18 @@ define('select2/dropdown/infiniteScroll',[
|
||||
|
||||
InfiniteScroll.prototype.append = function (decorated, data) {
|
||||
this.$loadingMore.remove();
|
||||
this.loading = false;
|
||||
|
||||
if ($.isArray(data)) {
|
||||
decorated.call(this, data);
|
||||
|
||||
if (data.length > 0) {
|
||||
this.$results.append(this.$loadingMore);
|
||||
return;
|
||||
}
|
||||
|
||||
this.loading = false;
|
||||
decorated.call(this, data.results);
|
||||
|
||||
if (this.showLoadingMore(data)) {
|
||||
this.$results.append(this.$loadingMore);
|
||||
}
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.bind = function (decorated, container, $container) {
|
||||
@ -12421,12 +12425,12 @@ define('select2/dropdown/infiniteScroll',[
|
||||
});
|
||||
|
||||
this.$results.on('scroll', function () {
|
||||
var loadMoreVisible = $.contains(
|
||||
var isLoadMoreVisible = $.contains(
|
||||
document.documentElement,
|
||||
self.$loadingMore[0]
|
||||
);
|
||||
|
||||
if (self.loading || !loadMoreVisible) {
|
||||
if (self.loading || !isLoadMoreVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -12451,6 +12455,10 @@ define('select2/dropdown/infiniteScroll',[
|
||||
this.trigger('query:append', params);
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.showLoadingMore = function (_, data) {
|
||||
return data.pagination && data.pagination.more;
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.createLoadingMore = function () {
|
||||
var $option = $(
|
||||
'<li class="option load-more" role="treeitem"></li>'
|
||||
|
4
dist/js/select2.full.min.js
vendored
4
dist/js/select2.full.min.js
vendored
File diff suppressed because one or more lines are too long
22
dist/js/select2.js
vendored
22
dist/js/select2.js
vendored
@ -2922,7 +2922,7 @@ define('select2/data/ajax',[
|
||||
var $request = $.ajax(options);
|
||||
|
||||
$request.success(function (data) {
|
||||
var results = self.processResults(data);
|
||||
var results = self.processResults(data, params);
|
||||
|
||||
callback(results);
|
||||
});
|
||||
@ -3288,14 +3288,18 @@ define('select2/dropdown/infiniteScroll',[
|
||||
|
||||
InfiniteScroll.prototype.append = function (decorated, data) {
|
||||
this.$loadingMore.remove();
|
||||
this.loading = false;
|
||||
|
||||
if ($.isArray(data)) {
|
||||
decorated.call(this, data);
|
||||
|
||||
if (data.length > 0) {
|
||||
this.$results.append(this.$loadingMore);
|
||||
return;
|
||||
}
|
||||
|
||||
this.loading = false;
|
||||
decorated.call(this, data.results);
|
||||
|
||||
if (this.showLoadingMore(data)) {
|
||||
this.$results.append(this.$loadingMore);
|
||||
}
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.bind = function (decorated, container, $container) {
|
||||
@ -3314,12 +3318,12 @@ define('select2/dropdown/infiniteScroll',[
|
||||
});
|
||||
|
||||
this.$results.on('scroll', function () {
|
||||
var loadMoreVisible = $.contains(
|
||||
var isLoadMoreVisible = $.contains(
|
||||
document.documentElement,
|
||||
self.$loadingMore[0]
|
||||
);
|
||||
|
||||
if (self.loading || !loadMoreVisible) {
|
||||
if (self.loading || !isLoadMoreVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -3344,6 +3348,10 @@ define('select2/dropdown/infiniteScroll',[
|
||||
this.trigger('query:append', params);
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.showLoadingMore = function (_, data) {
|
||||
return data.pagination && data.pagination.more;
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.createLoadingMore = function () {
|
||||
var $option = $(
|
||||
'<li class="option load-more" role="treeitem"></li>'
|
||||
|
4
dist/js/select2.min.js
vendored
4
dist/js/select2.min.js
vendored
File diff suppressed because one or more lines are too long
@ -745,11 +745,19 @@ $.fn.select2.amd.require(
|
||||
page: params.page
|
||||
};
|
||||
},
|
||||
processResults: function (data, page) {
|
||||
// parse the results into the format expected by Select2.
|
||||
processResults: function (data, params) {
|
||||
// parse the results into the format expected by Select2
|
||||
// since we are using custom formatting functions we do not need to
|
||||
// alter the remote JSON data
|
||||
return data.items;
|
||||
// alter the remote JSON data, except to indicate that infinite
|
||||
// scrolling can be used
|
||||
params.page = params.page || 1;
|
||||
|
||||
return {
|
||||
results: data.items,
|
||||
pagination: {
|
||||
more: (params.page * 30) < data.total_count
|
||||
}
|
||||
};
|
||||
},
|
||||
cache: true
|
||||
},
|
||||
|
2
src/js/select2/data/ajax.js
vendored
2
src/js/select2/data/ajax.js
vendored
@ -44,7 +44,7 @@ define([
|
||||
var $request = $.ajax(options);
|
||||
|
||||
$request.success(function (data) {
|
||||
var results = self.processResults(data);
|
||||
var results = self.processResults(data, params);
|
||||
|
||||
callback(results);
|
||||
});
|
||||
|
20
src/js/select2/dropdown/infiniteScroll.js
vendored
20
src/js/select2/dropdown/infiniteScroll.js
vendored
@ -12,14 +12,18 @@ define([
|
||||
|
||||
InfiniteScroll.prototype.append = function (decorated, data) {
|
||||
this.$loadingMore.remove();
|
||||
this.loading = false;
|
||||
|
||||
if ($.isArray(data)) {
|
||||
decorated.call(this, data);
|
||||
|
||||
if (data.length > 0) {
|
||||
this.$results.append(this.$loadingMore);
|
||||
return;
|
||||
}
|
||||
|
||||
this.loading = false;
|
||||
decorated.call(this, data.results);
|
||||
|
||||
if (this.showLoadingMore(data)) {
|
||||
this.$results.append(this.$loadingMore);
|
||||
}
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.bind = function (decorated, container, $container) {
|
||||
@ -38,12 +42,12 @@ define([
|
||||
});
|
||||
|
||||
this.$results.on('scroll', function () {
|
||||
var loadMoreVisible = $.contains(
|
||||
var isLoadMoreVisible = $.contains(
|
||||
document.documentElement,
|
||||
self.$loadingMore[0]
|
||||
);
|
||||
|
||||
if (self.loading || !loadMoreVisible) {
|
||||
if (self.loading || !isLoadMoreVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -68,6 +72,10 @@ define([
|
||||
this.trigger('query:append', params);
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.showLoadingMore = function (_, data) {
|
||||
return data.pagination && data.pagination.more;
|
||||
};
|
||||
|
||||
InfiniteScroll.prototype.createLoadingMore = function () {
|
||||
var $option = $(
|
||||
'<li class="option load-more" role="treeitem"></li>'
|
||||
|
Loading…
Reference in New Issue
Block a user