Posted 12 August 2022, 12:20 pm EST
Most samples for retrieving and displaying data using the TableSheet plugin use the promise chain pattern seenbelow:
//bind a view to the table sheet
myTable.fetch().then(function () {
var view = myTable.addView("myView");
sheet.setDataView(view);
});
I would like to use the async/await pattern instead and as a result separate the creation of a table, the call to fetch data and the creation of a view into separate async functions. The result is a view that displays no records initially, but as you interact with the table, rows will start to show up. It is as if the TableSheet is no longer in sync with when it should be redrawing the control. Even a call to to refresh() does not help.
I think there is behavioral differences between promise chaining and async/await, specifically the code inside the then() function executes right BEFORE the finish of the fetch() code. So it may be that things are happening out of order as a result of using async/await.
export function AddDataTable(tableName: string,
theme: string,
rowOptions: IRowOptions,
customRowActions,
tabHelper) {
if (spread) {
var dataManager = spread.dataManager();
dataManager.addTable(tableName, {
remote: {
read: function () { return tabHelper.invokeMethodAsync('GetItemsCallBack'); },
update: function (item) { return tabHelper.invokeMethodAsync('UpdateItemCallBack', item); },
create: function (item) { return tabHelper.invokeMethodAsync('CreateItemCallBack', item); },
batch: function (changes) { return tabHelper.invokeMethodAsync('BatchChangesCallBack', changes); },
delete: function (item) { return tabHelper.invokeMethodAsync('DeleteItemCallBack', item); }
},
batch: true
});
var sheet: GC.Spread.Sheets.TableSheet.TableSheet = spread.addSheetTab(0, tableName, GC.Spread.Sheets.SheetType.tableSheet);
sheet.applyTableTheme(GC.Spread.Sheets.Tables.TableThemes[theme]);
SetRowActions(sheet, rowOptions, customRowActions, tabHelper);
}
}
export async function GetTableData(tableName: string) {
if (spread) {
var dataManager = spread.dataManager();
var tbl = dataManager.tables[tableName];
if (tbl) {
await tbl.fetch();
}
}
}
export function CreateView(tableName: string, viewName: string, columns: Array<GC.Data.IColumn>) {
if (spread) {
var dataManager = spread.dataManager();
var tbl = dataManager.tables[tableName];
if (tbl) {
dataViews[viewName] = tbl.addView(viewName, columns);
SetActiveView(tableName, viewName);
}
}
}
export function SetActiveView(tableName: string, viewName: string) {
if (spread) {
var sheet: GC.Spread.Sheets.TableSheet.TableSheet = spread.getSheetTab(tableName);
sheet.setDataView(dataViews[viewName]);
}
}
