你必须创建一个单元格级别绑定的数据源, 然后使用 setBindingPath 方法来设置绑定路径到指定的单元格,并将数据源设置到表单上。例如:
你可以使用 getBindingPath 方法来获取指定单元格的绑定信息。
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
import GC from '@grapecity-software/spread-sheets';
import Panel from './panel';
import { SpreadSheets, Worksheet, Column } from '@grapecity-software/spread-sheets-react';
import './styles.css';
const {useState, useEffect} = React;
let person= { name: "Wang Feng", age: 25, sex: "Male", address: { postCode: "710075" } }, sheet;
const App = () => {
const [name, setName] = useState('Wang Feng');
const [age, setAge] = useState(25);
const [sex, setSex] = useState('Male');
const [postCode, setPostCode] = useState('710075');
const [bindingPath, setBindingPath] = useState('');
useEffect(() => {
person.name = name;
person.age = age;
person.sex = sex;
person.address.postCode = postCode;
sheet.repaint();
}, [name, age, sex, postCode]);
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => initSpread(spread, setName, setAge, setSex, setPostCode, setBindingPath)}>
<Worksheet>
<Column width={120}></Column>
<Column width={120}></Column>
<Column width={120}></Column>
</Worksheet>
</SpreadSheets>
</div>
<Panel name={name} age={age} sex={sex} postCode={postCode} bindingPath={bindingPath}
setName={setName} setAge={setAge} setSex={setSex} setPostCode={setPostCode} setBindingPath={setBindingPath}
></Panel>
</div>
)
}
function initSpread(spread, setName, setAge, setSex, setPostCode, setBindingPath) {
let spreadNS = GC.Spread.Sheets;
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)");
setBindingPath(path || "");
sheet.bind(spreadNS.Events.SelectionChanged, function () {
let activeCell = sheet.getSelections()[0];
let path = sheet.getBindingPath(activeCell.row, activeCell.col);
setBindingPath(path || "");
});
sheet.bind(spreadNS.Events.CellChanged, function () {
setName(person.name || "");
setAge(person.age || "");
setSex(person.sex || "");
setPostCode(person.address.postCode || "");
});
sheet.resumePaint();
}
ReactDOM.render(<App />, document.getElementById('app'));
import * as React from 'react';
export default function Panel (props) {
const { name, setName, age, setAge, sex, setSex, postCode, setPostCode, bindingPath } = props;
return (
<div class="options-container">
<div class="option-row">
<label style={{ backgroundColor: '#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" onChange={(e) => { setName(e.target.value) }} value={name} />
<label>Age: </label>
<input type="text" onChange={(e) => { setAge(isNaN(parseInt(e.target.value))? undefined:parseInt(e.target.value)) }} value={age} />
<label>Sex: </label>
<input type="text" onChange={(e) => { setSex(e.target.value) }} value={sex} />
<label>Postcode: </label>
<input type="text" onChange={(e) => { setPostCode(e.target.value) }} value={postCode} />
<label>GetBindingPath: </label>
<label style={{ color: 'darkgreen' }}>{bindingPath}</label>
</div>
</div>
);
}
<!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/react/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- SystemJS -->
<script src="$DEMOROOT$/zh/react/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('$DEMOROOT$/zh/lib/react/license.js').then(function() {
System.import('./src/app');
});
</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,
react: true
},
meta: {
'*.css': { loader: 'css' }
},
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-react': 'npm:@grapecity-software/spread-sheets-react/index.js',
'@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/jsob-test-dependency-package/react-components/index.js',
'react': 'npm:react/umd/react.production.min.js',
'react-dom': 'npm:react-dom/umd/react-dom.production.min.js',
'css': 'npm:systemjs-plugin-css/css.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: 'jsx'
},
"node_modules": {
defaultExtension: 'js'
},
}
});
})(this);