@@ -67,6 +67,9 @@ export class DataGridModel extends DOMWidgetModel {
6767 initialize ( attributes : any , options : any ) {
6868 super . initialize ( attributes , options ) ;
6969
70+ this . updateDataSync = this . updateDataSync . bind ( this ) ;
71+ this . syncTransformState = this . syncTransformState . bind ( this ) ;
72+
7073 this . on ( 'change:_data' , this . updateData . bind ( this ) ) ;
7174 this . on ( 'change:_transforms' , this . updateTransforms . bind ( this ) ) ;
7275 this . on ( 'change:selection_mode' , this . updateSelectionModel , this ) ;
@@ -87,50 +90,58 @@ export class DataGridModel extends DOMWidgetModel {
8790 } ) ;
8891 }
8992
93+ updateDataSync ( sender : any , msg : any ) {
94+ switch ( msg . type ) {
95+ case 'row-indices-updated' :
96+ this . set ( '_visible_rows' , msg . indices ) ;
97+ this . save_changes ( ) ;
98+ break ;
99+ case 'cell-updated' :
100+ this . set ( '_data' , this . data_model . dataset ) ;
101+ this . save_changes ( ) ;
102+ break ;
103+ case 'cell-edit-event' :
104+ // Update data in widget model
105+ const newData = this . get ( '_data' ) ;
106+ newData . data [ msg . row ] [ msg . columnIndex ] = msg . value ;
107+ this . set ( '_data' , newData ) ;
108+
109+ this . send (
110+ {
111+ event_type : 'cell-changed' ,
112+ region : msg . region ,
113+ row : msg . row ,
114+ column_index : msg . columnIndex ,
115+ value : msg . value ,
116+ } ,
117+ { ...this . _view_callbacks } ,
118+ ) ;
119+ break ;
120+ default :
121+ throw 'unreachable' ;
122+ }
123+ }
124+
125+ syncTransformState ( sender : any , value : any ) {
126+ this . set ( '_transforms' , value . transforms ) ;
127+ this . save_changes ( ) ;
128+ }
129+
90130 updateData ( ) {
91131 const data = this . data ;
92132 const schema = Private . createSchema ( data ) ;
93133
94- this . data_model = new ViewBasedJSONModel ( {
95- data : data . data ,
96- schema : schema ,
97- } ) ;
98- this . data_model . transformStateChanged . connect ( ( sender , value ) => {
99- this . set ( '_transforms' , value . transforms ) ;
100- this . save_changes ( ) ;
101- } ) ;
102-
103- this . data_model . dataSync . connect ( ( sender , msg ) => {
104- switch ( msg . type ) {
105- case 'row-indices-updated' :
106- this . set ( '_visible_rows' , msg . indices ) ;
107- this . save_changes ( ) ;
108- break ;
109- case 'cell-updated' :
110- this . set ( '_data' , this . data_model . dataset ) ;
111- this . save_changes ( ) ;
112- break ;
113- case 'cell-edit-event' :
114- // Update data in widget model
115- const newData = this . get ( '_data' ) ;
116- newData . data [ msg . row ] [ msg . columnIndex ] = msg . value ;
117- this . set ( '_data' , newData ) ;
118-
119- this . send (
120- {
121- event_type : 'cell-changed' ,
122- region : msg . region ,
123- row : msg . row ,
124- column_index : msg . columnIndex ,
125- value : msg . value ,
126- } ,
127- { ...this . _view_callbacks } ,
128- ) ;
129- break ;
130- default :
131- throw 'unreachable' ;
132- }
133- } ) ;
134+ if ( this . data_model ) {
135+ // Need to update existing ViewBasedJSONModel's dataset attribute.
136+ this . data_model . updateDataset ( { data : data . data , schema : schema } ) ;
137+ } else {
138+ this . data_model = new ViewBasedJSONModel ( {
139+ data : data . data ,
140+ schema : schema ,
141+ } ) ;
142+ this . data_model . transformStateChanged . connect ( this . syncTransformState ) ;
143+ this . data_model . dataSync . connect ( this . updateDataSync ) ;
144+ }
134145
135146 this . updateTransforms ( ) ;
136147 this . trigger ( 'data-model-changed' ) ;
0 commit comments