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:
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
</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" v-bind:value="dirtyRowsValue"></textarea>
</div>
</div>
</template>
<script setup>
import '@mescius/spread-sheets-vue';
import { ref } from "vue";
import GC from "@mescius/spread-sheets";
const GCsheets = GC.Spread.Sheets;
const spreadRef = ref(null);
const tableRef = ref(null);
const dirtyRowsValue = ref(null);
const initSpread = (spread) => {
spreadRef.value = spread;
spread.suspendPaint();
let sheet = spread.getActiveSheet();
let 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 }
]
};
let convert = function (item) {
return item['cost'] + '$';
}
let table = sheet.tables.add('tableSales', 0, 0, 5, 5);
tableRef.value = 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();
}
const onGetDirtyRowsClickEvent = (e) => {
let table = tableRef.value;
if (table) {
let dataArr = table.getDirtyRows();
let str = "";
for (let i = 0; i < dataArr.length; i++) {
str += dataArr[i].row + ", ";
}
dirtyRowsValue.value = str;
}
}
</script>
<style scoped>
#app {
height: 100%;
}
.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;
}
.custom-label {
background-color:#F4F8EB;
}
.state-text {
width: 100%;
height: 30px;
margin-top:10px;
}
</style>
<!DOCTYPE html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>SpreadJS VUE</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/en/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/en/vue3/node_modules/systemjs/dist/system.src.js"></script>
<script src="./systemjs.config.js"></script>
<script src="./compiler.js" type="module"></script>
<script>
var System = SystemJS;
System.import("./src/app.js");
System.import('$DEMOROOT$/en/lib/vue3/license.js');
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>
(function (global) {
SystemJS.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
packageConfigPaths: [
'./node_modules/*/package.json',
"./node_modules/@mescius/*/package.json",
"./node_modules/@babel/*/package.json",
"./node_modules/@vue/*/package.json"
],
map: {
'vue': "npm:vue/dist/vue.esm-browser.js",
'tiny-emitter': 'npm:tiny-emitter/index.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-vue': 'npm:@mescius/spread-sheets-vue/index.js'
},
meta: {
'*.css': { loader: 'systemjs-plugin-css' },
'*.vue': { loader: "../plugin-vue/index.js" }
}
});
})(this);