数据绑定

你可以在表格的列中绑定名称,数据字段,格式,单元格类型和转换函数。

你可以通过使用 bind 方法来将表格绑定到有记录的字段,将表列绑定到数据的字段。当你设置数据源时,表格将会自动地绑定到对应的记录。 例如: SpreadJS 提供了绑定API来解决复杂的需求,你可以参考如下代码: 示例 1: 示例 2: 如果已经绑定了数据源,你可以获取到被修改过的数据行:
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 name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.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; }