概述
本 Demo 演示如何把 DataManager 表绑定到工作表单元格。左侧工作表显示员工档案和紧急联系人,右侧记录列表用于切换当前员工;切换后,所有绑定字段会自动更新。
实现思路
准备员工数组,每条记录包含基本信息和 emergencyContacts 子数组。
用 DataManager 创建数据表,并包装成 CellBindingSource。
在工作表中搭建员工档案布局,并给单元格或表格设置绑定路径。
右侧记录列表点击后更新 activeRecordIndex。
同步右侧列表的高亮状态和当前记录说明。
代码解析
这段代码把 DataManager 表接到工作表数据源上。后续只要改变 activeRecordIndex,绑定到当前记录的单元格就会显示对应员工。
单元格使用字段名绑定员工基本信息,表格使用 emergencyContacts 绑定子数组,用来显示当前员工的紧急联系人。
右侧记录按钮最终调用这个函数,更新绑定源的活动记录,并刷新面板中的选中状态。
运行效果
工作表左侧显示员工档案卡片。
右侧记录列表显示员工编号、姓名、部门和所在地。
点击不同记录后,工作表中的员工信息和紧急联系人表会同步切换。
当前记录会在右侧面板中高亮,并显示当前员工标签。
API 参考
spread.dataManager().addTable(...):创建 DataManager 数据表。
GC.Spread.Sheets.Bindings.CellBindingSource:把数据表包装成单元格绑定源。
sheet.setDataSource(...):将绑定源设置到工作表。
sheet.setBindingPath(...):把单元格绑定到当前记录字段。
bindingSource.activeRecordIndex(...):读取或设置当前活动记录。
var spreadNS = GC.Spread.Sheets;
var spreadInstance;
var bindingSource;
var activeRecordIndex = 0;
var data = [
{
"empId": "EMP-001",
"fullName": "莎拉·詹金斯",
"department": "工程部",
"location": "美国旧金山",
"email": "sarah.j@company.com",
"emergencyContacts": [
{ "name": "大卫·詹金斯", "relationship": "配偶", "phone": "+1 (555) 019-2834" },
{ "name": "玛丽·詹金斯", "relationship": "母亲", "phone": "+1 (555) 019-5821" }
]
},
{
"empId": "EMP-002",
"fullName": "田中由纪",
"department": "产品设计部",
"location": "日本东京",
"email": "yuki.t@company.com",
"emergencyContacts": [
{ "name": "田中健二", "relationship": "兄弟", "phone": "+81 90-1234-5678" },
{ "name": "田中千代", "relationship": "母亲", "phone": "+81 90-8765-4321" }
]
},
{
"empId": "EMP-003",
"fullName": "卡洛斯·席尔瓦",
"department": "全球销售部",
"location": "巴西圣保罗",
"email": "carlos.s@company.com",
"emergencyContacts": [
{ "name": "安娜·席尔瓦", "relationship": "配偶", "phone": "+55 11 98765-4321" },
{ "name": "罗伯托·席尔瓦", "relationship": "父亲", "phone": "+55 11 91234-5678" }
]
},
{
"empId": "EMP-004",
"fullName": "阿玛拉·迪亚洛",
"department": "人力资源部",
"location": "英国伦敦",
"email": "amara.d@company.com",
"emergencyContacts": [
{ "name": "易卜拉欣·迪亚洛", "relationship": "父亲", "phone": "+44 7700 900077" },
{ "name": "法图玛塔·迪亚洛", "relationship": "姐妹", "phone": "+44 7700 900088" }
]
},
{
"empId": "EMP-005",
"fullName": "利亚姆·阮",
"department": "市场部",
"location": "Singapore",
"email": "liam.n@company.com",
"emergencyContacts": [
{ "name": "林·阮", "relationship": "配偶", "phone": "+65 9123 4567" },
{ "name": "明·阮", "relationship": "兄弟", "phone": "+65 8123 4567" }
]
},
{
"empId": "EMP-006",
"fullName": "埃琳娜·罗斯托娃",
"department": "财务部",
"location": "德国柏林",
"email": "elena.r@company.com",
"emergencyContacts": [
{ "name": "德米特里·罗斯托夫", "relationship": "配偶", "phone": "+49 151 2345678" },
{ "name": "奥尔加·罗斯托娃", "relationship": "母亲", "phone": "+49 160 8765432" }
]
},
{
"empId": "EMP-007",
"fullName": "陈大卫",
"department": "法务与合规部",
"location": "澳大利亚悉尼",
"email": "david.c@company.com",
"emergencyContacts": [
{ "name": "陈格蕾丝", "relationship": "姐妹", "phone": "+61 491 570 156" },
{ "name": "陈彼得", "relationship": "父亲", "phone": "+61 491 570 157" }
]
}
];
window.onload = function () {
spreadInstance = new spreadNS.Workbook(document.getElementById("ss"), { sheetCount: 1 });
initSpread(spreadInstance);
initRecordPanel(data);
};
function initSpread(spread) {
spread.suspendPaint();
var sheet = spread.sheets[0];
applyEmployeeDetailView(sheet);
var dataManagerTable = spread.dataManager().addTable("table1", { data: data });
bindingSource = new spreadNS.Bindings.CellBindingSource(dataManagerTable);
sheet.setDataSource(bindingSource);
bindingSource.activeRecordIndex(activeRecordIndex);
spread.resumePaint();
}
function initRecordPanel(records) {
var list = document.getElementById("recordList");
if (!list) {
return;
}
list.innerHTML = "";
records.forEach(function (record, index) {
var item = document.createElement("button");
item.type = "button";
item.className = "record-item";
item.setAttribute("data-index", index);
item.setAttribute("aria-pressed", index === activeRecordIndex ? "true" : "false");
item.innerHTML =
'<span class="record-item-index">记录 ' + (index + 1) + '</span>' +
'<span class="record-item-name">' + escapeHtml(record.fullName) + '</span>' +
'<span class="record-item-meta">' + escapeHtml(record.empId + " - " + record.department) + '</span>' +
'<span class="record-item-submeta">' + escapeHtml(record.location) + '</span>';
item.addEventListener("click", function () {
updateRecordIndex(bindingSource, index);
});
list.appendChild(item);
});
syncActiveRecordPanel();
}
function syncActiveRecordPanel() {
var items = document.querySelectorAll(".record-item");
for (var i = 0; i < items.length; i++) {
var item = items[i];
var isActive = Number(item.getAttribute("data-index")) === activeRecordIndex;
item.classList.toggle("active", isActive);
item.setAttribute("aria-pressed", isActive ? "true" : "false");
}
var activeLabel = document.getElementById("activeRecordLabel");
if (activeLabel && data[activeRecordIndex]) {
activeLabel.textContent = data[activeRecordIndex].fullName + " (" + data[activeRecordIndex].empId + ")";
}
}
function applyEmployeeDetailView(sheet) {
var Sheets = GC.Spread.Sheets;
var HAlign = Sheets.HorizontalAlign;
var VAlign = Sheets.VerticalAlign;
var LineStyle = Sheets.LineStyle;
var SheetArea = Sheets.SheetArea;
var Style = Sheets.Style;
var colors = {
navy: "Accent 1 -25",
white: "Background 1 -5",
darkGray: "Text 1 15"
};
function setCell(row, col, value, style, bindingPath) {
var cell = sheet.getCell(row, col, SheetArea.viewport);
if (value !== undefined) {
cell.value(value);
}
if (!style) {
return cell;
}
if (style.font) {
cell.font(style.font);
}
if (style.foreColor) {
cell.foreColor(style.foreColor);
}
if (style.backColor) {
cell.backColor(style.backColor);
}
if (style.hAlign !== undefined) {
cell.hAlign(style.hAlign);
}
if (style.vAlign !== undefined) {
cell.vAlign(style.vAlign);
}
if (style.textDecoration !== undefined) {
cell.textDecoration(style.textDecoration);
}
if (style.wordWrap !== undefined) {
cell.wordWrap(style.wordWrap);
}
if (bindingPath) {
cell.bindingPath(bindingPath);
}
return cell;
}
function setBottomBorder(row, col, colCount, color, lineStyle) {
sheet.getRange(row, col, 1, colCount, SheetArea.viewport)
.setBorder(new Sheets.LineBorder(color, lineStyle), { bottom: true });
}
sheet.suspendPaint();
sheet.setRowCount(15);
sheet.setColumnCount(5);
sheet.setColumnWidth(0, 21);
sheet.setColumnWidth(1, 175);
sheet.setColumnWidth(2, 210);
sheet.setColumnWidth(3, 175);
sheet.setColumnWidth(4, 35);
sheet.setRowHeight(1, 53);
sheet.addSpan(1, 1, 1, 4);
setCell(1, 1, " 员工档案与紧急联系人", {
font: "bold 21.3333px 'Segoe UI'",
foreColor: colors.white,
backColor: colors.navy,
hAlign: HAlign.left,
vAlign: VAlign.center
});
setCell(3, 1, "员工编号:", {
font: "bold 13.3333px 'Segoe UI'",
foreColor: colors.darkGray,
hAlign: HAlign.left,
vAlign: VAlign.bottom
});
setCell(3, 2, undefined, {
font: "bold 14.6667px 'Segoe UI'",
foreColor: colors.navy,
hAlign: HAlign.center,
vAlign: VAlign.bottom
});
sheet.setBindingPath(3, 2, "empId");
setCell(5, 1, "基本信息", {
font: "bold 16px 'Segoe UI'",
foreColor: colors.navy,
hAlign: HAlign.left,
vAlign: VAlign.bottom
});
setBottomBorder(5, 1, 1, colors.navy, LineStyle.medium);
sheet.getRange(6, 1, 4, 1).setStyle(new Style({
font: "bold 13.3333px 'Segoe UI'",
foreColor: colors.darkGray,
hAlign: HAlign.right,
vAlign: VAlign.bottom
}));
sheet.setValue(6, 1, "姓名:");
sheet.setValue(7, 1, "部门:");
sheet.setValue(8, 1, "所在地:");
sheet.setValue(9, 1, "邮箱:");
sheet.getRange(6, 2, 4, 1).setStyle(new Style({
font: "13.3333px 'Segoe UI'",
hAlign: HAlign.left,
vAlign: VAlign.bottom
}));
sheet.setBindingPath(6, 2, "fullName");
sheet.setBindingPath(7, 2, "department");
sheet.setBindingPath(8, 2, "location");
sheet.setBindingPath(9, 2, "email");
setCell(11, 1, "紧急联系人", {
font: "bold 16px 'Segoe UI'",
foreColor: colors.navy,
hAlign: HAlign.left,
vAlign: VAlign.bottom
});
var table = sheet.tables.add("emergencyContacts", 12, 1, 3, 3);
table.bindingPath("emergencyContacts");
sheet.resumePaint();
}
function updateRecordIndex(source, index) {
if (!source || index < 0 || index >= data.length) {
return;
}
activeRecordIndex = index;
source.activeRecordIndex(index);
syncActiveRecordPanel();
}
function escapeHtml(text) {
return String(text)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="spreadjs culture" content="zh-cn" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<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>
<aside class="options-container">
<div class="panel-header">
<div class="panel-eyebrow">当前记录</div>
<div id="activeRecordLabel" class="panel-title">王小雨(EMP-001)</div>
<div class="panel-description">
从列表中选择记录,切换电子表格使用的当前数据绑定索引。
</div>
</div>
<div id="recordList" class="record-list"></div>
</aside>
</div>
</body>
</html>
html,
body {
height: 100%;
margin: 0;
}
body {
position: absolute;
inset: 0;
font-family: "Segoe UI", sans-serif;
background: #eef2f6;
color: #213547;
}
.sample-tutorial {
display: flex;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
flex: 1 1 auto;
min-width: 0;
height: 100%;
}
.options-container {
width: 320px;
padding: 20px 16px;
box-sizing: border-box;
background: linear-gradient(180deg, #f8fbff 0%, #f1f6fb 100%);
border-left: 1px solid #d8e2ee;
overflow: auto;
}
.panel-header {
margin-bottom: 16px;
}
.panel-eyebrow {
font-size: 11px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
color: #6c8299;
margin-bottom: 8px;
}
.panel-title {
font-size: 20px;
line-height: 1.3;
font-weight: 700;
color: #18344f;
margin-bottom: 8px;
}
.panel-description {
font-size: 13px;
line-height: 1.5;
color: #5f748a;
}
.record-list {
display: flex;
flex-direction: column;
gap: 10px;
}
.record-item {
width: 100%;
border: 1px solid #d6e2ee;
border-radius: 12px;
background: #ffffff;
padding: 14px 14px 13px;
text-align: left;
cursor: pointer;
transition: border-color 0.2s ease, box-shadow 0.2s ease, transform 0.2s ease;
box-shadow: 0 6px 16px rgba(26, 57, 88, 0.04);
}
.record-item:hover {
border-color: #8eb4d8;
box-shadow: 0 10px 24px rgba(26, 57, 88, 0.08);
transform: translateY(-1px);
}
.record-item.active {
border-color: #2d6ea3;
background: linear-gradient(180deg, #eef7ff 0%, #e5f0fb 100%);
box-shadow: 0 12px 28px rgba(45, 110, 163, 0.14);
}
.record-item-index,
.record-item-name,
.record-item-meta,
.record-item-submeta {
display: block;
}
.record-item-index {
font-size: 11px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
color: #6d8195;
margin-bottom: 7px;
}
.record-item-name {
font-size: 16px;
font-weight: 700;
color: #16324a;
margin-bottom: 6px;
}
.record-item-meta {
font-size: 12px;
color: #31506f;
margin-bottom: 4px;
}
.record-item-submeta {
font-size: 12px;
color: #6a7f93;
}