TableSheet supports a self-defined function to configure the remote request options.
By default, the TableSheet will configure the remote requests by an object. If you want to handle the request manually, just replace the object of the function, and the data and column changes will be passed to the function.
The details of the data changes are in the Request and Response
section of the Overview
.
Handle requests in the AutoSync
mode:
function requestToServer(params?): Promise<any> {
// api of request to server
}
var myTable = dataManager.addTable("myTable", {
// ... other settings
autoSync: true // enable the autoSync mode
remote: {
read: function (): Promise<any[]>{
return requestToServer();
},
update: function (item): Promise<any>{ // the updated data change
return requestToServer(item);
},
create: function (item): Promise<any>{ // the created data change
return requestToServer(item);
},
delete: function (item): Promise<boolean>{ // the deleted data change
return requestToServer(item);
},
// // not necessary if want to define the column
// getColumns: function (): Promise<any[]>{
// return requestToServer();
// },
// addColumn: function (change): Promise<any>{ // the added column change
// return requestToServer(change);
// },
// updateColumn: function (change): Promise<any>{ // the updated column change
// return requestToServer(change);
// },
// removeColumn: function (change): Promise<boolean>{ // the removed column change
// return requestToServer(change);
// },
}
});
Handle requests in the Batch
mode:
function requestToServer(params?): Promise<any> {
// api of request to server
}
var myTable = dataManager.addTable("myTable", {
// ... other settings
batch: true, // enable the batch mode
remote: {
// ... other settings
batch: function (changes: any[]): Promise<any[]>{ // the added, updated, removed column changes and the created, updated, deleted data changes
return requestToServer(changes);
}
}
});
When a Table uses CRUD functions instead of CRUD options, these functions can't be saved and restored after JSON serialization / deserialization because of security issue. Setting the Table’s data source options after the fromJSON method is invoked could make the Table working again.
// 1. invoke Workbook's fromJSON
spread.fromJSON(spreadJson);
// 2. update table options
let myTable = spread.dataManager().tables["myTable"];
myTable.options = {
remote: {
read: function () {
return Promise.resolve(dataSource);
}
}
};
//// make sure myTable.options be reset if just updating the properties of the options
// let options = myTable.options;
// options.remote.read.url = './new-url';
//// It could specify the GC.Data.IRemoteFetchOption(similar as RequestInit) to the options of the read property for the fetch options
// options.remote.read.options = { headers: { 'AuthenticationToken': 'fake_token' } };
// myTable.options = options; // It's important to reset the options to update
// 3. invoke table's fetch then setDataView
myTable.fetch(true).then(function() {
let myView = myTable.views["myView"];
let sheet = spread.getActiveSheetTab();
sheet.setDataView(myView);
});
When a Table’s fetch method is invoked and its reload argument is true, the Table will load data again.
class GC.Data.Table {
/**
* Requests the table data from local data source or remote data source by the data source option.
* @param {boolean} [reload] Whether to reload the data from server side forcibly.
* @returns {Promise} The resolving Promise thenable. You could get the data in Promise.then().
*/
fetch (reload?: boolean): Promise<any>;
}
And a View’s fetch method has same reload argument with a Table’s fetch method.
class GC.Data.View {
/**
* Requests the view data from its host table and related table.
* @param {boolean} [reload] Whether to reload the data from server side forcibly.
* @returns {Promise} The resolving Promise thenable. You could get the data in Promise.then().
*/
fetch (reload?: boolean): Promise<any>;
}
Submit and view feedback for