Filter Action

The Filter object's onFilter method provides the ability to create a custom filter action. This allows you to add extended functionality for how the filter is done, and even allows you to filter the spread instance automatically at run-time, which can be useful when you want to filter data without making the user filter themselves.

Create a new class extended from RowFilterBase. For example: The next step is to override the onFilter(args) to provide the custom filter action. The args.action is one of the following FilterActionType enumerations: filter unfilter Use it to check if the filter or unfilter action should be provided. For example: You can use the filterButtonVisble method to control the display of the filter buttons.
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, 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 charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.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; }