Custom Multi-Column Filter Implementation for Ant Design Vue Tables
Custom Multi-Column Filter Implementation for Ant Design Vue Tables
When working with data tables, implementing Excel-like filtering functionality across multiple columns requires a custom approach since Ant Design Vue's built-in filter mechanism only supports single-column operations.
Table Configuration
<a-table
:columns="tableColumns"
:dataSource="tableData"
:pagination="false"
bordered
size="small"
:scroll="{ x: 1940, y: 400 }"
rowKey="id"
>
<template v-slot:filterDropdown="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }">
<div class="filter-query">
<a-input
:value="selectedKeys[0]"
style="min-width: 188px"
@change="e => onFilterInput(e.target.value, setSelectedKeys, column.key)"
@pressEnter="() => executeSearch(selectedKeys, confirm, column.key)"
/>
</div>
<div class="filter-options">
<a-checkbox
style="margin-left: 8px"
:indeterminate="isPartialSelected(filterState[column.key].selected, filterState[column.key].options)"
:checked="isAllSelected(filterState[column.key].selected, filterState[column.key].options)"
@change="(e) => toggleSelectAll(e.target.checked, column.key)"
>
Select All
</a-checkbox>
<a-checkbox-group
:value="filterState[column.key].selected"
class="filter-options-list"
name="filterGroup"
:options="filterState[column.key].options"
@change="(keys) => onOptionChange(keys, column.key)"
/>
</div>
<div class="filter-actions">
<a-button
type="primary"
icon="search"
size="small"
style="width: 90px; margin-right: 8px"
@click="() => executeSearch(selectedKeys, confirm, column.key)"
>
Search
</a-button>
<a-button
size="small"
style="width: 90px"
@click="() => resetFilter(selectedKeys, confirm, clearFilters, column.key, setSelectedKeys)"
>
Reset
</a-button>
</div>
</template>
<a-icon
slot="filterIcon"
slot-scope="filtered, column"
type="search"
:style="{ color: getFilterIconColor(column.key) }"
/>
</a-table>
Column Definition
data() {
return {
tableColumns: [
{
title: 'Column A',
key: 'colA',
align: 'center',
fixed: 'left',
width: 120
},
{
title: 'Column B',
key: 'colB',
align: 'center',
fixed: 'left',
scopedSlots: {
filterDropdown: 'filterDropdown',
filterIcon: 'filterIcon'
},
onFilterDropdownVisibleChange: visible => {
if (!visible) {
this.syncFilterState('colB')
}
},
width: 150
},
{
title: 'Column C',
key: 'colC',
align: 'center',
fixed: 'left',
scopedSlots: {
filterDropdown: 'filterDropdown',
filterIcon: 'filterIcon'
},
onFilterDropdownVisibleChange: visible => {
if (!visible) {
this.syncFilterState('colC')
}
},
width: 150
},
{
title: 'Column D',
key: 'colD',
align: 'center',
fixed: 'left',
scopedSlots: {
filterDropdown: 'filterDropdown',
filterIcon: 'filterIcon'
},
onFilterDropdownVisibleChange: visible => {
if (!visible) {
this.syncFilterState('colD')
}
},
width: 150
},
{
title: 'Actions',
key: 'actions',
align: 'center',
fixed: 'right',
width: 120,
scopedSlots: { customRender: 'actions' }
}
],
filterState: {},
activeFilters: []
}
}
created() {
this.initializeFilters()
}
methods: {
initializeFilters() {
const filterableColumns = this.tableColumns.filter(col => col.scopedSlots?.filterDropdown)
filterableColumns.forEach(col => {
this.filterState[col.key] = {
allOptions: [],
options: [],
optionsSnapshot: [],
selected: [],
selectedSnapshot: [],
excluded: [],
excludedSnapshot: [],
modified: false
}
})
}
}
Building Filter Options from Data
buildFilterOptions() {
const filterableColumns = this.tableColumns.filter(col => col.scopedSlots?.filterDropdown)
filterableColumns.forEach(col => {
const uniqueValues = [...new Set(this.rawData.map(item => item[col.key]))]
const options = uniqueValues.map(val => ({
label: val === null ? '(empty)' : val,
value: val === null ? 'null' : val
}))
this.filterState[col.key] = {
allOptions: options,
options: options,
optionsSnapshot: options,
selected: options.map(opt => opt.value),
selectedSnapshot: options.map(opt => opt.value),
excluded: [],
excludedSnapshot: [],
modified: false
}
})
}
Filter Input Handler
onFilterInput(value, setSelectedKeys, columnKey) {
setSelectedKeys(value ? [value] : [])
const state = this.filterState[columnKey]
state.options = state.optionsSnapshot.filter(
opt => opt.label.toString().indexOf(value) > -1
)
state.selected = state.options.map(opt => opt.value)
state.excluded = value
? state.allOptions.filter(opt => opt.label.toString().indexOf(value) === -1).map(opt => opt.value)
: []
this.filterState = { ...this.filterState }
}
Selection State Helpers
isPartialSelected(selected, allOptions) {
return selected.length > 0 && selected.length < allOptions.length
},
isAllSelected(selected, allOptions) {
return selected.length > 0 && selected.length === allOptions.length
},
getFilterIconColor(columnKey) {
return this.activeFilters.includes(columnKey) ? '#108ee9' : '#cccccc'
}
Sync State When Dropdown Closes
syncFilterState(columnKey) {
setTimeout(() => {
const state = this.filterState[columnKey]
state.options = [...state.optionsSnapshot]
state.selected = [...state.selectedSnapshot]
state.excluded = [...state.excludedSnapshot]
})
}
Select All Toggle
toggleSelectAll(checked, columnKey) {
const state = this.filterState[columnKey]
if (checked) {
state.selected = state.allOptions.map(opt => opt.value)
state.excluded = []
} else {
state.selected = []
state.excluded = state.allOptions.map(opt => opt.value)
}
this.filterState = { ...this.filterState }
}
Checkbox Change Handler
onOptionChange(checkedValues, columnKey) {
const state = this.filterState[columnKey]
state.selected = checkedValues
if (checkedValues.length === state.allOptions.length) {
state.excluded = []
} else {
state.excluded = state.allOptions
.filter(opt => checkedValues.indexOf(opt.value) === -1)
.map(opt => opt.value)
}
this.filterState = { ...this.filterState }
}
Execute Search
executeSearch(selectedKeys, confirm, columnKey) {
confirm()
setTimeout(() => {
if (this.filterState[columnKey].excluded.length === 0) {
this.activeFilters = this.activeFilters.filter(k => k !== columnKey)
} else if (!this.activeFilters.includes(columnKey)) {
this.activeFilters.push(columnKey)
}
setSelectedKeys([''])
}, 0)
const state = this.filterState[columnKey]
state.optionsSnapshot = [...state.options]
state.selectedSnapshot = [...state.selected]
state.excludedSnapshot = [...state.excluded]
this.applyFilters(columnKey)
}
applyFilters(currentColumn) {
this.tableData = this.rawData.filter(row => {
for (const colKey in this.filterState) {
const cellValue = row[colKey] === null ? 'null' : row[colKey]
if (colKey !== currentColumn) {
if (this.filterState[colKey].excluded.includes(cellValue)) {
return false
}
} else {
if (!this.filterState[colKey].selected.includes(cellValue)) {
return false
}
}
}
return true
})
this.refreshFilterOptions(currentColumn)
}
Refresh Options After Filter
refreshFilterOptions(triggerColumn) {
const filterableColumns = this.tableColumns.filter(col => col.scopedSlots?.filterDropdown)
filterableColumns.forEach(col => {
const state = this.filterState[col.key]
const newOptions = [...new Set(this.tableData.map(item => item[col.key]))].map(val => ({
label: val === null ? '(empty)' : val,
value: val === null ? 'null' : val
}))
if (state.modified && col.key !== triggerColumn) {
const filteredOptions = this.computeAvailableOptions(col.key)
state.options = filteredOptions
state.optionsSnapshot = filteredOptions
} else {
state.options = newOptions
state.optionsSnapshot = newOptions
}
})
}
computeAvailableOptions(targetColumn) {
const filteredData = this.rawData.filter(row => {
for (const colKey in this.filterState) {
if (colKey !== targetColumn && this.filterState[colKey].modified) {
const cellValue = row[colKey] === null ? 'null' : row[colKey]
if (this.filterState[colKey].excluded.includes(cellValue)) {
return false
}
}
}
return true
})
return [...new Set(filteredData.map(item => item[targetColumn]))].map(val => ({
label: val === null ? '(empty)' : val,
value: val === null ? 'null' : val
}))
}
Reset Handler
resetFilter(selectedKeys, confirm, clearFilters, columnKey, setSelectedKeys) {
this.toggleSelectAll(true, columnKey)
this.executeSearch(selectedKeys, setSelectedKeys, confirm, columnKey)
}
Key Implementation Details
- State Management: Each filterable column maintains its own state object tracking options, selections, and exclusions
- Snapshot Pattern: Changes are staged in a working copy, with snapshots updated only when search is cofnirmed
- Cross-Filter Dependency: When one column filters data, dependent options in other columns udpate to reflect only available values
- Null Handling: Null values are converted to string 'null' for consistent comparison
- Icon State: Filter icon color indicates whether a column has active filtering