Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

HtmlTable: Adding ability to serialize the state of a table and resto…

…re it.
  • Loading branch information...
commit 0d937eb10e96dd24dec273c3a4edfb31fc227f02 1 parent e8dddea
@anutron anutron authored
View
27 Docs/Interface/HtmlTable.Select.md
@@ -215,4 +215,31 @@ Deselects all rows
* (*object*) This instance of HtmlTable.
+HtmlTable Method: serialize {#HtmlTable:serialize}
+----------------------------------------
+
+Return an object storing the select state of the table.
+
+### Syntax
+
+ myTable.serialize();
+
+### Returns
+
+* (*object*) Object containing select state of the table.
+
+
+HtmlTable Method: restore {#HtmlTable:restore}
+----------------------------------------
+
+Restores the select state of the table from an object passed in.
+
+### Syntax
+
+ myTable.restore(tableState);
+
+### Arguments
+
+1. tableState - Object created by HtmlTable.serialize() which contains the select state of the table.
+
[HtmlTable]: /more/Interface/HtmlTable
View
28 Docs/Interface/HtmlTable.Sort.md
@@ -105,6 +105,34 @@ Reapplies the current sort. Note that this is called whenever new rows are added
* (*object*) This instance of HtmlTable.
+HtmlTable Method: serialize {#HtmlTable:serialize}
+----------------------------------------
+
+Return an object storing the sort state of the table.
+
+### Syntax
+
+ myTable.serialize();
+
+### Returns
+
+* (*object*) Object containing sort state of the table.
+
+
+HtmlTable Method: restore {#HtmlTable:restore}
+----------------------------------------
+
+Restores the sort state of the table from an object passed in.
+
+### Syntax
+
+ myTable.restore(tableState);
+
+### Arguments
+
+1. tableState - Object created by HtmlTable.serialize() which contains the sort state of the table.
+
+
Object: HtmlTable.Parsers {#HtmlTable:Parsers}
==============================================
View
26 Source/Interface/HtmlTable.Select.js
@@ -97,6 +97,7 @@ HtmlTable = Class.refactor(HtmlTable, {
this._selectedRows.push(row);
row.addClass(this.options.classRowSelected);
this.fireEvent('rowFocus', [row, this._selectedRows]);
+ this.fireEvent('stateChanged');
}
this._focused = row;
@@ -105,13 +106,36 @@ HtmlTable = Class.refactor(HtmlTable, {
return this;
},
+ getSelected: function(){
+ return this._selectedRows;
+ },
+
+ serialize: function() {
+ var previousSerialization = this.previous.apply(this, arguments) || {};
+ if (this.options.selectable) {
+ previousSerialization.selectedRows = this._selectedRows.map(function(row) {
+ return Array.indexOf(this.body.rows, row);
+ }.bind(this));
+ }
+ return previousSerialization;
+ },
+
+ restore: function(tableState) {
+ if(this.options.selectable && tableState.selectedRows) {
+ tableState.selectedRows.each(function(index) {
+ this.selectRow(this.body.rows[index]);
+ }.bind(this));
+ }
+ this.previous.apply(this, arguments);
+ },
+
deselectRow: function(row, _nocheck){
if (!this.isSelected(row) || (!_nocheck && !this.body.getChildren().contains(row))) return;
this._selectedRows = new Elements(Array.from(this._selectedRows).erase(row));
row.removeClass(this.options.classRowSelected);
this.fireEvent('rowUnfocus', [row, this._selectedRows]);
-
+ this.fireEvent('stateChanged');
return this;
},
View
18 Source/Interface/HtmlTable.Sort.js
@@ -121,6 +121,22 @@ HtmlTable = Class.refactor(HtmlTable, {
return this.sort(Array.indexOf(this.head.getElements(this.options.thSelector).flatten(), el) % this.body.rows[0].cells.length);
},
+ serialize: function() {
+ var previousSerialization = this.previous.apply(this, arguments) || {};
+ if (this.options.sortable) {
+ previousSerialization.sortIndex = this.sorted.index;
+ previousSerialization.sortReverse = this.sorted.reverse;
+ }
+ return previousSerialization;
+ },
+
+ restore: function(tableState) {
+ if(this.options.sortable && tableState.sortIndex) {
+ this.sort(tableState.sortIndex, tableState.sortReverse);
+ }
+ this.previous.apply(this, arguments);
+ },
+
setSortedState: function(index, reverse){
if (reverse != null) this.sorted.reverse = reverse;
else if (this.sorted.index == index) this.sorted.reverse = !this.sorted.reverse;
@@ -210,7 +226,7 @@ HtmlTable = Class.refactor(HtmlTable, {
this.setRowSort(data, pre);
if (rel) rel.grab(this.body);
-
+ this.fireEvent('stateChanged');
return this.fireEvent('sort', [this.body, this.sorted.index]);
},
View
24 Specs/1.3/Interface/HtmlTable.Select.js
@@ -13,4 +13,28 @@ describe('HtmlTable.Select', function(){
expect(SelectableTable.isSelected(row)).toEqual(false);
});
+ it('should serialize the state of the table', function(){
+ var SelectableTable = new HtmlTable({
+ selectable: true,
+ useKeyboard: false,
+ rows: [[0],[1],[2]]
+ });
+
+ SelectableTable.selectRow(SelectableTable.body.getChildren()[0]);
+ SelectableTable.selectRow(SelectableTable.body.getChildren()[2]);
+ var state = SelectableTable.serialize();
+ expect(state).toEqual({selectedRows: [0, 2]});
+ });
+
+ it('should restore the state of the table', function(){
+ var SelectableTable = new HtmlTable({
+ selectable: true,
+ useKeyboard: false,
+ rows: [[0],[1],[2]]
+ });
+ SelectableTable.restore({selectedRows: [0, 2]});
+ expect(SelectableTable.getSelected()).toEqual($$([SelectableTable.body.getChildren()[0], SelectableTable.body.getChildren()[2]]));
+ });
+
+
});
View
32 Specs/1.3/Interface/HtmlTable.Sort.js
@@ -134,4 +134,36 @@ describe('HtmlTable.Sort', function(){
});
+ describe('serialize/deserialize', function(){
+
+ it('should serialize the sorted state of a table', function(){
+ var table = new HtmlTable({
+ sortable: true,
+ headers: ['col'],
+ parsers: ['number'],
+ rows: [[1],[0],[2]]
+ });
+
+ table.sort(0, false);
+ expect(table.serialize()).toEqual({sortIndex: 0, sortReverse: false});
+ });
+
+ it('should restore the sorted state of a table', function(){
+ var table = new HtmlTable({
+ sortable: true,
+ headers: ['col'],
+ parsers: ['number'],
+ rows: [[1],[0],[2]]
+ });
+ table.restore({sortIndex: 0, sortReverse: false});
+
+ var order = Array.map(table.body.rows, function(item){
+ return item.cells[0].get('text');
+ });
+
+ expect(order).toEqual(['0','1','2']);
+ });
+
+
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.