Skip to content

Commit

Permalink
Support sorting by a given column; refs #34
Browse files Browse the repository at this point in the history
  • Loading branch information
pudo committed Feb 9, 2012
1 parent b96de6c commit 5fc4fa9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ this.recline.Model = this.recline.Model || {};
var numRows = queryObj.size;
var start = queryObj.offset;
var dfd = $.Deferred();
rows = model.backendConfig.data.rows;
var results = rows.slice(start, start+numRows);
results = model.backendConfig.data.rows;
// not complete sorting!
_.each(queryObj.sort, function(item) {
results = _.sortBy(results, function(row) {
var _out = row[item[0]];
return (item[1] == 'asc') ? _out : -1*_out;
});
});
var results = results.slice(start, start+numRows);
dfd.resolve(results);
return dfd.promise();
}
Expand Down
7 changes: 4 additions & 3 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ this.recline.Model = this.recline.Model || {};
// this does not fit very well with Backbone setup. Backbone really expects you to know the ids of objects your are fetching (which you do in classic RESTful ajax-y world). But this paradigm does not fill well with data set up we have here.
// This also illustrates the limitations of separating the Dataset and the Backend
query: function(queryObj) {
this.actualQuery = queryObj || this.defaultQuery;
this.actualQuery = _.extend({size: 100, offset: 0}, this.actualQuery);
this.queryState = queryObj || this.defaultQuery;
this.queryState = _.extend({size: 100, offset: 0}, this.queryState);

var self = this;
var backend = my.backends[this.backendConfig.type];
var dfd = $.Deferred();
backend.query(this, this.actualQuery).then(function(rows) {
backend.query(this, this.queryState).then(function(rows) {
var docs = _.map(rows, function(row) {
var _doc = new my.Document(row);
_doc.backendConfig = self.backendConfig;
Expand Down
2 changes: 2 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var util = function() {
, columnActions: ' \
<li><a data-action="bulkEdit" class="menuAction" href="JavaScript:void(0);">Transform...</a></li> \
<li><a data-action="deleteColumn" class="menuAction" href="JavaScript:void(0);">Delete this column</a></li> \
<li><a data-action="sortAsc" class="menuAction" href="JavaScript:void(0);">Sort ascending</a></li> \
<li><a data-action="sortDesc" class="menuAction" href="JavaScript:void(0);">Sort descending</a></li> \
'
, rowActions: '<li><a data-action="deleteRow" class="menuAction" href="JavaScript:void(0);">Delete this row</a></li>'
, cellEditor: ' \
Expand Down
6 changes: 6 additions & 0 deletions src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ my.DataTable = Backbone.View.extend({
var actions = {
bulkEdit: function() { self.showTransformColumnDialog('bulkEdit', {name: self.state.currentColumn}) },
transform: function() { self.showTransformDialog('transform') },
sortAsc: function() { self.setColumnSort('asc') },
sortDesc: function() { self.setColumnSort('desc') },
// TODO: Delete or re-implement ...
csv: function() { window.location.href = app.csvUrl },
json: function() { window.location.href = "_rewrite/api/json" },
Expand Down Expand Up @@ -290,6 +292,10 @@ my.DataTable = Backbone.View.extend({
$('.dialog').draggable({ handle: '.dialog-header', cursor: 'move' });
},

setColumnSort: function(order) {
var query = _.extend(this.model.queryState, {sort: [[this.state.currentColumn, order]]});
this.model.query(query);
},

// ======================================================
// Core Templating
Expand Down

0 comments on commit 5fc4fa9

Please sign in to comment.