概述
本 Demo 展示了 SpreadJS 行筛选的核心功能,通过创建不同类型的筛选条件对数据进行筛选。Demo 演示了五种筛选方式:文本条件(包含指定字母)、数字条件(数值比较)、日期条件(星期筛选)、样式条件(背景色筛选)和自定义条件(空值筛选),用户可以通过界面上的单选按钮实时切换不同的筛选条件。
实现思路
创建工作表并填充测试数据,包括文本、数字、日期、空值和不同背景色的单元格
创建 HideRowFilter 行筛选器并应用到工作表的指定列范围(第 0-1 列)
为每种筛选类型定义不同的条件对象(使用 Condition 类)
监听单选按钮的点击事件,动态切换筛选条件
应用筛选时先移除现有条件,再添加新条件,最后刷新工作表显示
代码解析
创建并应用行筛选器
这段代码创建了一个 HideRowFilter 实例,筛选范围覆盖第 0 列到第 1 列的所有行。Range 构造函数的参数中,-1 表示所有行,0 表示起始列,第二个 -1 表示行数(所有行),2 表示列数。filterButtonVisible(false) 隐藏筛选按钮,使界面更简洁。
文本条件筛选
创建文本条件对象,筛选包含字母 'e' 的单元格。compareType 设置为 contains(包含),expected 使用通配符 *e* 表示任意位置包含字母 e。
数字条件筛选
创建数字条件对象,筛选值小于 20 的单元格。compareType 使用 lessThan 表示小于比较。
日期条件筛选
使用 Condition 类的静态方法 fromWeek,参数为 4 表示星期四。这个条件会筛选所有日期为星期四的单元格。
样式条件筛选
创建颜色条件对象,筛选背景色为青色(Cyan)的单元格。compareType 设置为 backgroundColor 表示按背景色筛选。
自定义条件筛选
创建公式条件对象,使用 formulaCondition 类型和 CustomValueType.empty 筛选空值单元格。
动态应用筛选
每次切换筛选条件时,先调用 removeFilterItems(1) 移除第 1 列的现有条件,再调用 addFilterItem(1, condition) 添加新条件,最后调用 filter(1) 方法应用筛选并调用 invalidateLayout() 和 repaint() 刷新界面。
运行效果
默认显示所有数据,不应用任何筛选
点击"文本条件"单选按钮,表格只显示第 1 列值包含字母 'e' 的行(如 begin、end)
点击"数字条件"单选按钮,表格只显示第 1 列值小于 20 的行
点击"日期条件"单选按钮,表格只显示第 1 列日期为星期四的行
点击"样式条件"单选按钮,表格只显示第 1 列单元格背景色为青色的行
点击"自定义条件"单选按钮,表格只显示第 1 列为空的行
点击"无"单选按钮,清除所有筛选条件,恢复显示全部数据
筛选结果实时更新,行号会自动调整以反映筛选后的数据
var spreadNS = GC.Spread.Sheets;
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
initSpread(spread);
};
function initSpread(spread) {
var sheet = spread.getSheet(0);
sheet.suspendPaint();
sheet.setRowCount(50);
sheet.setColumnCount(8);
var rc = sheet.getRowCount();
var cc = sheet.getColumnCount();
for (var r = 0; r < rc; r++) {
for (var c = 0; c < cc; c++) {
if (c == 0) {
sheet.setValue(r, c, "Value is Number");
} else {
sheet.setValue(r, c, r + c);
}
}
}
sheet.setValue(0, 0, "Conditions", spreadNS.SheetArea.colHeader);
sheet.setValue(0, 1, "Cell Value", spreadNS.SheetArea.colHeader);
sheet.setValue(1, 0, "Text contains e");
sheet.setValue(10, 0, "Text doesn't contains e");
sheet.setValue(21, 0, "Text contains e");
sheet.setValue(1, 1, "begin");
sheet.setValue(10, 1, "during");
sheet.setValue(21, 1, "end");
sheet.setValue(2, 0, "Background is Cyan");
sheet.setValue(6, 0, "Background is Purple");
sheet.setValue(12, 0, "Background is Cyan");
sheet.getCell(2, 1).backColor("Cyan");
sheet.getCell(6, 1).backColor("Purple");
sheet.getCell(12, 1).backColor("Cyan");
sheet.setValue(3, 0, "Value is Thursday");
sheet.setValue(8, 0, "Value is Friday");
sheet.setValue(14, 0, "Value is Thursday");
sheet.setValue(3, 1, new Date(2011, 5, 30));
sheet.setValue(8, 1, new Date(2011, 6, 1));
sheet.setValue(14, 1, new Date(2011, 5, 30));
sheet.setValue(4, 0, "Value is null");
sheet.setValue(9, 0, "Value is null");
sheet.setValue(18, 0, "Value is null");
sheet.setValue(4, 1, null);
sheet.setValue(9, 1, null);
sheet.setValue(18, 1, null);
sheet.setColumnWidth(0, 150);
sheet.setColumnWidth(1, 150);
var filter = new spreadNS.Filter.HideRowFilter(new spreadNS.Range(-1, 0, -1, 2));
sheet.rowFilter(filter);
filter.filterButtonVisible(false);
sheet.resumePaint();
function applyFilter(type) {
var sheet = spread.getActiveSheet();
var filter = sheet.rowFilter();
if (filter) {
filter.removeFilterItems(1);
if (type !== "none") {
var condition = getCondition(type);
filter.addFilterItem(1, condition);
}
filter.filter(1);
sheet.invalidateLayout();
sheet.repaint();
}
}
function getCondition(type) {
switch (type) {
case "text":
return new spreadNS.ConditionalFormatting.Condition(spreadNS.ConditionalFormatting.ConditionType.textCondition, {
compareType: spreadNS.ConditionalFormatting.TextCompareType.contains,
expected: "*e*"
});
case "number":
return new spreadNS.ConditionalFormatting.Condition(spreadNS.ConditionalFormatting.ConditionType.numberCondition, {
compareType: spreadNS.ConditionalFormatting.GeneralComparisonOperators.lessThan,
expected: 20
});
case "date":
return spreadNS.ConditionalFormatting.Condition.fromWeek(4);
case "style":
return new spreadNS.ConditionalFormatting.Condition(spreadNS.ConditionalFormatting.ConditionType.colorCondition, {
compareType: spreadNS.ConditionalFormatting.ColorCompareType.backgroundColor,
expected: "Cyan"
});
case "custom":
return new spreadNS.ConditionalFormatting.Condition(spreadNS.ConditionalFormatting.ConditionType.formulaCondition, {
customValueType: spreadNS.ConditionalFormatting.CustomValueType.empty,
formula: null
});
}
}
_getElementById("none_condition").addEventListener('click', function () { applyFilter("none"); });
_getElementById("text_condition").addEventListener('click', function () { applyFilter("text"); });
_getElementById("number_condition").addEventListener('click', function () { applyFilter("number"); });
_getElementById("date_condition").addEventListener('click', function () { applyFilter("date"); });
_getElementById("style_condition").addEventListener('click', function () { applyFilter("style"); });
_getElementById("custom_condition").addEventListener('click', function () { applyFilter("custom"); });
}
function _getElementById(id) {
return document.getElementById(id);
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta name="spreadjs culture" content="zh-cn" />
<meta charset="utf-8" />
<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="$DEMOROOT$/spread/source/data/outlineColumn.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>
<hr>
<div class="option-row">
<input type="radio" name="filter_condition" id="none_condition" checked />
<label for="none_condition">无筛选</label>
</div>
<div class="option-row">
<input type="radio" name="filter_condition" id="text_condition" />
<label for="text_condition" style="display: inline-block; width: 210px;">文本条件(包含字母 e)</label>
</div>
<div class="option-row">
<input type="radio" name="filter_condition" id="number_condition" />
<label for="number_condition">数字条件(小于 20)</label>
</div>
<div class="option-row">
<input type="radio" name="filter_condition" id="date_condition" />
<label for="date_condition" id="label_date_condition" style="display: inline-block; width: 210px;">日期条件(星期四)</label>
</div>
<div class="option-row">
<input type="radio" name="filter_condition" id="style_condition" />
<label for="style_condition">样式条件(青色背景)</label>
</div>
<div class="option-row">
<input type="radio" name="filter_condition" id="custom_condition" />
<label for="custom_condition" id="label_custom_condition">自定义条件(值为空)</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;
}
.option-row {
font-size: 14px;
padding: 5px;
margin-top: 3px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}