Skip to content

Filtering ​

Filtering is made up of filters, onFilter, filteredValue and defaultFilteredValue on the column, together with the filter panel configuration.

For users, the most important thing is to tell two modes apart first: in one the component maintains the state internally, and in the other the state is fully controlled from outside the page.

Live demo ​

Multi-select filtering, single-select filtering (filterMultiple: false), filtering with a search box (filterSearch) and default filter values all appear in one table.

Edit in Playground

Basic example ​

vue
<script setup lang="ts">
import { VTable, type TableColumnsType } from '@vtable-guild/vtable-guild'

interface UserRow {
  key: string
  name: string
  status: 'active' | 'paused'
}

const columns: TableColumnsType<UserRow> = [
  { title: 'Name', dataIndex: 'name', key: 'name' },
  {
    title: 'Status',
    dataIndex: 'status',
    key: 'status',
    filters: [
      { text: 'Active', value: 'active' },
      { text: 'Paused', value: 'paused' },
    ],
    onFilter: (value, record) => record.status === value,
  },
]

const dataSource: UserRow[] = [
  { key: '1', name: 'Ada', status: 'active' },
  { key: '2', name: 'Grace', status: 'paused' },
]
</script>

<template>
  <VTable row-key="key" :columns="columns" :data-source="dataSource" />
</template>

A filters: [...] array provides choices; local filtering also needs onFilter, as in the example above. Listing choices alone does not match records automatically. You can also use onFilter: true for built-in matching, or filters: true to generate choices from valueEnum and match them.

Controlled and uncontrolled ​

  • defaultFilteredValue only takes effect on the first render and suits simple default filtering.
  • filteredValue puts the column into controlled mode, where the filter state is managed entirely from outside.
  • filterResetToDefaultFilteredValue controls whether "Reset" returns to the default value instead of clearing directly.

Filter combination rules ​

  • Multiple values selected in the same column are combined with OR.
  • Different columns are combined with AND.
  • The change event returns a filters snapshot of all columns, which suits syncing query conditions.
  • On tree data only top-level records are evaluated: onFilter does not recurse into children, a matching parent brings all of its children along, and a parent that does not match has its whole subtree removed. See Tree table for details.

When you declare the event handler separately, you can take its signature from the public event interface, and the parameters are typed automatically:

ts
import { ref } from 'vue'
import type { TableFiltersInfo, VTableEventProps } from '@vtable-guild/vtable-guild'

// Reuse the UserRow above; bind @change="handleChange" on the table.
const activeFilters = ref<TableFiltersInfo>({})
const handleChange: NonNullable<VTableEventProps<UserRow>['onChange']> = (filters) => {
  activeFilters.value = filters
}

filters is a TableFiltersInfo keyed by column key; each value is (string | number | boolean)[] | null, where null means the column has no filter value. The first argument of onFilter, in contrast, is a single string | number | boolean value, and the second argument keeps the UserRow type.

Custom filter panel ​

If the default filter panel is not enough, there are two ways to extend it:

  • customFilterDropdown, which takes over through a table-level slot.
  • filterDropdown, which defines a custom panel directly on a single column.

In addition, you can use:

  • filterSearch, to add search to the filter items.
  • filterMode: 'tree', to render the filter items as a tree.

The parameters of a custom panel use CustomFilterDropdownSlotProps<UserRow>: setSelectedKeys receives an array of filter values, and the optional arguments of confirm and clearFilters control the confirm and close behavior respectively. See the declarations at the end of the page for the full signatures.

Selects and date pickers inside the panel ​

a-select and a-date-picker from ant-design-vue / antdv-next, and el-select and el-date-picker from Element Plus, mount their own dropdowns on document.body by default. The table treats clicks inside these dropdowns as clicks outside the panel, so the filter panel closes as soon as an option is selected. Render the nested popup inside the panel instead: set getPopupContainer for ant-design-vue / antdv-next, and :teleported="false" for Element Plus.

vue
<script setup lang="ts">
// Reuse the UserRow and dataSource above; the status column changes to customFilterDropdown: true.
const statusOptions = [
  { label: 'Active', value: 'active' },
  { label: 'Paused', value: 'paused' },
]
// Mount the dropdown on the trigger's parent element, that is, inside the filter panel.
const insidePanel = (trigger: HTMLElement) => trigger.parentElement ?? document.body
</script>

<template>
  <VTable row-key="key" :columns="columns" :data-source="dataSource">
    <template #customFilterDropdown="{ selectedKeys, setSelectedKeys, confirm, clearFilters }">
      <!-- The nested dropdown opens inside the panel; min-height reserves space for it -->
      <div style="padding: 8px; width: 200px; min-height: 160px">
        <!-- Element Plus: <el-select :teleported="false" ...> -->
        <a-select
          :value="selectedKeys[0]"
          :options="statusOptions"
          :get-popup-container="insidePanel"
          style="width: 100%"
          @update:value="(value) => setSelectedKeys(value == null ? [] : [String(value)])"
        />
        <a-button size="small" @click="clearFilters()">Reset</a-button>
        <a-button size="small" type="primary" @click="confirm()">OK</a-button>
      </div>
    </template>
  </VTable>
</template>

The nested dropdown is then positioned inside the panel, and the part that extends beyond the panel is clipped by the panel's scroll area, which is why the example sets min-height on the panel content. Adjust the height to the number of options, or reduce the dropdown height (for example with list-height on a-select).

How outside clicks are detected ​

The filter panel, the row selection menu, the column settings and density menus, and the page-size dropdown share the same popup logic. Since 2.9.0-alpha.5, a popup listens for mousedown / touchstart in the capture phase on document to detect outside clicks; a stopPropagation() in an external control (such as @mousedown.stop) runs after the detection and cannot stop it, so the open popup closes all the same. Earlier versions listened in the bubble phase, where stopping propagation kept the popup open, so watch out for this pattern when upgrading. Put the controls that have to work together with the panel inside the panel; the filter panel can also switch to a controlled filterDropdownOpen: an outside click only triggers onFilterDropdownOpenChange(false), and whether to close is up to you.

When to use controlled mode ​

Using filteredValue directly is recommended in the following scenarios:

  • The filter conditions need to be synced to the URL.
  • The page query is driven by an API, and the table is only responsible for showing the current result.
  • Several filter controls need to interact with each other.

Clearing and controlled state ​

filteredValue: undefined uses the internal uncontrolled state; null and [] both mean controlled with no filter value. When controlled, confirming, resetting or clearing submits the filter values requested by this operation through the filters argument of change. The table filters the data by the new value only after the parent component updates filteredValue.

filterResetToDefaultFilteredValue: true makes reset restore defaultFilteredValue; otherwise it clears. When a custom panel calls clearFilters(), it submits the reset result once and requests the panel to close. To reset only the draft and keep the panel, pass clearFilters({ confirm: false, closeDropdown: false }). confirm({ closeDropdown: false }) submits and keeps the panel.

Remote filtering and mixed processing ​

The table's manual.filter: true skips local filtering while keeping the controls, state and events. Application code updates remote conditions and requests when extra.action === 'filter'. A column's onFilter: false also skips its local computation. Neither setting sends a request.

filteredValue determines who owns the filter state; manual.filter determines whether local filtering runs. They are independent: controlled state does not automatically disable local filtering, and application code must still update filteredValue for an external reset.

See Choose where data is processed and Remote filtering with local sorting and pagination. Local filtering with remote pagination only affects the loaded page; for the complete data set, see the full-data example. For valueEnum and automatic choices, see Cell display and enums.

API ​

TRecord is your application's row type. The tables below list only the filtering-related configuration; for all props see the API Reference.

Props ​

PropTypeDefaultDescription
manualboolean | TableManualConfigfalsetrue skips local sorting, filtering and pagination slicing; the object form sets sort, filter and pagination separately, with omitted fields treated as false. Controls local computation only; it neither sends nor prevents requests. Header state and the change event keep working; see Choosing a processing mode.
getPopupContainer(triggerNode: HTMLElement) => HTMLElement-Mount container for the filter, selection and page-size menus.

Column fields ​

FieldTypeDefaultDescription
filtersColumnFilterItem[] | boolean-true generates the options from valueEnum, false provides no options; you can also pass a custom array.
onFilterboolean | ((value: string | number | boolean, record: TRecord) => boolean)-true uses the default matching rule, a function defines a custom one, and false skips local filtering.
filterMultiplebooleantrueWhether multiple filter values can be selected.
filteredValueArray<string | number | boolean> | null-Controlled filter values; null and [] both mean controlled with no filter value.
defaultFilteredValueArray<string | number | boolean>-Uncontrolled default filter values.
customFilterDropdownbooleanfalseUses the table-level customFilterDropdown slot.
filterSearchboolean | ((input: string, filter: ColumnFilterItem) => boolean)falseSearch within the filter items.
filterMode'menu' | 'tree''menu'Display mode of the filter items.
filterResetToDefaultFilteredValuebooleanfalseRestores the default filter values on reset.
filterDropdownOpenboolean-Controlled visibility of the filter dropdown.
onFilterDropdownOpenChange(visible: boolean) => void-Callback when the visibility of the filter dropdown changes.
filteredboolean-Controls the highlight state of the filter icon externally; does not change the filtering logic.
filterIcon(opt: { filtered: boolean }) => VNodeChild-Custom filter icon.
filterDropdownVNodeChild | ((props: CustomFilterDropdownSlotProps<TRecord>) => VNodeChild)-Column-level custom filter panel; takes precedence over the table-level slot.

Events ​

EventPayloadDescription
change(filters: TableFiltersInfo, sorter: VTableSorterResult<TRecord>, extra: TableChangeExtra<TRecord>) => voidSingle event outlet after sorting, filtering, selection and pagination; extra.action is 'sort', 'filter', 'select' or 'paginate'.

When filtering triggers, extra.action is 'filter', and filters is the filter snapshot of all columns, keyed by column key.

Slots ​

SlotPayload typeDescription
customFilterDropdownCustomFilterDropdownSlotProps<TRecord>Table-level custom filter panel; the column must set customFilterDropdown: true.
customFilterIcon{ column: ColumnType<TRecord>; filtered: boolean }Table-level custom filter icon.

The column-level filterDropdown and filterIcon take precedence over the table-level slots.

Theming ​

To change only colors and sizes, override the CSS variables first; to change classes, use the per-instance ui or the global theme.table.slots. For how to choose between the three, see Three-layer theme overrides.

ui slots ​

SlotDescription
filterIconWrapperFilter icon wrapper.
filterIconThe filter trigger icon itself.
filterDropdownFilter panel root container.
filterDropdownListFilter option list container.
filterDropdownItemA single filter option.
filterDropdownItemSelectedSelected option styles.
filterDropdownItemSelectedSingleExtra styles for the selected option in single-select mode (filterMultiple: false).
filterDropdownItemHoverHovered option styles.
filterDropdownContentWrapperOption content wrapper.
filterDropdownActionsBottom action bar.
filterDropdownResetButtonExtra styles for the bottom "Reset" button.
filterDropdownConfirmButtonExtra styles for the bottom "OK" button.
filterDropdownSearchOuter layer of the search area.
filterDropdownSearchFieldSearch input container.
filterDropdownSearchIconSearch icon.
filterDropdownSearchInputSearch input.
filterDropdownListEmptyEmpty-result message.
filterDropdownSwitcherExpand/collapse button of a tree node.
filterDropdownSwitcherExpandedExpanded state.
filterDropdownSwitcherCollapsedCollapsed state.
filterDropdownSwitcherNoopPlaceholder for nodes that cannot be toggled.
filterDropdownTreeWrapperTree list wrapper.
filterDropdownTreeListTree list <ul>.
filterDropdownTreeItemA single tree node row.
filterDropdownTreeContentWrapperTree node content wrapper.
filterDropdownTreeItemSelectedSelected node styles.
filterDropdownTreeItemMatchedStyles for nodes matched by the search.
filterDropdownTreeCheckAllThe "Select all" row.

Preset defaults ​

The default classes of the two presets on the slots above, given in the structure of createVTableGuild({ theme }); copy and modify them as needed. Override values are merged with the defaults through cn(). In the default precompiled mode these utilities carry the vtg- prefix at runtime; for the override rules see Utility prefix and override rules.

ts
createVTableGuild({
  theme: {
    table: {
      slots: {
        filterIconWrapper: 'shrink-0 ml-1 self-stretch -my-1 -me-2 flex items-center',
        filterIcon: 'inline-flex items-center justify-center cursor-pointer transition-colors text-xs px-1 self-stretch rounded-md hover:bg-black/6',
        filterDropdown: '',
        filterDropdownList: 'm-0 p-0 list-none min-w-[calc(120px-2*var(--vtg-table-popup-padding-inline))]',
        filterDropdownItem: 'flex items-center cursor-pointer',
        filterDropdownItemSelected: 'bg-[color:var(--color-control-item-active-bg)] hover:bg-[color:var(--color-control-item-active-hover-bg)]',
        filterDropdownItemSelectedSingle: '',
        filterDropdownItemHover: 'hover:bg-[color:var(--color-control-item-hover-bg)]',
        filterDropdownSwitcher: 'inline-flex items-center justify-center w-6 h-6 cursor-pointer text-[10px] text-[color:var(--color-on-surface)] transition-transform duration-200',
        filterDropdownSwitcherExpanded: 'rotate-0',
        filterDropdownSwitcherCollapsed: '-rotate-90',
        filterDropdownSwitcherNoop: 'cursor-default invisible',
        filterDropdownContentWrapper: 'flex items-center gap-2 px-3 py-[5px] rounded-sm flex-1 min-w-0',
        filterDropdownTreeWrapper: 'pt-2 px-2 pb-0',
        filterDropdownTreeList: 'm-0 p-0 list-none [--vtg-table-filter-tree-indent-size:24px]',
        filterDropdownTreeItem: 'flex items-start cursor-pointer pb-1',
        filterDropdownTreeContentWrapper: 'flex min-h-6 flex-1 min-w-0 items-center gap-2 rounded-[6px] px-1 py-0',
        filterDropdownTreeItemSelected: 'bg-[color:var(--color-control-item-active-bg)] hover:bg-[color:var(--color-control-item-active-bg)]',
        filterDropdownTreeItemMatched: 'font-medium',
        filterDropdownTreeCheckAll: 'flex items-center w-full mb-1 ms-1',
        filterDropdownActions: 'flex items-center justify-between gap-2 px-2 py-2 border-t border-[color:var(--color-default)]',
        filterDropdownResetButton: '',
        filterDropdownConfirmButton: '',
        filterDropdownSearch: 'p-2 border-b border-[color:var(--color-default)]',
        filterDropdownSearchField: 'flex items-center gap-1 px-[11px] h-[32px] min-w-[140px] rounded-[6px] border border-[color:var(--color-default)] bg-[color:var(--color-surface)] transition-[border-color,box-shadow] hover:border-[#4096ff] focus-within:border-[#4096ff] focus-within:shadow-[0_0_0_2px_rgba(5,145,255,0.1)]',
        filterDropdownSearchIcon: 'inline-flex shrink-0 items-center justify-center text-[14px] text-black/25',
        filterDropdownSearchInput: 'w-auto min-w-[140px] shrink grow-0 basis-auto h-[22px] px-0 border-0 bg-transparent rounded-none text-[length:14px] leading-[22px] shadow-none focus:border-transparent placeholder:text-black/25',
        filterDropdownListEmpty: 'px-3 py-2 text-center text-[length:12px] leading-5 text-black/25 select-none cursor-default',
      },
    },
  },
})
ts
createVTableGuild({
  themePreset: 'element-plus',
  theme: {
    table: {
      slots: {
        filterIconWrapper: 'shrink-0 flex items-center',
        filterIcon: 'inline-flex items-center justify-center cursor-pointer transition-colors text-sm px-1 self-stretch',
        filterDropdown: '',
        filterDropdownList: 'p-0 m-0 list-none min-w-[calc(100px-2*var(--vtg-table-popup-padding-inline))]',
        filterDropdownItem: 'cursor-pointer',
        filterDropdownItemSelected: '[--color-on-surface:var(--color-primary)] font-medium hover:bg-[color:var(--color-control-item-hover-bg)]',
        filterDropdownItemSelectedSingle: 'bg-[color:var(--color-primary)] [--color-on-surface:#ffffff] hover:bg-[color:var(--color-primary)]',
        filterDropdownItemHover: 'hover:bg-[color:var(--color-control-item-hover-bg)] hover:[--color-on-surface:var(--color-primary)]',
        filterDropdownSwitcher: 'inline-flex items-center justify-center w-6 h-6 cursor-pointer text-xs text-[color:var(--color-muted)] transition-transform duration-200',
        filterDropdownSwitcherExpanded: 'rotate-0',
        filterDropdownSwitcherCollapsed: '-rotate-90',
        filterDropdownSwitcherNoop: 'cursor-default invisible',
        filterDropdownContentWrapper: 'flex items-center gap-2 leading-[36px] px-[10px] flex-1 min-w-0',
        filterDropdownTreeWrapper: 'py-[5px]',
        filterDropdownTreeList: 'm-0 p-0 list-none [--vtg-table-filter-tree-indent-size:24px]',
        filterDropdownTreeItem: 'flex items-center cursor-pointer',
        filterDropdownTreeContentWrapper: 'flex items-center gap-2 leading-[36px] px-[10px] flex-1 min-w-0',
        filterDropdownTreeItemSelected: '[--color-on-surface:var(--color-primary)] font-medium hover:bg-[color:var(--color-control-item-hover-bg)]',
        filterDropdownTreeItemMatched: 'font-medium',
        filterDropdownTreeCheckAll: 'flex items-center cursor-pointer px-[10px] leading-[36px]',
        filterDropdownActions: 'flex flex-row-reverse items-center justify-end gap-3 px-2 py-2 border-t border-[color:var(--vtg-border-color-lighter,var(--color-default))]',
        filterDropdownConfirmButton: 'bg-transparent border-0 shadow-none text-[color:var(--vtg-table-text-color)] hover:bg-transparent hover:text-[color:var(--color-primary)] active:bg-transparent text-[12px] px-[3px] h-auto leading-none',
        filterDropdownResetButton: 'bg-transparent border-0 shadow-none text-[color:var(--vtg-table-text-color)] hover:bg-transparent hover:text-[color:var(--color-primary)] active:bg-transparent text-[12px] px-[3px] h-auto leading-none disabled:text-[color:var(--color-text-disabled,#a8abb2)] disabled:cursor-not-allowed',
        filterDropdownSearch: 'px-[10px] pt-2 pb-1',
        filterDropdownSearchField: 'flex items-center gap-2 rounded-[var(--vtg-input-border-radius)] border border-[color:var(--color-default)] bg-[color:var(--color-surface)] px-2',
        filterDropdownSearchIcon: 'inline-flex shrink-0 items-center justify-center text-[12px] text-[color:var(--color-muted)]',
        filterDropdownSearchInput: 'min-w-0 flex-1 border-0 bg-transparent px-0 shadow-none focus:border-transparent placeholder:text-[color:var(--color-muted)]',
        filterDropdownListEmpty: 'px-3 py-3 text-center text-[length:12px] leading-5 text-[color:var(--color-muted)] select-none cursor-default',
      },
    },
  },
})

CSS variables ​

The tables show the default values in light mode; dark mode overrides them separately under .dark. The popup variables are shared by table popups such as the filter panel and the bulk selection menu.

VariablePurposeantdvelement-plus
--color-filter-iconInactive color of the filter iconvar(--color-sorter-icon)#909399
--vtg-table-filter-dropdown-max-heightMaximum height of the filter panel option area; scrolls internally beyond itundeclared, falls back to 264px280px
--vtg-table-popup-radiusRadius of table popups such as filtering and batch selection6px2px
--vtg-table-popup-borderTable popup border01px solid var(--vtg-border-color-lighter, var(--color-default))
--vtg-table-popup-padding-blockTable popup vertical padding4px5px
--vtg-table-popup-padding-inlineTable popup horizontal padding4px0px
--vtg-table-popup-shadowTable popup shadow0 6px 16px rgb(0 0 0 / 8%), 0 3px 6px -4px rgb(0 0 0 / 12%), 0 9px 28px 8px rgb(0 0 0 / 5%)0 0 12px rgb(0 0 0 / 12%)

Type Declarations ​

Filter values support string | number | boolean. The declarations below include the column configuration, the custom panel and the table events. The column configuration is an excerpt of the related fields; FilteringColumnFields is a documentation helper interface, not a package export. For the complete declarations see the Type reference.

View type declarations
ts
export interface ColumnFilterItem {
  text: string
  value: string | number | boolean
  children?: ColumnFilterItem[]
}
ts
/** ColumnType 节选;文档辅助接口,不是包导出。 */
export interface FilteringColumnFields<TRecord extends object = Record<string, any>> {
  key?: Key
  filters?: ColumnFilterItem[] | boolean
  onFilter?: boolean | ((value: string | number | boolean, record: TRecord) => boolean)
  filterMultiple?: boolean
  filteredValue?: (string | number | boolean)[] | null
  defaultFilteredValue?: (string | number | boolean)[]
  customFilterDropdown?: boolean
  filterSearch?: boolean | ((input: string, filter: ColumnFilterItem) => boolean)
  filterMode?: 'menu' | 'tree'
  filterResetToDefaultFilteredValue?: boolean
  filterDropdownOpen?: boolean
  onFilterDropdownOpenChange?: (visible: boolean) => void
  filtered?: boolean
  filterIcon?: (opt: { filtered: boolean }) => VNodeChild
  filterDropdown?: VNodeChild | ((props: CustomFilterDropdownSlotProps<TRecord>) => VNodeChild)
}
ts
export interface CustomFilterDropdownSlotProps<TRecord extends object = Record<string, any>> {
  column: ColumnType<TRecord>
  selectedKeys: (string | number | boolean)[]
  setSelectedKeys: (keys: (string | number | boolean)[]) => void
  confirm: (options?: { closeDropdown?: boolean }) => void
  clearFilters: (options?: { confirm?: boolean; closeDropdown?: boolean }) => void
  filters: ColumnFilterItem[]
  visible: boolean
  close: () => void
}
ts
export type TableFiltersInfo = Record<string, (string | number | boolean)[] | null>
ts
export interface TableChangeExtra<TRecord extends object = Record<string, any>> {
  action: 'sort' | 'filter' | 'select' | 'paginate'
  /** Available only when pagination is enabled. */
  pagination?: TablePaginationState
  currentDataSource: TRecord[]
}
ts
export interface VTableEventProps<TRecord extends object = Record<string, any>> {
  onChange?: (
    filters: TableFiltersInfo,
    sorter: VTableSorterResult<TRecord>,
    extra: TableChangeExtra<TRecord>,
  ) => void
  onResizeColumn?: (column: ColumnType<TRecord>, width: number) => void
  onRowDragEnd?: (newData: TRecord[], info: RowDragSortInfo) => void
}