添加 sheet.outlineColumn.options({columnIndex: index}), 让数据呈现树形结构。点击分组标记,该分组包含的行会被收起或者展开。
使用内置命令 increaseCellIndent 和 decreaseCellIndent 可以改变数据的分层结构。
增加单元格缩进命令: 使用键盘快捷键 'ctrl-atl-]' 增加单元格缩进和分组级别。
减少单元格缩进命令: 使用键盘快捷键 'ctrl-atl-[' 减少单元格缩进和分组级别。
使用 expandIndicator 或者 collapseIndicator 选项来定制展开收起的标记。
使用 images 和 showImage 选项来定制不同级别的图标。
使用 showCheckBox 选项来定制复选框是否可见。选中或者取消选中某个单元格中的复选框也会影响它的子单元格的复选框的选中状态。
使用 maxLevel 选项来控制数据分层级别。默认值是10。
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 {SpreadSheets, Worksheet, Column} from '@grapecity-software/spread-sheets-react';
import GC from '@grapecity-software/spread-sheets';
import './styles.css';
const Component = React.Component, useState = React.useState, spreadNS = GC.Spread.Sheets;
function _getElementById(id) {
return document.getElementById(id);
}
function loadData(sheet) {
let sd = data;
sheet.setDataSource(sd);
sheet.setColumnCount(6);
sheet.setColumnWidth(0, 150);
sheet.setColumnWidth(1, 350);
sheet.setColumnWidth(2, 150);
sheet.setColumnWidth(3, 150);
sheet.setColumnWidth(4, 150);
sheet.setColumnWidth(5, 150);
for (var r = 0; r < sd.length; r++) {
var level = sd[r].level;
if(level==0)
{
sheet.getRange(r,0,1,7).backColor("#CCCCCC");
}
else if(level==1)
{
sheet.getRange(r,0,1,7).backColor("#EEEEEE");
}
sheet.getCell(r, 0).textIndent(level);
}
}
function initOutlineColumn(sheet) {
sheet.outlineColumn.options({
columnIndex: 0,
showImage: true,
showCheckBox: true,
images: ['$DEMOROOT$/spread/source/images/task-1.png', '$DEMOROOT$/spread/source/images/task-2.png', '$DEMOROOT$/spread/source/images/task-3.png'],
expandIndicator: '$DEMOROOT$/spread/source/images/increaseIndicator.png',
collapseIndicator: '$DEMOROOT$/spread/source/images/decreaseIndicator.png',
maxLevel: 2
});
sheet.showRowOutline(false);
sheet.outlineColumn.refresh();
}
function setOutlineColumnOptions(sheet) {
let options = sheet.outlineColumn.options();
}
class App extends Component {
constructor(props) {
super(props);
this.spread = null;
this.state = {
maxLevel: 2,
image1: "/spreadjs/demos/spread/source/images/task-1.png",
image2: "/spreadjs/demos/spread/source/images/task-2.png",
image3: "/spreadjs/demos/spread/source/images/task-2.png",
indicator1: "/spreadjs/demos/spread/source/images/increaseIndicator.png",
indicator2: "/spreadjs/demos/spread/source/images/decreaseIndicator.png",
customIndicator:true
};
}
initSpread(spread) {
this.spread = spread;
let sheet = spread.getActiveSheet();
sheet.suspendPaint();
loadData(sheet);
initOutlineColumn(sheet);
setOutlineColumnOptions(sheet);
sheet.getRange(0,3,25,3).hAlign(GC.Spread.Sheets.HorizontalAlign.center);
sheet.resumePaint();
let options = sheet.outlineColumn.options();
this.setState({maxLevel: options.maxLevel});
this.setState({image1: options.images[0]});
this.setState({image2: options.images[1]});
this.setState({image3: options.images[2]});
this.setState({indicator1: options.expandIndicator});
this.setState({indicator2: options.collapseIndicator});
}
render() {
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => this.initSpread(spread)}>
<Worksheet>
</Worksheet>
</SpreadSheets>
</div>
<Panel
maxLevel={this.state.maxLevel}
image1={this.state.image1}
image2={this.state.image2}
image3={this.state.image3}
indicator1={this.state.indicator1}
indicator2={this.state.indicator2}
showIndicator={(e) => this.showIndicator(e)}
showCheckBox={(e) => this.showCheckBox(e)}
selectImage1={(e) => this.selectImage1(e)}
selectImage2={(e) => this.selectImage2(e)}
selectImage3={(e) => this.selectImage3(e)}
selectIndicator1={(e) => this.selectIndicator1(e)}
selectIndicator2={(e) => this.selectIndicator2(e)}
showImage={(e) => this.showImage(e)}
customIndicator={(indicatorsInfo) => this.customIndicator(indicatorsInfo)}
setCustomIndicator={(indicatorsInfo) => this.setCustomIndicator(indicatorsInfo)}
setMaxLevel={(maxLevel) => this.setMaxLevel(maxLevel)}
setImages={(images) => this.setImages(images)}
customIndicatorState={this.state.customIndicator}
></Panel>
</div>
);
}
showIndicator(e) {
let spread = this.spread;
let sheet = spread.getActiveSheet();
sheet.outlineColumn.options().showIndicator = e.target.checked;
sheet.outlineColumn.refresh();
}
showCheckBox(e) {
let spread = this.spread;
let sheet = spread.getActiveSheet();
sheet.outlineColumn.options().showCheckBox = e.target.checked;
sheet.outlineColumn.refresh();
}
showImage(e) {
let spread = this.spread;
let sheet = spread.getActiveSheet();
sheet.outlineColumn.options().showImage = e.target.checked;
sheet.outlineColumn.refresh();
}
customIndicator(indicatorsInfo) {
let spread = this.spread;
let sheet = spread.getActiveSheet();
this.setState({
customIndicator: indicatorsInfo
})
if (!indicatorsInfo) {
sheet.outlineColumn.options().expandIndicator = null;
sheet.outlineColumn.options().collapseIndicator = null;
sheet.outlineColumn.refresh();
} else {
sheet.outlineColumn.options().expandIndicator = this.state.indicator1;
sheet.outlineColumn.options().collapseIndicator = this.state.indicator2;
sheet.outlineColumn.refresh();
}
}
setCustomIndicator() {
let spread = this.spread;
let sheet = spread.getActiveSheet();
if(this.state.customIndicator)
{
sheet.outlineColumn.options().expandIndicator = this.state.indicator1;
sheet.outlineColumn.options().collapseIndicator = this.state.indicator2;
sheet.outlineColumn.refresh();
}
}
setMaxLevel(maxLevel) {
let spread = this.spread;
let sheet = spread.getActiveSheet();
sheet.outlineColumn.options().maxLevel = parseInt(this.state.maxLevel);
sheet.outlineColumn.refresh();
}
setImages() {
let spread = this.spread;
let sheet = spread.getActiveSheet();
sheet.outlineColumn.options().images = [
this.state.image1,
this.state.image2,
this.state.image3
];
sheet.outlineColumn.refresh();
}
selectImage1(e) {
let file = e.target.files[0];
if (!/image\/\w+/.test(file.type)) {
alert('The file must be an image!');
return false;
}
let self = this;
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
self.state.image1 = this.result;
};
}
selectImage2(e) {
let file = e.target.files[0];
if (!/image\/\w+/.test(file.type)) {
alert('The file must be an image!');
return false;
}
let self = this;
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
self.state.image2 = this.result;
};
}
selectImage3(e) {
let file = e.target.files[0];
if (!/image\/\w+/.test(file.type)) {
alert('The file must be an image!');
return false;
}
let self = this;
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
self.state.image3 = this.result;
};
}
selectIndicator1(e) {
let file = e.target.files[0];
if (!/image\/\w+/.test(file.type)) {
alert('The file must be an image!');
return false;
}
let self = this;
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
self.state.indicator1 = this.result;
};
}
selectIndicator2(e) {
let file = e.target.files[0];
if (!/image\/\w+/.test(file.type)) {
alert('The file must be an image!');
return false;
}
let self = this;
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
self.state.indicator2 = this.result;
};
}
}
function CheckBoxInput(props) {
const [checked, setChecked] = useState(props.checked);
return (
<input type="checkbox" id={props.id} checked={checked} onChange={(e) => {
setChecked(e.target.checked);
props.onChange(e)
}}/>
);
}
function TextInput(props) {
const [value, setValue] = useState(props.value);
return (
<input type="text" id={props.id} value={value} onChange={(e) => {
setValue(e.target.value);
props.onChange(e)
}}/>
);
}
class Panel extends Component {
constructor(props) {
super(props);
}
render() {
let props = this.props;
return (
<div className="options-container">
<div className="options-row">
<label>OutlineColumn Options:</label>
</div>
<hr />
<div className="options-row">
<CheckBoxInput id="showIndicator" checked={true} onChange={props.showIndicator}></CheckBoxInput>
<label htmlFor="showIndicator" className="inlineLabel">Show Indicator</label>
</div>
<div className="options-row">
<CheckBoxInput id="showCheckBox" checked={true} onChange={props.showCheckBox}></CheckBoxInput>
<label htmlFor="showCheckBox" className="inlineLabel">Show CheckBox</label>
</div>
<div className="options-row">
<CheckBoxInput id="showImage" checked={true} onChange={props.showImage}></CheckBoxInput>
<label htmlFor="showImage" className="inlineLabel">Show Image</label>
</div>
<div className="options-row">
<CheckBoxInput id="customIndicator" checked={true} onChange={(e) => {
props.customIndicator(e.target.checked);
}}></CheckBoxInput>
<label htmlFor="customIndicator" style={{width: "auto"}} className="inlineLabel">Allow Custome Indicator</label>
</div>
<div className="options-row">
<label htmlFor="maxLevel">Max Level:</label>
<TextInput id="maxLevel" value={props.maxLevel} onChange={(e) => {
this.maxLevel = e.target.value
}}></TextInput>
</div>
<div className="options-row">
<input type="button" value="Set" onClick={() => {
props.setMaxLevel(this.maxLevel)
}}/>
</div>
<div className="options-row">
<label>Custom Image Settings:</label>
</div>
<hr />
<div className="options-row">
<label htmlFor="img1">Label 1 image:</label>
<input type="file" name="img1" onChange={e => props.selectImage1(e)} />
</div>
<div className="options-row">
<label htmlFor="img2">Label 2 image:</label>
<input type="file" name="img2" onChange={e => props.selectImage2(e)} />
</div>
<div className="options-row">
<label htmlFor="img3">Label 3 image:</label>
<input type="file" name="img3" onChange={e => props.selectImage3(e)} />
</div>
<div className="options-row">
<input type="button" value="Set" onClick={() => {props.setImages()}}/>
</div>
<div className="options-row">
<label>Custom Indicator Image Settings:</label>
</div>
<hr />
<div className="options-row">
<label htmlFor="ind1">Expanded Image:</label>
<input type="file" name="ind1" onChange={e => props.selectIndicator1(e)} disabled={!props.customIndicatorState}/>
</div>
<div className="options-row">
<label htmlFor="ind2">Collapsed Image:</label>
<input type="file" name="ind2" onChange={e => props.selectIndicator2(e)} disabled={!props.customIndicatorState}/>
</div>
<div className="options-row">
<input type="button" value="Set" onClick={() => {
props.setCustomIndicator()
}} disabled={!props.customIndicatorState}/>
</div>
</div>
);
}
}
ReactDOM.render(<App/>, _getElementById('app'));
<!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">
<script src="$DEMOROOT$/spread/source/data/outlineColumn.js" type="text/javascript"></script>
<!-- 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"></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;
}
.options-row {
font-size: 14px;
padding: 5px;
/*margin-top: 10px;*/
}
input {
display: inline-block;
}
input[type="text"] {
padding: 4px 6px;
width: 100%;
margin-bottom: 10px;
}
input[type="button"] {
padding: 4px 6px;
width: 100%;
margin-bottom: 10px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
#app {
height: 100%;
}
.inlineLabel {
display: inline-block;
}
#maxLevel {
width:90px;
}
(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);