Skip to content

Commit b803b32

Browse files
authored
Merge pull request #106 from martinRenou/add_sorting
Add support for column sorting
2 parents a7f2018 + f52683d commit b803b32

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

js/src/sheet.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,51 @@ let SheetView = widgets.DOMWidgetView.extend({
342342
rowHeaders: true,
343343
colHeaders: true,
344344
search: true,
345+
columnSorting: {
346+
sortEmptyCells: false,
347+
indicator: true,
348+
headerAction: true,
349+
compareFunctionFactory: this._compareFunctionFactory
350+
},
345351
cells: (...args) => this._cell(...args),
346352
afterChange: (changes, source) => { this._on_change(changes, source); },
347353
afterRemoveCol: (changes, source) => { this._on_change_grid(changes, source); },
348354
afterRemoveRow: (changes, source) => { this._on_change_grid(changes, source); }
349355
}, this._hot_settings()));
350356
},
357+
_compareFunctionFactory: function(sortOrder, columnMeta) {
358+
return function(value, nextValue) {
359+
let a, b;
360+
if (sortOrder == 'desc') {
361+
a = value;
362+
b = nextValue;
363+
} else {
364+
a = nextValue;
365+
b = value;
366+
}
367+
368+
if (a instanceof widgets.WidgetModel) {
369+
a = a.get("value");
370+
}
371+
372+
if (b instanceof widgets.WidgetModel) {
373+
b = b.get("value");
374+
}
375+
376+
if (a == undefined || b == undefined) {
377+
return 0;
378+
}
379+
380+
if (a < b) {
381+
return -1;
382+
}
383+
if (a > b) {
384+
return 1;
385+
}
386+
387+
return 0;
388+
}
389+
},
351390
_update_hot_settings: function() {
352391
this.hot.updateSettings(this._hot_settings());
353392
},
@@ -401,7 +440,7 @@ let SheetView = widgets.DOMWidgetView.extend({
401440
this.model.save_changes();
402441
},
403442
_on_change: function(changes, source) {
404-
if(this.hot === undefined || source == 'loadData') {
443+
if(this.hot === undefined || source == 'loadData' || source == 'ObserveChanges.change') {
405444
return;
406445
}
407446
if(source == 'alter') {

0 commit comments

Comments
 (0)