筛选行为

筛选对象的 onFilter 方法提供了创建自定义筛选操作的能力。这允许你为过滤器的工作方式添加扩展功能,甚至允许你在运行时自动过滤Spread实例,这在你想要过滤数据而不让用户自己过滤的情况下是很有用的。

首先创建一个扩展自 RowFilterBase 的类。例如: 然后通过重写 onFilter(args) 方法来提供自定义筛选操作。 args.action 是下面列出的 FilterActionType 枚举的其中一个: filter unfilter 使用它来检查筛选和不筛选操作是否应当被提供。例如: 你可以调用 filterButtonVisble 方法来控制筛选按钮的显示。
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.setColumnWidth(0, 100); sheet.setColumnWidth(1, 200); sheet.setValue(1, 0, "Name"); sheet.setValue(1, 1, "Score"); var score = [ { name: "Simon", score: 59 }, { name: "Jack", score: 70 }, { name: "Lily", score: 86 }, { name: "Bob", score: 54 }, { name: "Lucy", score: 84 }, { name: "John", score: 99 } ]; for (var i = 0, len = score.length; i < len; i++) { var student = score[i]; sheet.setValue(i + 2, 0, student.name); sheet.setValue(i + 2, 1, student.score); } function SelectionFilter(range) { spreadNS.Filter.RowFilterBase.call(this, range); } SelectionFilter.prototype = new spreadNS.Filter.RowFilterBase(); SelectionFilter.prototype.onFilter = function (args) { var sheet = args.sheet, range = args.range, filterRows = args.filteredRows, filteredOutRows = args.filteredOutRows, action = args.action, doFilter = action === spreadNS.Filter.FilterActionType.filter; if (doFilter) { if (range.col < 0) { range.col = 0; range.colCount = sheet.getColumnCount(); } var activeCellSet; for (var i = 0, len = filterRows.length; i < len; i++) { var r = filterRows[i]; for (var c = range.col, len1 = range.col + range.colCount; c < len1; c++) { if (!activeCellSet) { activeCellSet = true; sheet.setSelection(r, c, 1, 1); } else { sheet.addSelection(r, c, 1, 1); } } } sheet.setValue(9, 1, "Select the filtered items"); } else { sheet.setSelection(0, 0, 1, 1); sheet.setValue(9, 1, "No filtered items"); } spread.focus(); }; var actionFilter = new SelectionFilter(new spreadNS.Range(2, 1, 6, 1)); sheet.rowFilter(actionFilter); actionFilter.filterButtonVisible(false); sheet.resumePaint(); document.getElementById("btnFilter").addEventListener('click',function () { var sheet = spread.getActiveSheet(), filter = sheet.rowFilter(), value = parseInt(document.getElementById("filterValue").value, 10), col = 1; if (filter && !isNaN(value)) { filter.removeFilterItems(col); var condition = new spreadNS.ConditionalFormatting.Condition(spreadNS.ConditionalFormatting.ConditionType.numberCondition, { compareType: spreadNS.ConditionalFormatting.GeneralComparisonOperators.lessThan, expected: value }); filter.addFilterItem(col, condition); filter.filter(); sheet.invalidateLayout(); sheet.repaint(); } }); document.getElementById("btnClearFilter").addEventListener('click',function () { var sheet = spread.getActiveSheet(), filter = sheet.rowFilter(); if (filter) { filter.removeFilterItems(1); } }); }
<!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/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity/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"> <div class="option-row"> <label for="filterValue">Score less than:</label> <input id="filterValue" placeholder="Input filter score value" type="text" value="60"/> <input type="button" id="btnFilter" value="Apply filter" title="Filter score column with filter action." /> <input type="button" id="btnClearFilter" value="Clear filter" title="Clear score column' filter." /> </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: 10px; } input { padding: 4px 6px; } label { display: block; margin-bottom: 6px; } input[type=button] { margin-top: 6px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }