...

/

Implementing the Filtering Functionality

Implementing the Filtering Functionality

Learn to implement filtering in Svelte using a custom extensible filtering algorithm.

We already have the option to sort the table, but we're missing the other part: filtering. When working with a large dataset, filters let us quickly locate the data relevant to us. In the following example, we've added a Filter component to our header. This is imported from Filter.svelte. Open Filter.svelte to see what we have for the component so far.

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}
Create the Filter component

We have a button positioned relatively with a width of 20px. Inside this button, we have an icon for the filter with auto margins horizontally and user-select: none for preventing text selection. The second class is not mandatory, but it is a nice addition for a better user experience. This way, the user won't be able to highlight and select the icon.

Note: mx stands for setting left and right margins, while my stands for setting top and bottom margins. Likewise, px is for left and right padding, while py is for top and bottom paddings.

This image is then toggled through the toggleFilter function. When the filter is toggled on, we display an "X" mark; otherwise, we display the filter icon.

Press + to interact
Filters toggled on
Filters toggled on

Why do we need the data-action="filter" attribute? This is so that we only close the pop-up when the "X" mark is clicked, and not when we interact with the filters. Let's style it and add select for letting users select which filter to use. We're going to have a filter for ...

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}
Select option added for filter
...