1
0
mirror of synced 2024-11-29 08:06:03 +03:00

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:
Kevin Brown 2015-01-02 19:58:09 -05:00
parent f1e86470ca
commit 09e3a76bf9
9 changed files with 96 additions and 48 deletions

View File

@ -2494,7 +2494,7 @@ define('select2/data/ajax',[
var $request = $.ajax(options); var $request = $.ajax(options);
$request.success(function (data) { $request.success(function (data) {
var results = self.processResults(data); var results = self.processResults(data, params);
callback(results); callback(results);
}); });
@ -2860,14 +2860,18 @@ define('select2/dropdown/infiniteScroll',[
InfiniteScroll.prototype.append = function (decorated, data) { InfiniteScroll.prototype.append = function (decorated, data) {
this.$loadingMore.remove(); this.$loadingMore.remove();
this.loading = false;
decorated.call(this, data); if ($.isArray(data)) {
decorated.call(this, data);
if (data.length > 0) { return;
this.$results.append(this.$loadingMore);
} }
this.loading = false; decorated.call(this, data.results);
if (this.showLoadingMore(data)) {
this.$results.append(this.$loadingMore);
}
}; };
InfiniteScroll.prototype.bind = function (decorated, container, $container) { InfiniteScroll.prototype.bind = function (decorated, container, $container) {
@ -2886,12 +2890,12 @@ define('select2/dropdown/infiniteScroll',[
}); });
this.$results.on('scroll', function () { this.$results.on('scroll', function () {
var loadMoreVisible = $.contains( var isLoadMoreVisible = $.contains(
document.documentElement, document.documentElement,
self.$loadingMore[0] self.$loadingMore[0]
); );
if (self.loading || !loadMoreVisible) { if (self.loading || !isLoadMoreVisible) {
return; return;
} }
@ -2916,6 +2920,10 @@ define('select2/dropdown/infiniteScroll',[
this.trigger('query:append', params); this.trigger('query:append', params);
}; };
InfiniteScroll.prototype.showLoadingMore = function (_, data) {
return data.pagination && data.pagination.more;
};
InfiniteScroll.prototype.createLoadingMore = function () { InfiniteScroll.prototype.createLoadingMore = function () {
var $option = $( var $option = $(
'<li class="option load-more" role="treeitem"></li>' '<li class="option load-more" role="treeitem"></li>'

View File

@ -2494,7 +2494,7 @@ define('select2/data/ajax',[
var $request = $.ajax(options); var $request = $.ajax(options);
$request.success(function (data) { $request.success(function (data) {
var results = self.processResults(data); var results = self.processResults(data, params);
callback(results); callback(results);
}); });
@ -2860,14 +2860,18 @@ define('select2/dropdown/infiniteScroll',[
InfiniteScroll.prototype.append = function (decorated, data) { InfiniteScroll.prototype.append = function (decorated, data) {
this.$loadingMore.remove(); this.$loadingMore.remove();
this.loading = false;
decorated.call(this, data); if ($.isArray(data)) {
decorated.call(this, data);
if (data.length > 0) { return;
this.$results.append(this.$loadingMore);
} }
this.loading = false; decorated.call(this, data.results);
if (this.showLoadingMore(data)) {
this.$results.append(this.$loadingMore);
}
}; };
InfiniteScroll.prototype.bind = function (decorated, container, $container) { InfiniteScroll.prototype.bind = function (decorated, container, $container) {
@ -2886,12 +2890,12 @@ define('select2/dropdown/infiniteScroll',[
}); });
this.$results.on('scroll', function () { this.$results.on('scroll', function () {
var loadMoreVisible = $.contains( var isLoadMoreVisible = $.contains(
document.documentElement, document.documentElement,
self.$loadingMore[0] self.$loadingMore[0]
); );
if (self.loading || !loadMoreVisible) { if (self.loading || !isLoadMoreVisible) {
return; return;
} }
@ -2916,6 +2920,10 @@ define('select2/dropdown/infiniteScroll',[
this.trigger('query:append', params); this.trigger('query:append', params);
}; };
InfiniteScroll.prototype.showLoadingMore = function (_, data) {
return data.pagination && data.pagination.more;
};
InfiniteScroll.prototype.createLoadingMore = function () { InfiniteScroll.prototype.createLoadingMore = function () {
var $option = $( var $option = $(
'<li class="option load-more" role="treeitem"></li>' '<li class="option load-more" role="treeitem"></li>'

View File

@ -12029,7 +12029,7 @@ define('select2/data/ajax',[
var $request = $.ajax(options); var $request = $.ajax(options);
$request.success(function (data) { $request.success(function (data) {
var results = self.processResults(data); var results = self.processResults(data, params);
callback(results); callback(results);
}); });
@ -12395,14 +12395,18 @@ define('select2/dropdown/infiniteScroll',[
InfiniteScroll.prototype.append = function (decorated, data) { InfiniteScroll.prototype.append = function (decorated, data) {
this.$loadingMore.remove(); this.$loadingMore.remove();
this.loading = false;
decorated.call(this, data); if ($.isArray(data)) {
decorated.call(this, data);
if (data.length > 0) { return;
this.$results.append(this.$loadingMore);
} }
this.loading = false; decorated.call(this, data.results);
if (this.showLoadingMore(data)) {
this.$results.append(this.$loadingMore);
}
}; };
InfiniteScroll.prototype.bind = function (decorated, container, $container) { InfiniteScroll.prototype.bind = function (decorated, container, $container) {
@ -12421,12 +12425,12 @@ define('select2/dropdown/infiniteScroll',[
}); });
this.$results.on('scroll', function () { this.$results.on('scroll', function () {
var loadMoreVisible = $.contains( var isLoadMoreVisible = $.contains(
document.documentElement, document.documentElement,
self.$loadingMore[0] self.$loadingMore[0]
); );
if (self.loading || !loadMoreVisible) { if (self.loading || !isLoadMoreVisible) {
return; return;
} }
@ -12451,6 +12455,10 @@ define('select2/dropdown/infiniteScroll',[
this.trigger('query:append', params); this.trigger('query:append', params);
}; };
InfiniteScroll.prototype.showLoadingMore = function (_, data) {
return data.pagination && data.pagination.more;
};
InfiniteScroll.prototype.createLoadingMore = function () { InfiniteScroll.prototype.createLoadingMore = function () {
var $option = $( var $option = $(
'<li class="option load-more" role="treeitem"></li>' '<li class="option load-more" role="treeitem"></li>'

File diff suppressed because one or more lines are too long

24
dist/js/select2.js vendored
View File

@ -2922,7 +2922,7 @@ define('select2/data/ajax',[
var $request = $.ajax(options); var $request = $.ajax(options);
$request.success(function (data) { $request.success(function (data) {
var results = self.processResults(data); var results = self.processResults(data, params);
callback(results); callback(results);
}); });
@ -3288,14 +3288,18 @@ define('select2/dropdown/infiniteScroll',[
InfiniteScroll.prototype.append = function (decorated, data) { InfiniteScroll.prototype.append = function (decorated, data) {
this.$loadingMore.remove(); this.$loadingMore.remove();
this.loading = false;
decorated.call(this, data); if ($.isArray(data)) {
decorated.call(this, data);
if (data.length > 0) { return;
this.$results.append(this.$loadingMore);
} }
this.loading = false; decorated.call(this, data.results);
if (this.showLoadingMore(data)) {
this.$results.append(this.$loadingMore);
}
}; };
InfiniteScroll.prototype.bind = function (decorated, container, $container) { InfiniteScroll.prototype.bind = function (decorated, container, $container) {
@ -3314,12 +3318,12 @@ define('select2/dropdown/infiniteScroll',[
}); });
this.$results.on('scroll', function () { this.$results.on('scroll', function () {
var loadMoreVisible = $.contains( var isLoadMoreVisible = $.contains(
document.documentElement, document.documentElement,
self.$loadingMore[0] self.$loadingMore[0]
); );
if (self.loading || !loadMoreVisible) { if (self.loading || !isLoadMoreVisible) {
return; return;
} }
@ -3344,6 +3348,10 @@ define('select2/dropdown/infiniteScroll',[
this.trigger('query:append', params); this.trigger('query:append', params);
}; };
InfiniteScroll.prototype.showLoadingMore = function (_, data) {
return data.pagination && data.pagination.more;
};
InfiniteScroll.prototype.createLoadingMore = function () { InfiniteScroll.prototype.createLoadingMore = function () {
var $option = $( var $option = $(
'<li class="option load-more" role="treeitem"></li>' '<li class="option load-more" role="treeitem"></li>'

File diff suppressed because one or more lines are too long

View File

@ -745,11 +745,19 @@ $.fn.select2.amd.require(
page: params.page page: params.page
}; };
}, },
processResults: function (data, page) { processResults: function (data, params) {
// parse the results into the format expected by Select2. // parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to // since we are using custom formatting functions we do not need to
// alter the remote JSON data // alter the remote JSON data, except to indicate that infinite
return data.items; // scrolling can be used
params.page = params.page || 1;
return {
results: data.items,
pagination: {
more: (params.page * 30) < data.total_count
}
};
}, },
cache: true cache: true
}, },

View File

@ -44,7 +44,7 @@ define([
var $request = $.ajax(options); var $request = $.ajax(options);
$request.success(function (data) { $request.success(function (data) {
var results = self.processResults(data); var results = self.processResults(data, params);
callback(results); callback(results);
}); });

View File

@ -12,14 +12,18 @@ define([
InfiniteScroll.prototype.append = function (decorated, data) { InfiniteScroll.prototype.append = function (decorated, data) {
this.$loadingMore.remove(); this.$loadingMore.remove();
this.loading = false;
decorated.call(this, data); if ($.isArray(data)) {
decorated.call(this, data);
if (data.length > 0) { return;
this.$results.append(this.$loadingMore);
} }
this.loading = false; decorated.call(this, data.results);
if (this.showLoadingMore(data)) {
this.$results.append(this.$loadingMore);
}
}; };
InfiniteScroll.prototype.bind = function (decorated, container, $container) { InfiniteScroll.prototype.bind = function (decorated, container, $container) {
@ -38,12 +42,12 @@ define([
}); });
this.$results.on('scroll', function () { this.$results.on('scroll', function () {
var loadMoreVisible = $.contains( var isLoadMoreVisible = $.contains(
document.documentElement, document.documentElement,
self.$loadingMore[0] self.$loadingMore[0]
); );
if (self.loading || !loadMoreVisible) { if (self.loading || !isLoadMoreVisible) {
return; return;
} }
@ -68,6 +72,10 @@ define([
this.trigger('query:append', params); this.trigger('query:append', params);
}; };
InfiniteScroll.prototype.showLoadingMore = function (_, data) {
return data.pagination && data.pagination.more;
};
InfiniteScroll.prototype.createLoadingMore = function () { InfiniteScroll.prototype.createLoadingMore = function () {
var $option = $( var $option = $(
'<li class="option load-more" role="treeitem"></li>' '<li class="option load-more" role="treeitem"></li>'