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="ja-jp" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets-resources-ja/dist/gc.spread.sheets.resources.ja.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">次の値未満のスコア</label>
<input id="filterValue" placeholder="フィルタのスコア値を入力してください" type="text" value="60"/>
<input type="button" id="btnFilter" value="フィルタを適用" title=""Score列をフィルタリングします。" />
<input type="button" id="btnClearFilter" value="フィルタをクリア" title="Score列のフィルタリングをクリアします。" />
</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;
}