The Felt SDK allows you to filter which features are visible in a layer using expressions that evaluate against feature properties. Filters can come from different sources and are combined to determine what's visible.
By understanding how filters work and combine, you can create dynamic views of your data that respond to user interactions and application state.
Understanding filter sources
Layer filters can come from multiple sources, which are combined to create the final filter:
Style filters: Set by the map creator in the Felt UI
Component filters: Set through interactive legend components
Ephemeral filters: Set temporarily through the SDK
You can inspect these different filter sources using getLayerFilters:
constfilters=awaitfelt.getLayerFilters("layer-1");console.log(filters.style);// Base filters from the layer styleconsole.log(filters.components);// Filters from legend componentsconsole.log(filters.ephemeral);// Filters set through the SDKconsole.log(filters.combined);// The final result of combining all filters
Setting filters
Use setLayerFilters to apply ephemeral filters to a layer:
// Listen for selection changes
felt.onSelectionChange({
handler: async ({ selection }) => {
// Find the first selected feature
const selectedFeature = selection.find(node => node.type === "feature");
if (selectedFeature) {
// Get the state code from the selected feature
const stateCode = selectedFeature.entity.properties.STATE_CODE;
// Filter the counties layer to show only counties in the selected state
felt.setLayerFilters({
layerId: "counties-layer",
filters: ["STATE_CODE", "eq", stateCode]
});
} else {
// Clear the filter when nothing is selected
felt.setLayerFilters({
layerId: "counties-layer",
filters: null
});
}
}
});
const filters = await felt.getLayerFilters("layer-1");
// Check if there are any style filters before adding ephemeral ones
if (filters.style) {
console.log("This layer already has style filters");
}
import type { Filters } from "@feltmaps/sdk";
const filter: Filters = ["POPULATION", "gt", 1000000];
felt.setLayerFilters({
layerId: "layer-1",
filters: filter
});