你必须创建一个单元格级别绑定的数据源, 然后使用 setBindingPath 方法来设置绑定路径到指定的单元格,并将数据源设置到表单上。例如:
你可以使用 getBindingPath 方法来获取指定单元格的绑定信息。
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
<gc-worksheet>
<gc-column :width=120></gc-column>
<gc-column :width=120></gc-column>
<gc-column :width=120></gc-column>
</gc-worksheet>
</gc-spread-sheets>
<div class="options-container">
<div class="option-row">
<label style="background-color:#F4F8EB;">Select one of the gray cells to get its binding. You can also
change the Person object below to see it change in the Spread instance: change any of the text in the
text boxes below.</label>
</div>
<div class="option-row">
<label>Name: </label>
<input type="text" v-on:keyup="updateName" :value="name" />
<label>Age: </label>
<input type="text" v-on:keyup="updateAge" :value="age" />
<label>Sex: </label>
<input type="text" v-on:keyup="updateSex" :value="sex" />
<label>Postcode: </label>
<input type="text" v-on:keyup="updatePostcode" :value="postcode" />
<label>GetBindingPath: </label>
<label style="color:darkgreen">{{path}}</label>
</div>
</div>
</div>
</template>
<script>
import Vue from "vue";
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
import '@grapecity-software/spread-sheets-vue';
import GC from '@grapecity-software/spread-sheets';
import './styles.css';
let App = Vue.extend({
name: "app",
data() {
return {
name: 'Wang Feng',
age: 25,
sex: 'Male',
path: "",
postcode: '710075',
spread: null
};
},
methods: {
updateName(value) {
if (person && sheet) {
person.name = value.target.value;
sheet.repaint();
}
},
updateAge(value) {
if (person && sheet) {
person.age = isNaN(parseInt(value.target.value)) ? undefined : parseInt(value.target.value);
sheet.repaint();
}
},
updateSex(value) {
if (person && sheet) {
person.sex = value.target.value;
sheet.repaint();
}
},
updatePostcode(value) {
if (person && sheet) {
person.address.postcode = value.target.value;
sheet.repaint();
}
},
initSpread(spread) {
this.spread = spread;
let spreadNS = GC.Spread.Sheets;
let self = this;
sheet = spread.sheets[0];
sheet.suspendPaint();
sheet.setValue(0, 0, 'cell-binding');
sheet.setValue(2, 1, 'Name');
sheet.setValue(3, 1, 'Age');
sheet.setValue(4, 1, 'Sex');
sheet.setValue(5, 1, 'Address.postcode');
let source = new spreadNS.Bindings.CellBindingSource(person);
sheet.setBindingPath(2, 2, "name");
sheet.setBindingPath(3, 2, "age");
sheet.setBindingPath(4, 2, "sex");
sheet.setBindingPath(5, 2, "address.postcode");
sheet.setDataSource(source);
sheet.setSelection(2, 2, 1, 1);
let path = sheet.getBindingPath(2, 2);
sheet.getRange(2, 2, 4, 1).backColor("rgb(208,206,206)");
this.path = path || "";
sheet.bind(spreadNS.Events.SelectionChanged, function() {
let activeCell = sheet.getSelections()[0];
let path = sheet.getBindingPath(activeCell.row, activeCell.col);
self.path = path || "";
});
sheet.bind(spreadNS.Events.CellChanged, function() {
self.age = person.age || 0;
self.name = person.name || "";
self.postcode = person.address.postcode || "";
self.sex = person.sex || "";
});
sheet.resumePaint();
}
}
});
let person = {
name: "Wang Feng",
age: 25,
sex: "Male",
address: {
postcode: "710075"
}
},
sheet;
new Vue({
render: h => h(App)
}).$mount('#app');
</script>
<!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$/zh/vue/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- SystemJS -->
<script src="$DEMOROOT$/zh/vue/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('./src/app.vue');
System.import('$DEMOROOT$/zh/lib/vue/license.js');
</script>
</head>
<body>
<div id="app" style="height: 100%;"></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;
}
input[type=text] {
margin-top: 6px;
margin-bottom: 6px;
display: block;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
(function(global) {
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
meta: {
'*.css': { loader: 'css' },
'*.vue': { loader: 'vue-loader' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'@grapecity-software/spread-sheets': 'npm:@grapecity-software/spread-sheets/index.js',
'@grapecity-software/spread-sheets-resources-zh': 'npm:@grapecity-software/spread-sheets-resources-zh/index.js',
'@grapecity-software/spread-sheets-vue': 'npm:@grapecity-software/spread-sheets-vue/index.js',
'@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/jsob-test-dependency-package/react-components/index.js',
'jszip': 'npm:jszip/dist/jszip.js',
'css': 'npm:systemjs-plugin-css/css.js',
'vue': 'npm:vue/dist/vue.min.js',
'vue-loader': 'npm:systemjs-vue-browser/index.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'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
}
}
});
})(this);