@@ -12,6 +12,9 @@ let saveDataFileName = '';
12
12
let tableContainer , table , progressRing , saveFileTypeSelector ;
13
13
let tableColumns = [ ] ;
14
14
let tableData = [ ] ;
15
+ let loadedRows = 0 ;
16
+ let totalRows = 0 ;
17
+ let dataPage = 0 ;
15
18
16
19
// table view settings
17
20
const toolbarHeight = 40 ; // table view toolbar height offset
@@ -89,8 +92,12 @@ window.addEventListener('message', event => {
89
92
fileName = event . data . fileName ;
90
93
vscode . setState ( { documentUrl : documentUrl } ) ;
91
94
tableData = event . data . tableData ;
95
+ totalRows = event . data . totalRows ;
92
96
loadData ( tableData , fileName ) ;
93
97
break ;
98
+ case 'addData' :
99
+ addData ( table , event . data . dataRows ) ;
100
+ break ;
94
101
}
95
102
} ) ;
96
103
@@ -140,10 +147,26 @@ function reloadData() {
140
147
* Loads and displays table data.
141
148
*
142
149
* @param {* } tableData Data array to display in tabulator table.
143
- * @param {* } fileName Data file name for table config persistence and reload.
144
150
*/
145
- function loadData ( tableData , documentUrl ) {
151
+ function loadData ( tableData ) {
146
152
logTableData ( tableData ) ;
153
+ if ( table === undefined ) {
154
+ createTable ( tableData ) ;
155
+ }
156
+ else {
157
+ // reload table data
158
+ clearTable ( table ) ;
159
+ addData ( table , tableData ) ;
160
+ progressRing . style . visibility = 'hidden' ;
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Creates new Tabulator table with initial set of data to display.
166
+ *
167
+ * @param {* } tableData Data array to display in tabulator table.
168
+ */
169
+ function createTable ( tableData ) {
147
170
if ( table === undefined ) {
148
171
table = new Tabulator ( '#table-container' , {
149
172
height : window . innerHeight - toolbarHeight ,
@@ -211,63 +234,64 @@ function loadData(tableData, documentUrl) {
211
234
}
212
235
} ) ;
213
236
214
- table . on ( 'tableBuilt' , function ( ) {
215
- // hide data loading progress ring
216
- progressRing . style . visibility = 'hidden' ;
217
-
218
- // get table columns for debug
219
- const columns = table . getColumns ( ) ;
220
- console . log ( 'tableView.columns:' , columns ) ;
221
-
222
- // add row selection column
223
- table . addColumn ( {
224
- formatter : 'rowSelection' ,
225
- titleFormatter : 'rowSelection' ,
226
- headerMenu : [ ] , // don't show header context menu
227
- headerSort : false ,
228
- download : false // don't include it in data save
229
- } , true ) // add as 1st column
230
- . then ( function ( column ) {
231
- // column - the component for the newly created column
232
- // run code after column has been added
233
- } )
234
- . catch ( function ( error ) {
235
- // log adding row selection column error for now
236
- console . error ( 'tableView.addRowSelectionCollumn: Error\n' , error ) ;
237
- } ) ;
238
- } ) ;
239
- }
240
- else {
241
- // reload table data
242
- clearTable ( table ) ;
243
- addData ( table , tableData ) ;
244
- progressRing . style . visibility = 'hidden' ;
237
+ // update table settings after initial data rows load
238
+ table . on ( 'tableBuilt' , onTableBuilt ) ;
245
239
}
246
240
}
247
241
248
242
/**
249
- * Removes all table data.
243
+ * Updates Tabulator table after initial set of data rows is loaded .
250
244
*/
251
- function clearTable ( table ) {
252
- if ( table ) {
253
- table . clearData ( ) ;
245
+ function onTableBuilt ( ) {
246
+ // hide data loading progress ring
247
+ progressRing . style . visibility = 'hidden' ;
248
+
249
+ // get table columns for debug
250
+ const columns = table . getColumns ( ) ;
251
+ console . log ( 'tableView.columns:' , columns ) ;
252
+
253
+ // add row selection column
254
+ table . addColumn ( {
255
+ formatter : 'rowSelection' ,
256
+ titleFormatter : 'rowSelection' ,
257
+ headerMenu : [ ] , // don't show header context menu
258
+ headerSort : false ,
259
+ download : false // don't include it in data save
260
+ } , true ) // add as 1st column
261
+ . then ( function ( column ) {
262
+ // column - the component for the newly created column
263
+ // run code after column has been added
264
+ } )
265
+ . catch ( function ( error ) {
266
+ // log adding row selection column error for now
267
+ console . error ( 'tableView.addRowSelectionCollumn: Error\n' , error ) ;
268
+ } ) ;
269
+
270
+ // request more data for incremental data loading
271
+ loadedRows = table . getRows ( ) . length ;
272
+ if ( loadedRows < totalRows ) {
273
+ dataPage ++ ;
274
+ progressRing . style . visibility = 'visible' ;
275
+ vscode . postMessage ( {
276
+ command : 'addData' ,
277
+ dataPage : dataPage
278
+ } ) ;
254
279
}
255
280
}
256
281
257
282
/**
258
- * Scrolls table data display to the first table row .
283
+ * Clears displayed table data .
259
284
*/
260
- function scrollToFirstRow ( ) {
261
- const rows = table . getRows ( ) ;
262
- table . scrollToRow ( rows [ 0 ] , 'top' , true ) ;
263
- }
285
+ function clearTable ( table ) {
286
+ if ( table ) {
287
+ // clear displayed table view
288
+ table . clearData ( ) ;
264
289
265
- /**
266
- * Scrolls table data display to the last table row.
267
- */
268
- function scrollToLastRow ( ) {
269
- const rows = table . getRows ( ) ;
270
- table . scrollToRow ( rows [ rows . length - 1 ] , 'top' , true ) ;
290
+ // reset rows and data page counters
291
+ loadedRows = 0 ;
292
+ totalRows = 0 ;
293
+ dataPage = 0 ;
294
+ }
271
295
}
272
296
273
297
/**
@@ -279,7 +303,23 @@ function scrollToLastRow() {
279
303
function addData ( table , tableData ) {
280
304
if ( table && tableData ) {
281
305
table . addData ( tableData , true )
282
- . then ( function ( rows ) { //rows - array of the row components for the rows updated or added
306
+ . then ( function ( rows ) { // rows - array of the row components for the rows updated or added
307
+ // update loaded rows counter
308
+ loadedRows += rows . length ;
309
+ if ( loadedRows < totalRows ) {
310
+ // request more data to load and display
311
+ dataPage ++ ;
312
+ progressRing . style . visibility = 'visible' ;
313
+ vscode . postMessage ( {
314
+ command : 'addData' ,
315
+ dataPage : dataPage
316
+ } ) ;
317
+ }
318
+ else {
319
+ // hide data loading progress ring
320
+ progressRing . style . visibility = 'hidden' ;
321
+ console . log ( 'tableView:rowCount:' , loadedRows ) ;
322
+ }
283
323
} )
284
324
. catch ( function ( error ) {
285
325
// handle error updating data
@@ -288,6 +328,22 @@ function addData(table, tableData) {
288
328
}
289
329
}
290
330
331
+ /**
332
+ * Scrolls table data display to the first table row.
333
+ */
334
+ function scrollToFirstRow ( ) {
335
+ const rows = table . getRows ( ) ;
336
+ table . scrollToRow ( rows [ 0 ] , 'top' , true ) ;
337
+ }
338
+
339
+ /**
340
+ * Scrolls table data display to the last table row.
341
+ */
342
+ function scrollToLastRow ( ) {
343
+ const rows = table . getRows ( ) ;
344
+ table . scrollToRow ( rows [ rows . length - 1 ] , 'top' , true ) ;
345
+ }
346
+
291
347
/**
292
348
* Saves table data as CSV, TSV, or JSON data array document.
293
349
*/
@@ -335,6 +391,6 @@ function saveData() {
335
391
* @param tableData Loaded able data.
336
392
*/
337
393
function logTableData ( tableData ) {
338
- console . log ( 'tabular.data.view :rowCount:' , tableData . length ) ;
394
+ console . log ( 'tableView :rowCount:' , tableData . length ) ;
339
395
console . log ( '1st 10 rows:' , tableData . slice ( 0 , 10 ) ) ;
340
396
}
0 commit comments