Select Filter (dropdown)
index
Demo
Demo with Localization
Description
Multiple Select (dropdown) filter is useful when we want to filter the grid 1 or more search term value.
We use an external lib named multiple-select-vanilla.
Note
For this filter to work you will need to add Multiple-Select.js to your project. This is a customized version of the original (thought all the original lib options are available so you can still consult the original site for all options). Couple of small options were added to suit SlickGrid-Universal needs, which is why it points to slickgrid-universal/dist/lib
folder. This lib is required if you plan to use multipleSelect
or singleSelect
Filters. What was customized to (compare to the original)
okButton
option was added to add an OK button for simpler closing of the dropdown after selecting multiple options.okButtonText
was also added for locale (i18n)
offsetLeft
option was added to make it possible to offset the dropdown. By default it is set to 0 and is aligned to the left of the select element. This option is particularly helpful when used as the last right column, not to fall off the screen.autoDropWidth
option was added to automatically resize the dropdown with the same width as the select filter element.
UI Sample
Scroll down below to see the UI Print Screens
Types
There are 3 types of select filter
Filters.singleSelect
which will filter the dataset with 1 value (usesEQ
internally) with checkbox icons.Filters.multipleSelect
which will do a search with 1 or more values (usesIN
internally) with radio icons.
Less recommended
Filters.select
which will filter the dataset with 1 value (usesEQ
internally), same assingleSelect
but uses styling from the browser setup.
this one is less recommended, it is a simple and plain select dropdown. There are no styling applied and will be different in every browser. If you want a more consistent visual UI, it's suggested to use the other 2 filters (
multipleSelect
orsingleSelect
)
SASS Styling
You can change the multipleSelect
and singleSelect
styling with SASS variables for styling. For more info on how to use SASS in your project, read the Wiki - Styling
How to use Select Filter
Simply set the flag filterable
to True and and enable the filters in the Grid Options. Here is an example with a full column definition:
Default Search Term(s)
If you want to load the grid with certain default filter(s), you can use the following optional property:
searchTerms
(array of values)
Note
Even though the option of searchTerms
it is much better to use the more powerful presets
grid options, please refer to the Grid State & Presets for more info.
NOTE If you also have presets
in the grid options, then your searchTerms
will be ignored completely (even if it's a different column) since presets
have higher priority over searchTerms
. See Grid State & Grid Presets from more info.
Sample
How to add Translation?
LabelKey
For the Select (dropdown) filter, you can fill in the "labelKey" property, if found it will translate it right away. If no labelKey
is provided nothing will be translated (unless you have enableTranslateLabel
set to true), else it will use "label"
enableTranslateLabel
You could also use the enableTranslateLabel
which will translate regardless of the label key name (so it could be used with label
, labelKey
or even a customStructure
label).
Custom Structure (key/label pair)
What if your select options (collection) have totally different value/label pair? In this case, you can use the customStructure
to change the property name(s) to use. You can change the label and/or the value, they can be passed independently.
Custom Structure with Translation
What if you want to use customStructure
and translate the labels? Simply pass the flag enableTranslateLabel: true
How to filter empty values?
By default you cannot filter empty dataset values (unless you use a multipleSelect
Filter). You might be wondering, why though? By default an empty value in a singleSelect
Filter is equal to returning all values. You could however use this option emptySearchTermReturnAllValues
set to false
to add the ability to really search only empty values.
Note: the defaults for single & multiple select filters are different
single select filter default is
emptySearchTermReturnAllValues: true
multiple select filter default is
emptySearchTermReturnAllValues: false
Collection FilterBy/SortBy
You can also pre-sort or pre-filter the collection given to the multipleSelect/singleSelect Filters. Also note that if the enableTranslateLabel
flag is set to True
, it will use the translated value to filter or sort the collection. The supported filters are
equal
: filters the collection for only the value provided incollectionFilterBy.value
notEqual
: filters the collection for every value EXCEPT the value provided incollectionFilterBy.value
in
: supportscollectionFilterBy.property
as a nested array, and if thecollectionFilterBy.value
exists in the nested array, the parent item will remain in the collection (NOT be filtered from the collection). For example:columnDef.filter.collection: [{ foo: ['bar'] }, { foo: ['foo'] }]
,collectionFilterBy.property: 'foo'
,collectionFilterBy.value: 'bar'
will return the first item in the collection onlynotIn
:opposite ofin
contains
: assumes thecollectionFilterBy.value
is an array and will check if any of those values exists in thecollectionFilterBy.property
. For example:collection: [{ foo: 'bar' }, { foo: 'foo' }]
,collectionFilterBy.property: 'foo'
,collectionFilterBy.value: [ 'bar', 'foo' ]
will return bot items
Full example:
Multiple FilterBy/SortBy
You can also pass multiple collectionFilterBy
or collectionSortBy
simply by changing these object to array of objects.
However please note that by default the collectionFilterBy
will not merge the result after each pass, it will instead chain them and use the new returned collection after each pass (which means that if original collection is 100 items and 20 items are returned after 1st pass, then the 2nd pass will filter out of these 20 items and so on).
What if you wanted to merge the results instead? Then in this case, you can change the filterResultAfterEachPass
flag defined in `collectionOptions
LabelPrefix / LabelSuffix
labelPrefix
and labelSuffix
were recently added, they are also supported by the customStructure
and can also be overridden. See Collection Label Prefix/Suffix
Custom Structure with Translation
What if you want to use customStructure
and translate the labels? Simply pass the flag enableTranslateLabel: true
Collection FilterBy/SortBy
You can also pre-sort or pre-filter the collection given to the multipleSelect/singleSelect Filters. Also note that if the enableTranslateLabel
flag is set to True
, it will use the translated value to filter or sort the collection. For example:
Collection Label Prefix/Suffix
You can use labelPrefix
and/or labelSuffix
which will concatenate the multiple properties together (labelPrefix
+ label
+ labelSuffix
) which will used by each Select Filter option label. You can also use the property separatorBetweenTextLabels
to define a separator between prefix, label & suffix.
Note If enableTranslateLabel
flag is set to True
, it will also try to translate the Prefix / Suffix / OptionLabel texts.
For example, say you have this collection
You can display all of these properties inside your dropdown labels, say you want to show (symbol with abbreviation and country name). Now you can.
So you can create the multipleSelect
Filter with a customStructure
by using the symbol as prefix, and country as suffix. That would make up something like this:
$ USD USA
$ CAD Canada
with a customStructure
defined as
Collection Label Render HTML
By default HTML is not rendered and the label
will simply show HTML as text. But in some cases you might want to render it, you can do so by enabling the enableRenderHtml
flag.
NOTE: this is currently only used by the Filters that have a collection
which are the MultipleSelect
& SingleSelect
Filters.
Collection Add Blank Entry
In some cases a blank entry at the beginning of the collection could be useful, the most common example for this is to use the first option as a blank entry to tell our Filter to show everything. So for that we can use the addBlankEntry
flag in `collectionOptions
Collection Add Custom Entry at the Beginning/End of the Collection
We can optionally add a custom entry at the beginning of the collection, the most common example for this is to use the first option as a blank entry to tell our Filter to show everything. So for that we can use the addCustomFirstEntry
or addCustomLastEntry
flag in `collectionOptions
Collection Async Load
You can also load the collection asynchronously, but for that you will have to use the collectionAsync
property, which expect a Promise to be passed (it actually accepts 3 types: HttpClient
, FetchClient
or regular Promise
).
Load the collection through an Http call
Using Async Load when Collection is inside an Object Property
What if my collection is nested under the response object? For that you can use collectionInsideObjectProperty
to let the filter know how to get the collection.
Modifying the collection afterward
If you want to modify the collection
afterward, you simply need to find the associated Column reference from the Column Definition and modify the collection
property (not collectionAsync
since that is only meant to be used on page load).
For example
Collection Watch
We can enable the collection watch via the column filter enableCollectionWatch
flag, or if you use a collectionAsync
then this will be enabled by default. The collection watch will basically watch for any changes applied to the collection (any mutation changes like push
, pop
, unshift
, ...) and will also watch for the filter.collection
array replace, when any changes happens then it will re-render the Select Filter with the updated collection list.
Filter Options (MultipleSelectOption
interface)
MultipleSelectOption
interface)All the available options that can be provided as filterOptions
to your column definitions can be found under this multipleSelectOption interface and you should cast your filterOptions
to that interface to make sure that you use only valid options of the multiple-select.js
library.
Grid Option `defaultFilterOptions
You could also define certain options as a global level (for the entire grid or even all grids) by taking advantage of the defaultFilterOptions
Grid Option. Note that they are set via the filter type as a key name (autocompleter
, date
, ...) and then the content is the same as filterOptions
(also note that each key is already typed with the correct filter option interface), for example
Multiple-select.js Options
You can use any options from Multiple-Select.js and add them to your filterOptions
property. However please note that this is a customized version of the original (all original lib options are available so you can still consult the original site for all options).
Couple of small options were added to suit SlickGrid-Universal needs, which is why we are using a fork ghiscoding/multiple-select-modified folder (which is our customized version of the original). This lib is required if you plan to use multipleSelect
or singleSelect
Filters. What was customized to (compare to the original) is the following:
okButton
option was added to add an OK button for simpler closing of the dropdown after selecting multiple options.okButtonText
was also added for locale (i18n)
offsetLeft
option was added to make it possible to offset the dropdown. By default it is set to 0 and is aligned to the left of the select element. This option is particularly helpful when used as the last right column, not to fall off the screen.autoDropWidth
option was added to automatically resize the dropdown with the same width as the select filter element.autoAdjustDropHeight
(defaults to true), when set will automatically adjust the drop (up or down) heightautoAdjustDropPosition
(defaults to true), when set will automatically calculate the area with the most available space and use best possible choise for the drop to show (up or down)autoAdjustDropWidthByTextSize
(defaults to true), when set will automatically adjust the drop (up or down) width by the text size (it will use largest text width)to extend the previous 3 autoAdjustX flags, the following options can be helpful
minWidth
(defaults to null, to use whenautoAdjustDropWidthByTextSize
is enabled)maxWidth
(defaults to 500, to use whenautoAdjustDropWidthByTextSize
is enabled)adjustHeightPadding
(defaults to 10, to use whenautoAdjustDropHeight
is enabled), when usingautoAdjustDropHeight
we might want to add a bottom (or top) padding instead of taking the entire available spacemaxHeight
(defaults to 275, to use whenautoAdjustDropHeight
is enabled)
useSelectOptionLabel` to show different selected label text (on the input select element itself)
useSelectOptionLabelToHtml
is also available if you wish to render label text as HTML for these to work, you have define theoptionLabel
in thecustomStructure
Code
Display shorter selected label text
If we find that our text shown as selected text is too wide, we can choose change that by using optionLabel
in Custom Structure.
Query against another field property
What if your grid is displaying a certain field
but you wish to query against another field? Well you could do that with 1 of the following 3 options:
queryField
(query on a specific field for both the Filter and Sort)queryFieldFilter
(query on a specific field for only the Filter)queryFieldSorter
(query on a specific field for only the Sort)
Example
Last updated