1
0
mirror of synced 2024-11-22 13:06:08 +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);
$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>'

View File

@ -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>'

View File

@ -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>'

File diff suppressed because one or more lines are too long

22
dist/js/select2.js vendored
View File

@ -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>'

File diff suppressed because one or more lines are too long

View File

@ -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
},

View File

@ -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);
});

View File

@ -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>'