Spreadsheet filters

This section provides more details about the methods, events, and properties related to the spreadsheet filters. Jspreadsheet gives developers a flexible way to interact programmatically with the filters and customize their behavior.

Documentation

Methods

Method Description
setFilter(number, array) Apply filters programmatically.
setFilter(columnNumber: Number, values: String[])
getFilter(mixed) Currently applied filters to a column or to all columns.
getFilter(columnNumber: Number|null)
openFilter(number) Open the filter input.
openFilter(columnNumber: Number)
closeFilter() Close the filter input.
closeFilter()
resetFilters() Reset all filters.
resetFilters()
showFilter(number) Enable the filter icon for one or all columns.
showFilter(columnNumber: Number|null)
hideFilter(number) Disable the filter icon for one or all columns.
hideFilter(columnNumber: Number|null)
resetFilters(mixed) Reset the filters for one or all columns..
resetFilters(columnNumber: Number|null)


Related events

It is possible to completely overwrite the filters' behavior using onbeforefilter. The event can be used to intercept andcancel or change the filter results.
Events Description
onbeforefilter Action to be executed before applying the filter. Returns an array with the valid row numbers or returns false to return all rows.
onbeforefilter(worksheet: Object, terms: Object, rowNumbers: Number[]). The object contains the terms used in each of the existing columns.
onfilter After the filter has been applied to the rows.
onfilter(worksheet: Object, terms: String[], rowNumbers: Number[])
onopenfilter Customize the items available when the filter editor is open.
onopenfilter(worksheet: Object, column: number options: Object[]) => options | undefined


Initial Settings

Property Description
filters: boolean Start the spreadsheet with the filters enabled. Default: false


Enable the filter for individual columns

The properties filter is available on the columns object when creating a new spreadsheet. The property filter: true will enable the filter for the column specified.

jspreadsheet(document.getElementById('spreadsheet'), {
    worksheets: [{
        data: data,
        filters: false,
        columns: [
            { type: 'text', title: 'Car', filter: true },
            { type: 'text' },
         ]
     }]
});


Examples

Interacting with the filters programmatically

Enable the filters on the initialization and apply or reset filters programmatically.




Apply ['Honda'] programatically to the first worksheet, second column


Source code

<html>
<script src="https://jspreadsheet.com/v9/jspreadsheet.js"></script>
<link rel="stylesheet" href="https://jspreadsheet.com/v9/jspreadsheet.css" type="text/css" />
<script src="https://jsuites.net/v5/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v5/jsuites.css" type="text/css" />

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons" />

<div id="spreadsheet"></div>

<input type='button' value='Apply' class='jbutton dark' onclick="apply()">
<input type='button' value='Reset' class='jbutton dark' onclick="reset()">

<script>
// Set your JSS license key (The following key only works for one day)
jspreadsheet.setLicense('NjU4Y2Q0MDg0ODcwZmI1YTUwZTEwODkwZjFkNWJiNTA3ZjM3OGYxOTBlOGNkYTA3MWUxZjU5OGI0MTVlMzE1ZjBiYzYwYzQ0MDExNzMwMWIzOWEwMjhlODllMGIxNTZmODEyYjRkZjUyODM3YjgyNzhiNWZiMzY3OWUzMjAwYjMsZXlKdVlXMWxJam9pU25Od2NtVmhaSE5vWldWMElpd2laR0YwWlNJNk1UY3hNRGt3T0Rnd01Td2laRzl0WVdsdUlqcGJJbXB6Y0hKbFlXUnphR1ZsZEM1amIyMGlMQ0pqYjJSbGMyRnVaR0p2ZUM1cGJ5SXNJbXB6YUdWc2JDNXVaWFFpTENKamMySXVZWEJ3SWl3aWQyVmlJaXdpYkc5allXeG9iM04wSWwwc0luQnNZVzRpT2lJek5DSXNJbk5qYjNCbElqcGJJblkzSWl3aWRqZ2lMQ0oyT1NJc0luWXhNQ0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owWlhJaUxDSmlZWElpTENKMllXeHBaR0YwYVc5dWN5SXNJbk5sWVhKamFDSXNJbkJ5YVc1MElpd2ljMmhsWlhSeklsMHNJbVJsYlc4aU9uUnlkV1Y5');

var apply = function() {
    worksheets[0].setFilter(1, ['Honda']);
}
var reset = function() {
    worksheets[0].resetFilters();
}

var data = [
    ['Jazz', 'Honda', '2019-02-12', true, '2000,00', '=E1*0.1', '#777700'],
    ['Civic', 'Honda', '2018-07-11', false, '4000,01', '=E2*0.1', '#007777'],
    ['Civic', 'Honda', '2018-07-12', true, '3200,01', '=E3*0.1', '#117717'],
    ['Picanto', 'Kia', '2018-07-12', false, '4000,00', '=E4*0.1', '#ffb74d'],
    ['Optima', 'Kia', '2020-01-12', false, '3000,00', '=E5*0.1', '#4db6ac'],
];

var worksheets = jspreadsheet(document.getElementById('spreadsheet'), {
    worksheets: [{
        data: data,
        filters: true,
        columns: [
            {
                type:'text',
                title:'Car',
                width:120
            },
            {
                type: 'dropdown',
                title:'Make',
                width:180,
                source:[
                    "Alfa Romeo",
                    "Audi",
                    "Bmw",
                    "Chevrolet",
                    "Chrystler",
                    "Dodge",
                    "Ferrari",
                    "Fiat",
                    "Ford",
                    "Honda",
                    "Hyundai",
                    "Jaguar",
                    "Jeep",
                    "Kia",
                    "Mazda",
                    "Mercedez-Benz",
                    "Mitsubish",
                    "Nissan",
                    "Peugeot",
                    "Porsche",
                    "Subaru",
                    "Suzuki",
                    "Toyota",
                    "Volkswagen"
                  ]
            },
            {
                type: 'calendar',
                title:'Available',
                width:120,
                options:{ format:'DD/MM/YYYY' }
            },
            {
                type: 'checkbox',
                title:'Stock',
                width:80
            },
            {
                type: 'number',
                title:'Price',
                mask:'$ #.##0,00',
                width:100,
                decimal:','
            },
            {
                type: 'text',
                width:110,
                title:'Commission',
                truncate: 3,
            },
            {
                title: 'Color',
                type: 'color',
                width:100,
                render:'square',
            },
         ]
     }]
});
</script>
</html>

Enable filters for individual columns

It is possible to enable the filters at the column level. This means you can use the filters for one specific column only.



Source code

<html>
<script src="https://jspreadsheet.com/v9/jspreadsheet.js"></script>
<link rel="stylesheet" href="https://jspreadsheet.com/v9/jspreadsheet.css" type="text/css" />
<script src="https://jsuites.net/v5/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v5/jsuites.css" type="text/css" />

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons" />

<div id="spreadsheet-2"></div>

<script>
// Set your JSS license key (The following key only works for one day)
jspreadsheet.setLicense('NjU4Y2Q0MDg0ODcwZmI1YTUwZTEwODkwZjFkNWJiNTA3ZjM3OGYxOTBlOGNkYTA3MWUxZjU5OGI0MTVlMzE1ZjBiYzYwYzQ0MDExNzMwMWIzOWEwMjhlODllMGIxNTZmODEyYjRkZjUyODM3YjgyNzhiNWZiMzY3OWUzMjAwYjMsZXlKdVlXMWxJam9pU25Od2NtVmhaSE5vWldWMElpd2laR0YwWlNJNk1UY3hNRGt3T0Rnd01Td2laRzl0WVdsdUlqcGJJbXB6Y0hKbFlXUnphR1ZsZEM1amIyMGlMQ0pqYjJSbGMyRnVaR0p2ZUM1cGJ5SXNJbXB6YUdWc2JDNXVaWFFpTENKamMySXVZWEJ3SWl3aWQyVmlJaXdpYkc5allXeG9iM04wSWwwc0luQnNZVzRpT2lJek5DSXNJbk5qYjNCbElqcGJJblkzSWl3aWRqZ2lMQ0oyT1NJc0luWXhNQ0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owWlhJaUxDSmlZWElpTENKMllXeHBaR0YwYVc5dWN5SXNJbk5sWVhKamFDSXNJbkJ5YVc1MElpd2ljMmhsWlhSeklsMHNJbVJsYlc4aU9uUnlkV1Y5');

// Create the spreadsheet
jspreadsheet(document.getElementById('spreadsheet-2'), {
    worksheets: [{
        data: data2,
        filters: false,
        columns: [
            { type: 'text', filter: true, width: '300px' },
            { type: 'text' },
         ]
     }]
});
</script>
</html>

Customize the spreadsheet filter behavior

Custom filters can be created using the onbeforefilter event.


In this example, if "Canada" is present in the terms in the first column filter, always display all rows.

Source code

<html>
<script src="https://jspreadsheet.com/v9/jspreadsheet.js"></script>
<link rel="stylesheet" href="https://jspreadsheet.com/v9/jspreadsheet.css" type="text/css" />
<script src="https://jsuites.net/v5/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v5/jsuites.css" type="text/css" />

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons" />

<div id="spreadsheet-3"></div>

<script>
// Set your JSS license key (The following key only works for one day)
jspreadsheet.setLicense('NjU4Y2Q0MDg0ODcwZmI1YTUwZTEwODkwZjFkNWJiNTA3ZjM3OGYxOTBlOGNkYTA3MWUxZjU5OGI0MTVlMzE1ZjBiYzYwYzQ0MDExNzMwMWIzOWEwMjhlODllMGIxNTZmODEyYjRkZjUyODM3YjgyNzhiNWZiMzY3OWUzMjAwYjMsZXlKdVlXMWxJam9pU25Od2NtVmhaSE5vWldWMElpd2laR0YwWlNJNk1UY3hNRGt3T0Rnd01Td2laRzl0WVdsdUlqcGJJbXB6Y0hKbFlXUnphR1ZsZEM1amIyMGlMQ0pqYjJSbGMyRnVaR0p2ZUM1cGJ5SXNJbXB6YUdWc2JDNXVaWFFpTENKamMySXVZWEJ3SWl3aWQyVmlJaXdpYkc5allXeG9iM04wSWwwc0luQnNZVzRpT2lJek5DSXNJbk5qYjNCbElqcGJJblkzSWl3aWRqZ2lMQ0oyT1NJc0luWXhNQ0lzSW1Ob1lYSjBjeUlzSW1admNtMXpJaXdpWm05eWJYVnNZU0lzSW5CaGNuTmxjaUlzSW5KbGJtUmxjaUlzSW1OdmJXMWxiblJ6SWl3aWFXMXdiM0owWlhJaUxDSmlZWElpTENKMllXeHBaR0YwYVc5dWN5SXNJbk5sWVhKamFDSXNJbkJ5YVc1MElpd2ljMmhsWlhSeklsMHNJbVJsYlc4aU9uUnlkV1Y5');

// Create the spreadsheet
jspreadsheet(document.getElementById('spreadsheet-3'), {
    worksheets: [{
        data: [
            ['United States', 'Wholemeal', 'Yes', '2019-02-12'],
            ['Canada', 'Breakfast Cereals', 'Yes', '2019-03-01'],
            ['Canada', 'Grains', 'No', '2018-11-10'],
            ['Brazil', 'Pasta', 'Yes', '2019-01-12'],
        ],
        defaultColWidth: '140px',
        filters: true,
    }],
    onbeforefilter: function(worksheet, terms, results) {
        // Show all rows if Canada is one of the options
        if (terms[0] && terms[0].indexOf('Canada') >= 0) {
            return false;
        }
        return results;
    }
});
</script>
</html>