PivotTable Filter

SpreadJS PivotTables support Label Filters, and Value Filters. These functions can be used to filter data. Different filters can be set to obtain the required data.

Description
app.vue
index.html
styles.css
Copy to CodeMine

You can set different filters to get the data that you need.

The user can set a filter as follows:

///* function labelFilter(fieldName: string, filterInfo: GC.Spread.Pivot.IPivotTextFilterInfo | GC.Spread.Pivot.IPivotConditionFilterInfo): GC.Spread.Pivot.IPivotTextFilterInfo | GC.Spread.Pivot.IPivotConditionFilterInfo | void
/**
 * get or set label filter info for a field
 * @param {string} fieldName the target field name of pivot table
 * @param {GC.Spread.Pivot.IPivotFilterInfo} filterInfo the label filter info when set
 * @returns get pivot table labelFilter information
 */
labelFilter (fieldName: string, filterInfo?: IPivotConditionFilterInfo | IPivotTextFilterInfo): IPivotConditionFilterInfo | IPivotTextFilterInfo | void;
///* function valueFilter(fieldName: string, filterInfo:GC.Spread.Pivot.IPivotConditionFilterInfo):GC.Spread.Pivot.IPivotConditionFilterInfo | void
/**
 * get or set value filter info for a field
 * @param {string} fieldName the target field name of pivot table
 * @param {GC.Spread.Pivot.IPivotFilterInfo} filterInfo the value filter info when set
 * @returns the pivot table value information
 */
valueFilter (fieldName: string, filterInfo?: IPivotConditionFilterInfo): IPivotConditionFilterInfo | void

You can use the Label Filter as follows:

    let labelFilter = {condition:{conType:GC.Pivot.PivotConditionType.caption, val:[20,30], operator: GC.Pivot.PivotCaptionFilterOperator.between}};
    // if val.length === 1, val=[20,null]
    pivotTable.labelFilter("region", labelFilter);
    let itemList = ["Europe", "South America", "Asia", "Australia"]
    let labelFilter = { textItem: { list: itemList, isAll: false } };
    pivotTable.labelFilter("region", labelFilter);

You can use the Value Filter as follows:

    let valueFilter = { condition: { conType: GC.Pivot.PivotConditionType.value, val: [50, 100], operator: GC.Pivot.PivotValueFilterOperator.between }, conditionByName: "Sum of amount" };
    // if val.length === 1, val=[20,null]
    pivotTable.valueFilter("region", valueFilter);

SpreadJS PivotTable supports five conditions:

    export interface IPivotCaptionConditionFilterInfo{
        conType: GC.Pivot.PivotConditionType;
        val: [string, string];
        operator: GC.Pivot.PivotCaptionFilterOperator;
    }
    export interface IPivotDateConditionFilterInfo{
        conType: GC.Pivot.PivotConditionType;
        val: [Date, Date];
        operator: GC.Pivot.PivotDateFilterOperator;
        isWholeDay: boolean;
    }
    export interface IPivotTop10ConditionFilterInfo{
        conType: GC.Pivot.PivotConditionType;
        val: number;
        type: GC.Pivot.PivotTop10FilterType; //The type of data involved in the calculation
        operator: GC.Pivot.PivotTop10FilterOperator;
    }
    export interface IPivotValueConditionInfo{
        conType: GC.Pivot.PivotConditionType;
        val: [number, number];
        operator: GC.Pivot.PivotValueFilterOperator;
    }
    export interface ITextCollectionCondition{
        list: string[];// field item list
        isAll: boolean;// is field all item or no
    }

For date filter operator, we also support month to date and quarter to date.

  • conType: Used to describe the type of condition
  • operator: Used to describe the operate type of filter; setting up an operator corresponds to the type of conType

SpreadJS PivotTables support two filters, and users can set filters using the following two data types, for example:

    export interface IPivotConditionFilterInfo {
        conditionByName: string;
        condition: GC.Pivot.IPivotCaptionConditionFilterInfo | GC.Pivot.IPivotDateConditionFilterInfo | GC.Pivot.IPivotTop10ConditionFilterInfo | GC.Pivot.IPivotValueConditionInfo
    }

    export interface IPivotTextFilterInfo{
        textItem: GC.Spread.Pivot.ITextCollectionCondition;
    }
You can set different filters to get the data that you need. The user can set a filter as follows: You can use the Label Filter as follows: You can use the Value Filter as follows: SpreadJS PivotTable supports five conditions: For date filter operator, we also support month to date and quarter to date. conType: Used to describe the type of condition operator: Used to describe the operate type of filter; setting up an operator corresponds to the type of conType SpreadJS PivotTables support two filters, and users can set filters using the following two data types, for example:
<template> <div class="sample-tutorial"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> </gc-spread-sheets> <div class="options-container"> <div class="whole-field">The following operations all work on <b>Salesperson</b> Field:</div> <div>Label Filter:</div> <div class="pivottable-filter label-filter"> <select name="labelFilter" id="labelFilter" class="pivot-filter" v-model="labelFilter"> <option value=1>equalsTo</option> <option value=2>notEqualsTo</option> <option value=3>beginsWith</option> <option value=4>doesNotBeginWith</option> <option value=5>endsWith</option> <option value=6>doesNotEndWith</option> <option value=7>contains</option> <option value=8>doesNotContain</option> <option value=9>greaterThan</option> <option value=10>greaterThanOrEqualsTo</option> <option value=11>lessThan</option> <option value=12>lessThanOrEqualsTo</option> <option value=13>between</option> <option value=14>notBetween</option> </select> <input type="text" class="label-filter-input filter-input" id="label-val1" placeholder="value1" v-model="label1" /> <input type="text" class="label-filter-input filter-input" id="label-val2" placeholder="value2" v-model="label2" /> </div> <input type="button" class="set-label-filter set-filter" id="set-label-filter" value="Set Label Filter" @click="updatePivotLabelFilter" /> <br /> <input type="button" class="set-label-filter set-filter" id="clear-label-filter" value="Clear Label Filter" @click="clearPivotLabelFilter" /> <br /> <hr /> <div class="value-filter-label">Value Filter:</div> <div class="pivottable-filter value-filter"> <select name="valueFilter" id="valueFilter" class="pivot-filter" v-model="valueFilter"> <option value=1>equalsTo</option> <option value=2>notEqualsTo</option> <option value=3>greaterThan</option> <option value=4>greaterThanOrEqualsTo</option> <option value=5>lessThan</option> <option value=6>lessThanOrEqualsTo</option> <option value=7>between</option> <option value=8>notBetween</option> </select> <input type="text" class="value-filter-input filter-input" id="value-val1" placeholder="value1" v-model="value1" /> <input type="text" class="value-filter-input filter-input" id="value-val2" placeholder="value2" v-model="value2" /> </div> <input type="button" class="set-value-filter set-filter" id="set-value-filter" value="Set Value Filter" @click="updatePivotValueFilter" /> <br /> <input type="button" class="clear-value-filter set-filter" id="clear-value-filter" value="Clear Value Filter" @click="clearPivotValueFilter" /> <br /> <hr /> </div> </div> </template> <script> import Vue from 'vue'; import '@mescius/spread-sheets-vue' import GC from '@mescius/spread-sheets'; import "@mescius/spread-sheets-shapes"; import "@mescius/spread-sheets-pivot-addon"; import './styles.css'; let App = Vue.extend({ name: "app", data: function () { return { autoGenerateColumns: false, autoMergeMode: GC.Spread.Sheets.AutoMerge.AutoMergeMode.restricted, autoMergeSelectionMode: GC.Spread.Sheets.AutoMerge.SelectionMode.merged, spread: null, pivotTable: null, labelFilter: 1, label1: null, label2: null, valueFilter: 1, value1: null, value2: null }; }, methods: { initSpread: function (spread) { this.spread = spread; spread.suspendPaint(); spread.setSheetCount(2); let sheet1 = spread.getSheet(0); let sheet2 = spread.getSheet(1); let tableName = this.getSource(sheet2, pivotSales); let pivotTable = this.addPivotTable(sheet1, tableName); this.pivotTable = pivotTable; spread.resumePaint(); }, clearPivotLabelFilter: function () { let spread = this.spread; let pivotTable = this.pivotTable; spread.suspendPaint(); pivotTable.labelFilter("Salesperson", null); this.label1 = ""; this.label2 = ""; this.labelFilter = 1; spread.resumePaint(); }, updatePivotLabelFilter: function () { let spread = this.spread; let pivotTable = this.pivotTable; spread.suspendPaint(); let labelType = this.labelFilter, labelFilterValue1 = this.label1, labelFilterValue2 = this.label2; if (labelType) { if (labelType < 13) { labelFilterValue2 = null; } pivotTable.labelFilter("Salesperson", { condition: { conType: GC.Pivot.PivotConditionType.caption, val: [labelFilterValue1, labelFilterValue2], operator: labelType - 1 } }); } else { pivotTable.labelFilter("Salesperson", null); } spread.resumePaint(); }, clearPivotValueFilter: function () { let spread = this.spread; let pivotTable = this.pivotTable; spread.suspendPaint(); pivotTable.valueFilter("Salesperson", null); this.value1 = null; this.value2 = null; this.valueFilter = 1; spread.resumePaint(); }, updatePivotValueFilter: function () { let spread = this.spread; let pivotTable = this.pivotTable; spread.suspendPaint(); let valueType = this.valueFilter, valueFilterValue1 = +this.value1, valueFilterValue2 = +this.value2; if (valueType) { if (valueType < 7) { valueFilterValue2 = null; } pivotTable.valueFilter("Salesperson", { condition: { conType: GC.Pivot.PivotConditionType.value, val: [valueFilterValue1, valueFilterValue2], operator: valueType - 1 }, conditionByName: "Quantity" }); } else { pivotTable.valueFilter("Salesperson", null); } spread.resumePaint(); }, updatePivotSort: function (sortType) { let spread = this.spread; let pivotTable = this.pivotTable; spread.suspendPaint(); if (!sortType) { pivotTable.sort("Salesperson", { sortType: GC.Pivot.SortType.asc }); } else { pivotTable.sort("Salesperson", { sortType: GC.Pivot.SortType.desc }); } spread.resumePaint(); }, getSource: function (sheet, tableSource) { sheet.name("DataSource"); sheet.setRowCount(117); sheet.setColumnWidth(0, 120); sheet.getCell(-1, 0).formatter("YYYY-mm-DD"); sheet.getRange(-1, 4, 0, 2).formatter("$ #,##0"); let table = sheet.tables.add('table', 0, 0, 117, 6); for (let i = 2; i <= 117; i++) { sheet.setFormula(i - 1, 5, '=D' + i + '*E' + i) } table.style(GC.Spread.Sheets.Tables.TableThemes["none"]); sheet.setArray(0, 0, tableSource); return table.name(); }, addPivotTable: function (sheet, source) { sheet.suspendPaint(); sheet.name("PivotTable"); sheet.setRowCount(10000); let pivotTable = sheet.pivotTables.add("PivotTable", source, 1, 1, GC.Spread.Pivot.PivotTableLayoutType.outline, GC.Spread.Pivot.PivotTableThemes.light8); pivotTable.suspendLayout(); pivotTable.add("salesperson", "Salesperson", GC.Spread.Pivot.PivotTableFieldType.rowField); pivotTable.add("car", "Cars", GC.Spread.Pivot.PivotTableFieldType.rowField); let groupInfo = { originFieldName: "date", dateGroups: [{ by: GC.Pivot.DateGroupType.quarters }] }; pivotTable.group(groupInfo); pivotTable.add("Quarters (date)", "Quarters (date)", GC.Spread.Pivot.PivotTableFieldType.columnField); pivotTable.add("quantity", "Quantity", GC.Spread.Pivot.PivotTableFieldType.valueField, GC.Pivot.SubtotalType.sum); pivotTable.resumeLayout(); sheet.resumePaint(); pivotTable.autoFitColumn(); return pivotTable; }, bindEvent: function (pivotTable, spread) { document.getElementById("set-label-filter").addEventListener("click", function (e) { spread.suspendPaint(); let labelType, labelFilterValue1, labelFilterValue2; let labelNode = document.getElementById("labelFilter"); labelType = labelNode.selectedIndex; if (labelType) { labelFilterValue1 = document.getElementById("label-val1").value; labelFilterValue2 = document.getElementById("label-val2").value; if (labelType < 13) { labelFilterValue2 = null; } pivotTable.labelFilter("Salesperson", { condition: { conType: GC.Pivot.PivotConditionType.caption, val: [labelFilterValue1, labelFilterValue2], operator: labelType - 1 } }); } else { pivotTable.labelFilter("Salesperson", null); } spread.resumePaint(); }); document.getElementById("set-value-filter").addEventListener("click", function (e) { spread.suspendPaint(); let valueType, valueFilterValue1, valueFilterValue2; let valueNode = document.getElementById("valueFilter"); valueType = valueNode.selectedIndex; if (valueType) { valueFilterValue1 = document.getElementById("value-val1").value; valueFilterValue2 = document.getElementById("value-val2").value; if (valueType < 7) { valueFilterValue2 = null; } pivotTable.valueFilter("Salesperson", { condition: { conType: GC.Pivot.PivotConditionType.value, val: [valueFilterValue1, valueFilterValue2], operator: valueType - 1 }, conditionByName: "Quantity" }); } else { pivotTable.valueFilter("Salesperson", null); } spread.resumePaint(); }); } }, computed: { dataSource() { return getData(); } } }); new Vue({ render: h => h(App) }).$mount('#app'); </script>
<!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/vue/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/spread/source/data/pivot-data.js" type="text/javascript"></script> <!-- SystemJS --> <script src="$DEMOROOT$/en/vue/node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); System.import('$DEMOROOT$/en/lib/vue/license.js'); </script> </head> <body> <div id="app"></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; } .pivottable-filter{ height: 100px; } .pivot-filter{ width: 200px; height: 25px; display: block; margin-bottom: 10px; float: left; } .filter-input{ width: 200px; height: 20px; display: block; /* margin-left: 15px; */ margin-top: 10px; } .set-filter{ width: 200px; margin-top: 20px; /* float: right; */ } .value-filter-label{ margin-top: 25px; } .sort-type{ width: 200px; margin-top: 25px; display: block; } .sort{ width: 200px; margin-top: 10px; display: block; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .whole-field{ margin-bottom: 10px; }
(function (global) { System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, meta: { '*.css': { loader: 'css' }, '*.vue': { loader: 'vue-loader' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-shapes': 'npm:@mescius/spread-sheets-shapes/index.js', '@mescius/spread-sheets-pivot-addon': 'npm:@mescius/spread-sheets-pivot-addon/index.js', '@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js', 'jszip': 'npm:jszip/dist/jszip.js', 'css': 'npm:systemjs-plugin-css/css.js', 'vue': 'npm:vue/dist/vue.min.js', 'vue-loader': 'npm:systemjs-vue-browser/index.js', 'tiny-emitter': 'npm:tiny-emitter/index.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build':'npm:systemjs-plugin-babel/systemjs-babel-browser.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'js' }, rxjs: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' } } }); })(this);