Skip to content

Commit f1271ab

Browse files
authored
Fix cell editing when new value is excluded by transform (#141)
* Fix editing when new value is excluded by transform * update widget model
1 parent c1fa5f3 commit f1271ab

File tree

2 files changed

+65
-33
lines changed

2 files changed

+65
-33
lines changed

src/core/viewbasedjsonmodel.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,15 @@ export class ViewBasedJSONModel extends MutableDataModel {
430430
});
431431
}
432432

433+
// Notify listeners of the cell change event
434+
this.dataSync.emit({
435+
type: 'cell-edit-event',
436+
columnIndex: options.column,
437+
row: options.row,
438+
region: options.region,
439+
value: options.value
440+
})
441+
433442
// We need to rerun the transforms, as the changed cell may change the order
434443
// or visibility of other rows
435444
this.currentView = this._transformState.createView(this._dataset);
@@ -609,7 +618,7 @@ namespace ViewBasedJSONModel {
609618
*/
610619
data: DataSource;
611620
}
612-
export type IDataSyncEvent = ISyncCell | ISyncRowIndices;
621+
export type IDataSyncEvent = ISyncCell | ISyncRowIndices | ICellEditEvent;
613622

614623
/**
615624
* An event that indicates a needed change to the kernel-side dataset.
@@ -624,12 +633,39 @@ namespace ViewBasedJSONModel {
624633
/**
625634
* The discriminated type of the args object.
626635
*/
627-
type: 'row-indices-updated'
636+
type: 'row-indices-updated';
628637

629638
/**
630639
* An list of the rows in the untransformed dataset that are currently
631640
* represented in the `View`.
632641
*/
633642
indices: number[];
634643
}
644+
645+
export interface ICellEditEvent {
646+
/**
647+
* The discriminated type of the args object.
648+
*/
649+
type: 'cell-edit-event';
650+
651+
/**
652+
* The CellRegion associated with this change.
653+
*/
654+
region: DataModel.CellRegion;
655+
656+
/**
657+
* The row number associated with this change.
658+
*/
659+
row: number;
660+
661+
/**
662+
* The column index associated with this change.
663+
*/
664+
columnIndex: number;
665+
666+
/**
667+
* The new data value
668+
*/
669+
value: ReadonlyJSONValue;
670+
}
635671
}

src/datagrid.ts

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -108,32 +108,28 @@ export
108108
this.set('_data', this.data_model.dataset);
109109
this.save_changes();
110110
break;
111+
case ('cell-edit-event'):
112+
// Update data in widget model
113+
const newData = this.get('_data');
114+
newData.data[msg.row][msg.columnIndex] = msg.value;
115+
this.set('_data', newData);
116+
117+
this.comm.send({
118+
method: 'custom',
119+
content: {
120+
event_type: 'cell-changed',
121+
region: msg.region,
122+
row: msg.row,
123+
column_index: msg.columnIndex,
124+
value: msg.value
125+
}
126+
}, null);
127+
break;
111128
default:
112129
throw 'unreachable';
113130
}
114-
})
115-
116-
this.data_model.changed.connect((sender: ViewBasedJSONModel, args: any) => {
117-
if (args.type === 'cells-changed') {
118-
const value = this.data_model.data(args.region, args.row, args.column);
119-
const datasetRow = this.data_model.getDatasetRowFromView(args.region, args.row);
120-
// Getting a copy of the data and update the front-end model in place
121-
const newData = this.get('_data');
122-
newData.data[datasetRow][args.column] = value;
123-
this.set('_data', newData);
124-
// Update backend with new data
125-
this.comm.send({
126-
method: 'custom',
127-
content: {
128-
event_type: 'cell-changed',
129-
region: args.region,
130-
row: datasetRow,
131-
column_index: args.column,
132-
value: value
133-
}
134-
}, null);
135-
}
136131
});
132+
137133
this.updateTransforms();
138134
this.trigger('data-model-changed');
139135
this.updateSelectionModel();
@@ -269,7 +265,7 @@ function unpack_data(
269265
}
270266

271267
export
272-
class DataGridView extends DOMWidgetView {
268+
class DataGridView extends DOMWidgetView {
273269
_createElement(tagName: string) {
274270
this.pWidget = new JupyterPhosphorPanelWidget({ view: this });
275271
// initialize to light theme unless set earlier
@@ -327,7 +323,7 @@ class DataGridView extends DOMWidgetView {
327323
this.model.set('column_widths', JSONExt.deepCopy(this.grid.columnWidths));
328324
this.model.save_changes();
329325
});
330-
326+
331327
this.model.on('data-model-changed', () => {
332328
this.grid.dataModel = this.model.data_model;
333329
});
@@ -410,7 +406,7 @@ class DataGridView extends DOMWidgetView {
410406

411407
return Promise.all(promises);
412408
}
413-
409+
414410
public updateGridStyle() {
415411
this.grid.updateGridStyle();
416412
}
@@ -430,9 +426,9 @@ class DataGridView extends DOMWidgetView {
430426
private updateGridRenderers() {
431427
const defaultRenderer = this.default_renderer.renderer;
432428
const renderers: Dict<CellRenderer> = {};
433-
Object.entries(this.renderers).forEach(([name, rendererView]) =>{
434-
renderers[name] = rendererView.renderer;
435-
}
429+
Object.entries(this.renderers).forEach(([name, rendererView]) => {
430+
renderers[name] = rendererView.renderer;
431+
}
436432
);
437433
this.grid.defaultRenderer = defaultRenderer;
438434
this.grid.renderers = renderers;
@@ -510,9 +506,9 @@ namespace Private {
510506
// Updating the primary key to account for a multiIndex primary key.
511507
const primaryKey = data.schema.primaryKey.map((key: string) => {
512508
for (let i = 0; i < data.schema.fields.length; i++) {
513-
const curFieldKey = Array.isArray(key)
514-
? data.schema.fields[i].name[0]
515-
: data.schema.fields[i].name;
509+
const curFieldKey = Array.isArray(key)
510+
? data.schema.fields[i].name[0]
511+
: data.schema.fields[i].name;
516512
const newKey = Array.isArray(key) ? key[0] : key;
517513

518514
if (curFieldKey == newKey) {

0 commit comments

Comments
 (0)