updated backgrid, pageable

pull/2/head
kay.one 11 years ago
parent 30a55c465a
commit 9515064f14

@ -50,6 +50,15 @@
Backbone.View.prototype.initialize.apply(this, arguments); Backbone.View.prototype.initialize.apply(this, arguments);
this.name = options.name || this.name; this.name = options.name || this.name;
this.placeholder = options.placeholder || this.placeholder; this.placeholder = options.placeholder || this.placeholder;
var collection = this.collection, self = this;
if (Backbone.PageableCollection &&
collection instanceof Backbone.PageableCollection &&
collection.mode == "server") {
collection.queryParams[this.name] = function () {
return self.$el.find("input[type=text]").val();
};
}
}, },
/** /**
@ -59,9 +68,8 @@
*/ */
search: function (e) { search: function (e) {
if (e) e.preventDefault(); if (e) e.preventDefault();
var $text = $(e.target).find("input[type=text]");
var data = {}; var data = {};
data[$text.attr("name")] = $text.val(); data[this.name] = this.$el.find("input[type=text]").val();
this.collection.fetch({data: data}); this.collection.fetch({data: data});
}, },

File diff suppressed because it is too large Load Diff

@ -52,23 +52,12 @@
Initializer. Initializer.
@param {Object} options @param {Object} options
@param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns
Column metadata.
@param {Backbone.Collection} options.collection @param {Backbone.Collection} options.collection
@param {boolean} [options.fastForwardHandleLabels] Whether to render fast forward buttons. @param {boolean} [options.fastForwardHandleLabels] Whether to render fast forward buttons.
*/ */
initialize: function (options) { initialize: function (options) {
//Backgrid.requireOptions(options, ["columns", "collection"]); Backgrid.requireOptions(options, ["collection"]);
this.columns = options.columns;
if (!(this.columns instanceof Backbone.Collection)) {
this.columns = new Backgrid.Columns(this.columns);
}
var columns = this.columns;
this.listenTo(columns, "add", this.render);
this.listenTo(columns, "remove", this.render);
this.listenTo(columns, "change:renderable", this.render);
var collection = this.collection; var collection = this.collection;
var fullCollection = collection.fullCollection; var fullCollection = collection.fullCollection;
if (fullCollection) { if (fullCollection) {
@ -92,31 +81,35 @@
changePage: function (e) { changePage: function (e) {
e.preventDefault(); e.preventDefault();
var label = $(e.target).text(); var $li = $(e.target).parent();
var ffLabels = this.fastForwardHandleLabels; if (!$li.hasClass("active") && !$li.hasClass("disabled")) {
var collection = this.collection; var label = $(e.target).text();
var ffLabels = this.fastForwardHandleLabels;
if (ffLabels) {
switch (label) { var collection = this.collection;
case ffLabels.first:
collection.getFirstPage(); if (ffLabels) {
return; switch (label) {
case ffLabels.prev: case ffLabels.first:
if (collection.hasPrevious()) collection.getPreviousPage(); collection.getFirstPage();
return; return;
case ffLabels.next: case ffLabels.prev:
if (collection.hasNext()) collection.getNextPage(); collection.getPreviousPage();
return; return;
case ffLabels.last: case ffLabels.next:
collection.getLastPage(); collection.getNextPage();
return; return;
case ffLabels.last:
collection.getLastPage();
return;
}
} }
}
var state = collection.state; var state = collection.state;
var pageIndex = $(e.target).text() * 1; var pageIndex = +label;
collection.getPage(state.firstPage === 0 ? pageIndex - 1 : pageIndex); collection.getPage(state.firstPage === 0 ? pageIndex - 1 : pageIndex);
}
}, },
/** /**
@ -132,12 +125,13 @@
var state = collection.state; var state = collection.state;
// convert all indices to 0-based here // convert all indices to 0-based here
var lastPage = state.lastPage ? state.lastPage : state.firstPage; var firstPage = state.firstPage;
lastPage = state.firstPage === 0 ? lastPage : lastPage - 1; var lastPage = +state.lastPage;
var currentPage = state.firstPage === 0 ? state.currentPage : state.currentPage - 1; lastPage = Math.max(0, firstPage ? lastPage - 1 : lastPage);
var currentPage = Math.max(state.currentPage, state.firstPage);
currentPage = firstPage ? currentPage - 1 : currentPage;
var windowStart = Math.floor(currentPage / this.windowSize) * this.windowSize; var windowStart = Math.floor(currentPage / this.windowSize) * this.windowSize;
var windowEnd = windowStart + this.windowSize; var windowEnd = Math.min(lastPage + 1, windowStart + this.windowSize);
windowEnd = windowEnd <= lastPage ? windowEnd : lastPage + 1;
if (collection.mode !== "infinite") { if (collection.mode !== "infinite") {
for (var i = windowStart; i < windowEnd; i++) { for (var i = windowStart; i < windowEnd; i++) {
@ -185,8 +179,7 @@
}, },
/** /**
Render the paginator handles inside an unordered list placed inside a Render the paginator handles inside an unordered list.
cell that spans all the columns.
*/ */
render: function () { render: function () {
this.$el.empty(); this.$el.empty();

@ -1,5 +1,5 @@
/* /*
backbone-pageable 1.2.2 backbone-pageable 1.2.3
http://github.com/wyuenho/backbone-pageable http://github.com/wyuenho/backbone-pageable
Copyright (c) 2013 Jimmy Yuen Ho Wong Copyright (c) 2013 Jimmy Yuen Ho Wong
@ -65,6 +65,7 @@
var _isUndefined = _.isUndefined; var _isUndefined = _.isUndefined;
var _result = _.result; var _result = _.result;
var ceil = Math.ceil; var ceil = Math.ceil;
var floor = Math.floor;
var max = Math.max; var max = Math.max;
var BBColProto = Backbone.Collection.prototype; var BBColProto = Backbone.Collection.prototype;
@ -303,6 +304,8 @@
state.firstPage : state.firstPage :
state.currentPage; state.currentPage;
if (!_isArray(models)) models = models ? [models] : [];
if (mode != "server" && state.totalRecords == null && !_isEmpty(models)) { if (mode != "server" && state.totalRecords == null && !_isEmpty(models)) {
state.totalRecords = models.length; state.totalRecords = models.length;
} }
@ -848,6 +851,28 @@
return this.fetch(_omit(options, "fetch")); return this.fetch(_omit(options, "fetch"));
}, },
/**
Fetch the page for the provided item offset in server mode, or reset the current page of this
collection to the page for the provided item offset in client mode.
@param {Object} options {@link #getPage} options.
@chainable
@return {XMLHttpRequest|Backbone.PageableCollection} The XMLHttpRequest
from fetch or this.
*/
getPageByOffset: function (offset, options) {
if (offset < 0) {
throw new RangeError("`offset must be > 0`");
}
offset = finiteInt(offset);
var page = floor(offset / this.state.pageSize);
if (this.state.firstPage !== 0) page++;
if (page > this.state.lastPage) page = this.state.lastPage;
return this.getPage(page, options);
},
/** /**
Overidden to make `getPage` compatible with Zepto. Overidden to make `getPage` compatible with Zepto.

Loading…
Cancel
Save