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,
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="ko-kr"/>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-resources-ko/dist/gc.spread.sheets.resources.ko.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;
}