Overview

SpreadJS allows you to bind the name, dataField, formatter, cellType, and convert function inside of a table column.

Description
app.component.ts
index.html
app.component.html
styles.css
Copy to CodeMine

Use the bind method to bind the table to a field with records, and the table columns to the record's fields. When you set a different data source, the table will automatically bind to corresponding records. For example:

    table.bind(tableColumns, 'sales', data);

We provide the bind API to solve complicated call issue; you can use it as shown in the below code:
Example 1:

    var data = {
        name: 'Jones', region: 'East',
        sales: [
            {orderDate: '1/6/2013', item: 'Pencil', units: 95, cost: 1.99, isMakeMoney: true},
            {orderDate: '4/1/2013', item: 'Binder', units: 60, cost: 4.99, isMakeMoney: false},
            {orderDate: '6/8/2013', item: 'Pen Set', units: 16, cost: 15.99, isMakeMoney: false}
        ]
    };
    var convert = function (item) {
        return item['cost'] + '$';
    }
    var table = sheet.tables.add('tableSales', 0, 0, 5, 5);
    var tableColumn1 = new spreadNS.Tables.TableColumn();
    tableColumn1.name("Order Date");
    tableColumn1.dataField("orderDate");
    tableColumn1.formatter("d/M/yy");
    var tableColumn2 = new spreadNS.Tables.TableColumn();
    tableColumn2.name("Item");
    tableColumn2.dataField("item");
    var tableColumn3 = new spreadNS.Tables.TableColumn();
    tableColumn3.name("Units");
    tableColumn3.dataField("units");
    var tableColumn4 = new spreadNS.Tables.TableColumn();
    tableColumn4.name("Cost");
    tableColumn4.dataField("cost");
    tableColumn4.value(convert);
    var tableColumn5 = new spreadNS.Tables.TableColumn();
    tableColumn5.name("IsMakeMoney");
    tableColumn5.dataField("isMakeMoney");
    tableColumn5.cellType(new GC.Spread.Sheets.CellTypes.CheckBox());
    table.autoGenerateColumns(false);
    table.bind([tableColumn1, tableColumn2, tableColumn3, tableColumn4, tableColumn5], 'sales', data);

Example 2:

    var data = {
        name: 'Jones', region: 'East',
        sales: [
            {orderDate: '1/6/2013', item: 'Pencil', units: 95, cost: 1.99, isMakeMoney: true},
            {orderDate: '4/1/2013', item: 'Binder', units: 60, cost: 4.99, isMakeMoney: false},
            {orderDate: '6/8/2013', item: 'Pen Set', units: 16, cost: 15.99, isMakeMoney: false}
        ]
    };
    var convert = function (item) {
        return item['cost'] + '$';
    }
    var table = sheet.tables.add('tableSales', 0, 0, 5, 5);
    var tableColumn1 = new spreadNS.Tables.TableColumn(1, "orderDate", "Order Date", "d/M/yy");
    var tableColumn2 = new spreadNS.Tables.TableColumn(2, "item", "Item");
    var tableColumn3 = new spreadNS.Tables.TableColumn(3, "units", "Units");
    var tableColumn4 = new spreadNS.Tables.TableColumn(4, "cost", "Cost", null, null, convert);
    var tableColumn5 = new spreadNS.Tables.TableColumn(5, "isMakeMoney", "IsMakeMoney", null, new GC.Spread.Sheets.CellTypes.CheckBox());
    table.autoGenerateColumns(false);
    table.bind([tableColumn1, tableColumn2, tableColumn3, tableColumn4, tableColumn5], 'sales', data);

When you have already bound the data source, you can get the dirty table rows if you change the data source value:

  var dirtyRows = table.getDirtyRows();
Use the bind method to bind the table to a field with records, and the table columns to the record's fields. When you set a different data source, the table will automatically bind to corresponding records. For example: We provide the bind API to solve complicated call issue; you can use it as shown in the below code: Example 1: Example 2: When you have already bound the data source, you can get the dirty table rows if you change the data source value:
import { Component, NgModule, enableProdMode } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { SpreadSheetsModule } from '@mescius/spread-sheets-angular'; import GC from '@mescius/spread-sheets'; import './styles.css'; const GCsheets = GC.Spread.Sheets; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { spread: GC.Spread.Sheets.Workbook; table: GC.Spread.Sheets.Tables.Table; dirtyRowsValue: string = null; hostStyle = { width: 'calc(100% - 280px)', height: '100%', overflow: 'hidden', float: 'left' }; constructor() { } initSpread($event: any) { this.spread = $event.spread; let spread = this.spread; spread.suspendPaint(); let sheet = spread.getActiveSheet(); let data = { name: 'Jones', region: 'East', sales: [ {orderDate: '2020-12-12', item: 'Pencil', units: 95, cost: 1.99, isDelivered: true}, {orderDate: '2020-10-04', item: 'Binder', units: 60, cost: 4.99, isDelivered: false}, {orderDate: '2020-09-05', item: 'Pen Set', units: 16, cost: 15.99, isDelivered: false} ] }; let convert = function (item: any) { return item['cost'] + '$'; } let table = sheet.tables.add('tableSales', 0, 0, 5, 5); this.table = table; table.style(GCsheets.Tables.TableThemes["medium4"]); let tableColumn1 = new GCsheets.Tables.TableColumn(1, "orderDate", "Order Date", "yyyy-mm-dd"); let tableColumn2 = new GCsheets.Tables.TableColumn(2, "item", "Item"); let tableColumn3 = new GCsheets.Tables.TableColumn(3, "units", "Units"); let tableColumn4 = new GCsheets.Tables.TableColumn(4, "cost", "Cost", null, null, convert); let tableColumn5 = new GCsheets.Tables.TableColumn(5, "isDelivered", "Delivered", null, new GC.Spread.Sheets.CellTypes.CheckBox()); table.autoGenerateColumns(false); table.bind([tableColumn1, tableColumn2, tableColumn3, tableColumn4, tableColumn5], 'sales', data); sheet.setColumnWidth(0, 120); sheet.setColumnWidth(1, 120); sheet.setColumnWidth(2, 120); sheet.setColumnWidth(3, 120); sheet.setColumnWidth(4, 120); spread.resumePaint(); } onGetDirtyRowsClickEvent() { let table = this.table; if (table) { let dataArr = table.getDirtyRows(); let str = ""; for (let i = 0; i < dataArr.length; i ++) { str += dataArr[i].row + ", "; } this.dirtyRowsValue = str; } } } @NgModule({ imports: [BrowserModule, SpreadSheetsModule], declarations: [AppComponent], exports: [AppComponent], bootstrap: [AppComponent] }) export class AppModule {} enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule);
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/angular/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <!-- Polyfills --> <script src="$DEMOROOT$/en/angular/node_modules/core-js/client/shim.min.js"></script> <script src="$DEMOROOT$/en/angular/node_modules/zone.js/fesm2015/zone.min.js"></script> <!-- SystemJS --> <script src="$DEMOROOT$/en/angular/node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.import('@angular/compiler'); System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('$DEMOROOT$/en/lib/angular/license.ts'); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html>
<div class="sample-tutorial"> <gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="initSpread($event)"> </gc-spread-sheets> <div class="options-container"> <div class="option-row"> <label class="custom-label">Change table data and click button to get the dirty rows.</label> </div> <input type="button" id="getState" value="Get Dirty Rows" (click)="onGetDirtyRowsClickEvent()" /> <textarea id="stateText" class="state-text" [value]="dirtyRowsValue"></textarea> </div> </div>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; margin-top: 10px; } label { display: block; margin-bottom: 6px; } input { padding: 4px 6px; } input[type=button] { margin-top: 6px; display: block; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .custom-label { background-color:#F4F8EB; } .state-text { width: 100%; height: 30px; margin-top:10px; }
(function (global) { System.config({ transpiler: 'ts', typescriptOptions: { tsconfig: true }, meta: { 'typescript': { "exports": "ts" }, '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { 'core-js': 'npm:core-js/client/shim.min.js', 'zone': 'npm:zone.js/fesm2015/zone.min.js', 'rxjs': 'npm:rxjs/dist/bundles/rxjs.umd.min.js', '@angular/core': 'npm:@angular/core/fesm2022', '@angular/common': 'npm:@angular/common/fesm2022/common.mjs', '@angular/compiler': 'npm:@angular/compiler/fesm2022/compiler.mjs', '@angular/platform-browser': 'npm:@angular/platform-browser/fesm2022/platform-browser.mjs', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/fesm2022/platform-browser-dynamic.mjs', '@angular/common/http': 'npm:@angular/common/fesm2022/http.mjs', '@angular/router': 'npm:@angular/router/fesm2022/router.mjs', '@angular/forms': 'npm:@angular/forms/fesm2022/forms.mjs', 'jszip': 'npm:jszip/dist/jszip.min.js', 'typescript': 'npm:typescript/lib/typescript.js', 'ts': './plugin.js', 'tslib': 'npm:tslib/tslib.js', 'css': 'npm:systemjs-plugin-css/css.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build':'npm:systemjs-plugin-babel/systemjs-babel-browser.js', '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-angular': 'npm:@mescius/spread-sheets-angular/fesm2020/mescius-spread-sheets-angular.mjs', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'ts' }, rxjs: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' }, "node_modules/@angular": { defaultExtension: 'mjs' }, "@mescius/spread-sheets-angular": { defaultExtension: 'mjs' }, '@angular/core': { defaultExtension: 'mjs', main: 'core.mjs' } } }); })(this);