From 7d4fb280bad1cc4bf9c17665f2e85ff65ed0dad2 Mon Sep 17 00:00:00 2001 From: Alec Posney Date: Tue, 5 Sep 2017 13:31:04 +1000 Subject: [PATCH] Add ability to easily select all for multi-filter The multi filter option is useful but lacking in an easy easy way to select all values. I have added in a psudo option '*' that when selected automatically fills out the mutli-select with all possible filters. I have also added in a second psudo option '-' which becomes available _if_ the multi-filter has all possible values selected. This makes it easy to clear the multi-filter. --- client/app/services/query-result.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/client/app/services/query-result.js b/client/app/services/query-result.js index c7e1e0799d..269cb5ab8d 100644 --- a/client/app/services/query-result.js +++ b/client/app/services/query-result.js @@ -1,10 +1,13 @@ import debug from 'debug'; import moment from 'moment'; -import { uniq, contains, values, some, each, isArray, isNumber, isString } from 'underscore'; +import { uniq, contains, values, some, each, isArray, isNumber, isString, includes } from 'underscore'; const logger = debug('redash:services:QueryResult'); const filterTypes = ['filter', 'multi-filter', 'multiFilter']; +const ALL_VALUES = '*'; +const NONE_VALUES = '-'; + function getColumnNameWithoutType(column) { let typeSplit; if (column.indexOf('::') !== -1) { @@ -208,6 +211,21 @@ function QueryResultService($resource, $timeout, $q) { this.filterFreeze = filterFreeze; if (filters) { + filters.forEach((filter) => { + if (filter.multiple && includes(filter.current, ALL_VALUES)) { + filter.current = filter.values.slice(1); + } + + if (filter.current.length === (filter.values.length - 1)) { + filter.values[0] = NONE_VALUES; + } + + if (filter.multiple && includes(filter.current, NONE_VALUES)) { + filter.current = []; + filter.values[0] = ALL_VALUES; + } + }); + this.filteredData = this.query_result.data.rows.filter(row => filters.reduce((memo, filter) => { if (!isArray(filter.current)) { @@ -378,6 +396,12 @@ function QueryResultService($resource, $timeout, $q) { }); }); + filters.forEach((filter) => { + if (filter.multiple) { + filter.values.unshift(ALL_VALUES); + } + }); + filters.forEach((filter) => { filter.values = uniq(filter.values, (v) => { if (moment.isMoment(v)) {