Data Binding

This sample shows how you can bind the name, dataField, formatter, cellType, and convert function inside of a table column.

<p>Use the <strong>bind</strong> 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:</p> <pre><code class="hljs js language-js"> table.bind(tableColumns, <span class="hljs-string">'sales'</span>, data); </code></pre> <p>We provide the bind API to solve complicated call issue; you can use it as shown in the below code:<br /> Example 1:</p> <pre><code class="hljs js language-js"> <span class="hljs-keyword">var</span> data = { <span class="hljs-attr">name</span>: <span class="hljs-string">'Jones'</span>, <span class="hljs-attr">region</span>: <span class="hljs-string">'East'</span>, <span class="hljs-attr">sales</span>: [ {<span class="hljs-attr">orderDate</span>: <span class="hljs-string">'1/6/2013'</span>, <span class="hljs-attr">item</span>: <span class="hljs-string">'Pencil'</span>, <span class="hljs-attr">units</span>: <span class="hljs-number">95</span>, <span class="hljs-attr">cost</span>: <span class="hljs-number">1.99</span>, <span class="hljs-attr">isMakeMoney</span>: <span class="hljs-literal">true</span>}, {<span class="hljs-attr">orderDate</span>: <span class="hljs-string">'4/1/2013'</span>, <span class="hljs-attr">item</span>: <span class="hljs-string">'Binder'</span>, <span class="hljs-attr">units</span>: <span class="hljs-number">60</span>, <span class="hljs-attr">cost</span>: <span class="hljs-number">4.99</span>, <span class="hljs-attr">isMakeMoney</span>: <span class="hljs-literal">false</span>}, {<span class="hljs-attr">orderDate</span>: <span class="hljs-string">'6/8/2013'</span>, <span class="hljs-attr">item</span>: <span class="hljs-string">'Pen Set'</span>, <span class="hljs-attr">units</span>: <span class="hljs-number">16</span>, <span class="hljs-attr">cost</span>: <span class="hljs-number">15.99</span>, <span class="hljs-attr">isMakeMoney</span>: <span class="hljs-literal">false</span>} ] }; <span class="hljs-keyword">var</span> convert = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">item</span>) </span>{ <span class="hljs-keyword">return</span> item[<span class="hljs-string">'cost'</span>] + <span class="hljs-string">'$'</span>; } <span class="hljs-keyword">var</span> table = sheet.tables.add(<span class="hljs-string">'tableSales'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>); <span class="hljs-keyword">var</span> tableColumn1 = <span class="hljs-keyword">new</span> spreadNS.Tables.TableColumn(); tableColumn1.name(<span class="hljs-string">"Order Date"</span>); tableColumn1.dataField(<span class="hljs-string">"orderDate"</span>); tableColumn1.formatter(<span class="hljs-string">"d/M/yy"</span>); <span class="hljs-keyword">var</span> tableColumn2 = <span class="hljs-keyword">new</span> spreadNS.Tables.TableColumn(); tableColumn2.name(<span class="hljs-string">"Item"</span>); tableColumn2.dataField(<span class="hljs-string">"item"</span>); <span class="hljs-keyword">var</span> tableColumn3 = <span class="hljs-keyword">new</span> spreadNS.Tables.TableColumn(); tableColumn3.name(<span class="hljs-string">"Units"</span>); tableColumn3.dataField(<span class="hljs-string">"units"</span>); <span class="hljs-keyword">var</span> tableColumn4 = <span class="hljs-keyword">new</span> spreadNS.Tables.TableColumn(); tableColumn4.name(<span class="hljs-string">"Cost"</span>); tableColumn4.dataField(<span class="hljs-string">"cost"</span>); tableColumn4.value(convert); <span class="hljs-keyword">var</span> tableColumn5 = <span class="hljs-keyword">new</span> spreadNS.Tables.TableColumn(); tableColumn5.name(<span class="hljs-string">"IsMakeMoney"</span>); tableColumn5.dataField(<span class="hljs-string">"isMakeMoney"</span>); tableColumn5.cellType(<span class="hljs-keyword">new</span> GC.Spread.Sheets.CellTypes.CheckBox()); table.autoGenerateColumns(<span class="hljs-literal">false</span>); table.bind([tableColumn1, tableColumn2, tableColumn3, tableColumn4, tableColumn5], <span class="hljs-string">'sales'</span>, data); </code></pre> <p>Example 2:</p> <pre><code class="hljs js language-js"> <span class="hljs-keyword">var</span> data = { <span class="hljs-attr">name</span>: <span class="hljs-string">'Jones'</span>, <span class="hljs-attr">region</span>: <span class="hljs-string">'East'</span>, <span class="hljs-attr">sales</span>: [ {<span class="hljs-attr">orderDate</span>: <span class="hljs-string">'1/6/2013'</span>, <span class="hljs-attr">item</span>: <span class="hljs-string">'Pencil'</span>, <span class="hljs-attr">units</span>: <span class="hljs-number">95</span>, <span class="hljs-attr">cost</span>: <span class="hljs-number">1.99</span>, <span class="hljs-attr">isMakeMoney</span>: <span class="hljs-literal">true</span>}, {<span class="hljs-attr">orderDate</span>: <span class="hljs-string">'4/1/2013'</span>, <span class="hljs-attr">item</span>: <span class="hljs-string">'Binder'</span>, <span class="hljs-attr">units</span>: <span class="hljs-number">60</span>, <span class="hljs-attr">cost</span>: <span class="hljs-number">4.99</span>, <span class="hljs-attr">isMakeMoney</span>: <span class="hljs-literal">false</span>}, {<span class="hljs-attr">orderDate</span>: <span class="hljs-string">'6/8/2013'</span>, <span class="hljs-attr">item</span>: <span class="hljs-string">'Pen Set'</span>, <span class="hljs-attr">units</span>: <span class="hljs-number">16</span>, <span class="hljs-attr">cost</span>: <span class="hljs-number">15.99</span>, <span class="hljs-attr">isMakeMoney</span>: <span class="hljs-literal">false</span>} ] }; <span class="hljs-keyword">var</span> convert = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">item</span>) </span>{ <span class="hljs-keyword">return</span> item[<span class="hljs-string">'cost'</span>] + <span class="hljs-string">'$'</span>; } <span class="hljs-keyword">var</span> table = sheet.tables.add(<span class="hljs-string">'tableSales'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>, <span class="hljs-number">5</span>); <span class="hljs-keyword">var</span> tableColumn1 = <span class="hljs-keyword">new</span> spreadNS.Tables.TableColumn(<span class="hljs-number">1</span>, <span class="hljs-string">"orderDate"</span>, <span class="hljs-string">"Order Date"</span>, <span class="hljs-string">"d/M/yy"</span>); <span class="hljs-keyword">var</span> tableColumn2 = <span class="hljs-keyword">new</span> spreadNS.Tables.TableColumn(<span class="hljs-number">2</span>, <span class="hljs-string">"item"</span>, <span class="hljs-string">"Item"</span>); <span class="hljs-keyword">var</span> tableColumn3 = <span class="hljs-keyword">new</span> spreadNS.Tables.TableColumn(<span class="hljs-number">3</span>, <span class="hljs-string">"units"</span>, <span class="hljs-string">"Units"</span>); <span class="hljs-keyword">var</span> tableColumn4 = <span class="hljs-keyword">new</span> spreadNS.Tables.TableColumn(<span class="hljs-number">4</span>, <span class="hljs-string">"cost"</span>, <span class="hljs-string">"Cost"</span>, <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span>, convert); <span class="hljs-keyword">var</span> tableColumn5 = <span class="hljs-keyword">new</span> spreadNS.Tables.TableColumn(<span class="hljs-number">5</span>, <span class="hljs-string">"isMakeMoney"</span>, <span class="hljs-string">"IsMakeMoney"</span>, <span class="hljs-literal">null</span>, <span class="hljs-keyword">new</span> GC.Spread.Sheets.CellTypes.CheckBox()); table.autoGenerateColumns(<span class="hljs-literal">false</span>); table.bind([tableColumn1, tableColumn2, tableColumn3, tableColumn4, tableColumn5], <span class="hljs-string">'sales'</span>, data); </code></pre> <p>When you have already bound the data source, you can get the dirty table rows if you change the data source value:</p> <pre><code class="hljs js language-js"> <span class="hljs-keyword">var</span> dirtyRows = table.getDirtyRows(); </code></pre>
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, 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); 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/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/purejs/node_modules/@grapecity/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:lavender;">Change table data and click button 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; }