Data Binding

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

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:
var spreadNS = GC.Spread.Sheets; window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss")); initSpread(spread); }; function initSpread(spread) { var sheet = spread.getActiveSheet(); var spreadNS = GC.Spread.Sheets; var data = { name: 'Jones', region: 'East', sales: [ {orderDate: '1/6/2013', item: 'Pencil', units: 95, cost: 1.99, isDelivered: true}, {orderDate: '4/1/2013', item: 'Binder', units: 60, cost: 4.99, isDelivered: false}, {orderDate: '6/8/2013', item: 'Pen Set', units: 16, cost: 15.99, isDelivered: false} ] }; var convert = function (item) { return item['cost'] + '$'; } var table = sheet.tables.add('tableSales', 0, 0, 5, 5); table.style(spreadNS.Tables.TableThemes["medium4"]); var tableColumn1 = new spreadNS.Tables.TableColumn(1, "orderDate", "Order Date", "yyyy-mm-dd"); 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, "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); document.getElementById("getState").addEventListener('click',function () { var table = sheet.tables.findByName("tableSales"); if (table) { var dataArr = table.getDirtyRows(); var str = ""; for (var i = 0; i < dataArr.length; i ++) { str += dataArr[i].row + ", "; } document.getElementById("stateText").value = str; } }); }
<!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/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> <div class="options-container"> <div class="option-row"> <label style="background-color:#F4F8EB;">Change table data and click button to get the dirty rows.</label> </div> <input type="button" id="getState" value="Get Dirty Rows"/> <textarea id="stateText" style="width: 100%;height: 30px;margin-top:10px"></textarea> </div> </div></body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } .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; }