概述
本 Demo 展示了如何在工作表的行和列标题末尾显示添加按钮,并通过界面控件动态调整按钮的可见性、尺寸、工具提示和样式。Demo 还演示了如何为按钮注册自定义命令,在保留默认添加行为的基础上扩展自定义逻辑。
实现思路
初始化工作表,设置初始行列数(10 行 10 列)
显示行和列的添加按钮,设置 visible 属性为 true
为按钮注册自定义命令,保存默认的添加行为并在自定义命令中调用
绑定界面控件的事件监听器,实现动态修改按钮属性
通过复选框控制按钮可见性,通过输入框修改尺寸、工具提示和背景颜色
代码解析
显示添加按钮并设置初始状态
这段代码在初始化时显示了行和列的添加按钮。addRowButtonOption 和 addColumnButtonOption 分别用于配置行按钮和列按钮,visible 属性控制按钮是否显示。
注册自定义命令
这段代码为行按钮注册了自定义命令。首先保存按钮原有的默认命令(addRowCommand),然后注册新命令 clickRowButtonToDo,在执行时调用原有的默认命令。这样可以在保留默认添加行功能的基础上,扩展自定义逻辑。
动态修改按钮属性
这些代码演示了如何动态修改按钮的可见性、宽度和样式。每次修改属性后,都需要调用 spread.repaint() 重绘表格以应用更改。
运行效果
工作表的行标题底部和列标题右侧会显示添加按钮
勾选或取消勾选"显示列添加按钮"复选框,可以控制列添加按钮的显示与隐藏
勾选或取消勾选"显示行添加按钮"复选框,可以控制行添加按钮的显示与隐藏
在"列按钮宽度"输入框中输入数值,列添加按钮的宽度会相应改变
在"行按钮高度"输入框中输入数值,行添加按钮的高度会相应改变
在"列按钮提示"和"行按钮提示"输入框中输入文本,鼠标悬停在按钮上时会显示提示信息
使用颜色选择器可以更改按钮的背景颜色
点击添加按钮会添加新的行或列
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
var spreadNS = GC.Spread.Sheets;
var sheet = spread.getSheet(0);
sheet.suspendPaint();
sheet.options.addRowButtonOption.visible = true;
sheet.options.addColumnButtonOption.visible = true;
sheet.setRowCount(10);
sheet.setColumnCount(10);
sheet.resumePaint();
/*
* Show or hide the add row/column button.
*/
_getElementById("columnBtnVisible").addEventListener('click',function () {
var sheet = spread.getActiveSheet();
sheet.options.addColumnButtonOption.visible = this.checked;
spread.repaint();
});
_getElementById("rowBtnVisible").addEventListener('click',function () {
var sheet = spread.getActiveSheet();
sheet.options.addRowButtonOption.visible = this.checked;
spread.repaint();
});
/*
* Update the width/height of the add row/column button.
*/
_getElementById("columnBtnWidth").addEventListener('change',function () {
var sheet = spread.getActiveSheet();
sheet.options.addColumnButtonOption.width = parseInt(this.value);
spread.repaint();
});
_getElementById("rowBtnHeight").addEventListener('change',function () {
var sheet = spread.getActiveSheet();
sheet.options.addRowButtonOption.height = parseInt(this.value);
spread.repaint();
});
/*
* Update the tooltip of the add row/column button.
*/
_getElementById("columnBtnTooltip").addEventListener('input',function () {
var sheet = spread.getActiveSheet();
sheet.options.addColumnButtonOption.tooltip = this.value;
spread.repaint();
});
_getElementById("rowBtnTooltip").addEventListener('input',function () {
var sheet = spread.getActiveSheet();
sheet.options.addRowButtonOption.tooltip = this.value;
spread.repaint();
});
/*
* Update the style of the add row/column button.
*/
_getElementById("columnBtnBackColor").addEventListener('input',function () {
var sheet = spread.getActiveSheet();
var style = new GC.Spread.Sheets.Style();
style.backColor = this.value;
style.vAlign = GC.Spread.Sheets.VerticalAlign.center;
style.hAlign = GC.Spread.Sheets.HorizontalAlign.center;
sheet.options.addColumnButtonOption.style = style;
spread.repaint();
});
_getElementById("rowBtnBackColor").addEventListener('input',function () {
var sheet = spread.getActiveSheet();
var style = new GC.Spread.Sheets.Style();
style.backColor = this.value;
style.vAlign = GC.Spread.Sheets.VerticalAlign.center;
style.hAlign = GC.Spread.Sheets.HorizontalAlign.center;
sheet.options.addRowButtonOption.style = style;
spread.repaint();
});
// Define the command
const addRowCommand = sheet.options.addRowButtonOption.command;
spread.commandManager().register('clickRowButtonToDo', {
canUndo: false,
execute: function (context, options) {
// the self-defined command
options.cmd = addRowCommand;
spread.commandManager().execute(options);
}
});
sheet.options.addRowButtonOption.command = 'clickRowButtonToDo';
const addColumnCommand = sheet.options.addColumnButtonOption.command;
spread.commandManager().register('clickColumnButtonToDo', {
canUndo: false,
execute: function (context, options) {
// the self-defined command
options.cmd = addColumnCommand;
spread.commandManager().execute(options);
}
});
sheet.options.addColumnButtonOption.command = 'clickColumnButtonToDo';
};
function _getElementById(id) {
return document.getElementById(id);
}
<!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" />
<meta name="spreadjs culture" content="zh-cn" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.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">
<label>设置以下选项以显示添加列/行按钮。</label>
<div class="option-row">
<input type="checkbox" id="columnBtnVisible" checked />
<label class="checkbox-label" for="columnBtnVisible">添加列按钮可见</label>
</div>
<div class="option-row">
<input type="checkbox" id="rowBtnVisible" checked />
<label class="checkbox-label" for="rowBtnVisible">添加行按钮可见</label>
</div>
<hr>
<div class="option-row">
<label >设置添加列/行按钮的宽度/高度。</label>
</div>
<div class="option-row">
<label for="columnBtnWidth">添加列按钮宽度</label>
<input type="number" value="62" id="columnBtnWidth" />
<label for="rowBtnHeight">添加行按钮高度</label>
<input type="number" value="20" id="rowBtnHeight" />
</div>
<hr>
<div class="option-row">
<label >设置添加列/行按钮的工具提示。</label>
</div>
<div class="option-row">
<label for="columnBtnTooltip">添加列按钮工具提示</label>
<input type="text" value="" id="columnBtnTooltip" />
<label for="rowBtnTooltip">添加行按钮工具提示</label>
<input type="text" value="" id="rowBtnTooltip" />
</div>
<hr>
<div class="option-row">
<label >设置添加列/行按钮的背景颜色。</label>
</div>
<div class="option-row">
<label for="columnBtnBackColor">添加列按钮背景颜色</label>
<input type="color" value="" id="columnBtnBackColor" />
<label for="rowBtnBackColor">添加行按钮背景颜色</label>
<input type="color" value="" id="rowBtnBackColor" />
</div>
</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;
}
label {
margin-bottom: 6px;
display: block;
margin-top: 10px;
}
.checkbox-label {
display: inline;
}
input[type=button] {
margin-top: 6px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}