概述
本 Demo 展示了如何对数据透视表的文本字段进行分组。Demo 中以销售人员(salesperson)字段为例,将 "Alan"、"Bob"、"John" 分为一组,"Serena"、"Tess" 分为另一组,并通过 checkbox 提供交互式的分组开关。
实现思路
创建数据源表(DataSource)和数据透视表(PivotTable)
使用 group() 方法对 salesperson 字段进行文本分组
将分组后的新字段添加到数据透视表的行区域
绑定 checkbox 事件,允许用户动态切换分组显示
代码解析
创建文本分组
这段代码定义了文本分组信息。originFieldName 指定要分组的原始字段名称,textGroup.fieldName 定义新创建的分组字段名称,textGroup.groupItems 是一个对象,其键为组名,值为属于该组的原始文本项数组。
添加分组字段到数据透视表
使用 add() 方法将分组字段添加到数据透视表的行区域,第 5 个参数 0 表示将该字段插入到行区域的第一个位置。
动态切换分组
通过 checkbox 的选中状态控制是否应用分组。选中时调用 group() 方法创建分组并添加新字段,取消选中时调用 ungroup() 方法移除分组。
运行效果
初始状态显示分组后的数据透视表,salesperson 字段按两个组(group1 和 group2)分类显示
取消选中 checkbox 后,分组被移除,数据透视表恢复到未分组状态
再次选中 checkbox,重新应用分组
分组字段和原始字段可以同时显示在行区域,形成层级结构
API 参考
group() 方法
对数据透视表字段进行分组。
groupInfo.originFieldName:源字段名称
groupInfo.textGroup:文本分组配置
fieldName:新创建的分组字段名称
groupItems:分组项对象,键为组名,值为属于该组的文本项数组
ungroup() 方法
根据字段名称取消分组。
fieldName:要取消分组的字段名称
add() 方法
将字段添加到数据透视表。
name:字段名称
caption:字段标题
fieldType:字段类型(行字段、列字段或值字段)
subtotalType:可选,小计类型
index:可选,插入位置
window.onload = function () {
let spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 2});
initSpread(spread);
};
function initSpread(spread) {
spread.suspendPaint();
let sheet1 = spread.getSheet(0);
let sheet2 = spread.getSheet(1);
let tableName = getSource(sheet2, pivotSales);
let pivotTable = addPivotTable(sheet1, tableName);
bindEvent(pivotTable,spread);
spread.resumePaint();
}
function getSource(sheet, tableSource){
sheet.name("DataSource");
sheet.setRowCount(117);
sheet.setColumnWidth(0, 120);
sheet.getCell(-1, 0).formatter("YYYY-mm-DD");
sheet.getRange(-1,4,0,2).formatter("$ #,##0");
let table = sheet.tables.add('table', 0, 0, 117, 6);
for(let i=2;i<=117;i++)
{
sheet.setFormula(i-1,5,'=D'+i+'*E'+i)
}
table.style(GC.Spread.Sheets.Tables.TableThemes["none"]);
sheet.setArray(0, 0, tableSource);
return table.name();
}
function addPivotTable(sheet, source){
sheet.suspendPaint();
sheet.name("PivotTable");
sheet.setRowCount(10000);
let pivotTable = sheet.pivotTables.add("PivotTable", source, 1, 1, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.dark3);
pivotTable.suspendLayout();
pivotTable.add("car", "Cars", GC.Spread.Pivot.PivotTableFieldType.columnField);
let groupInfo = { originFieldName: "salesperson",textGroup: {
fieldName:"salesperson1",
groupItems:{
group1:["Alan", "Bob", "John"],
group2:["Serena", "Tess"]
}
} };
pivotTable.group(groupInfo);
pivotTable.add("salesperson1", "salesperson1", GC.Spread.Pivot.PivotTableFieldType.rowField);
pivotTable.add("salesperson", "salesperson", GC.Spread.Pivot.PivotTableFieldType.rowField);
pivotTable.add("total", "Totals", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum);
pivotTable.resumeLayout();
sheet.resumePaint();
pivotTable.autoFitColumn();
return pivotTable;
}
function bindEvent(pivotTable,spread){
document.getElementById("group-text").addEventListener("click",function(e){
spread.suspendPaint();
if(e.target.checked){
let groupInfo = { originFieldName: "salesperson",textGroup: {
fieldName:"salesperson1",
groupItems:{
group1:["Alan", "Bob", "John"],
group2:["Serena", "Tess"]
}
} };
pivotTable.group(groupInfo);
pivotTable.add("salesperson1", "salesperson1", GC.Spread.Pivot.PivotTableFieldType.rowField, null, 0);
} else {
pivotTable.ungroup("salesperson1");
}
spread.resumePaint();
});
}
<!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-shapes/dist/gc.spread.sheets.shapes.min.js"
type="text/javascript"></script>
<script
src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-pivot-addon/dist/gc.spread.pivot.pivottables.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="$DEMOROOT$/spread/source/data/pivot-data.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">
<div class="group">
<label><b>分组字段</b></label>
</div>
<hr />
<div class="group">
<input type="checkbox" id="group-text" class="group-text" checked>
<label for="group-text">对 <b>文本</b> 字段分组</label>
</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;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.group{
width: 250px;
margin-bottom: 20px;
}
select{
width: 250px;
}