Need searchable dropdown (AutoComplete) inside the column cell #544
Replies: 4 comments 5 replies
-
You can render a custom |
Beta Was this translation helpful? Give feedback.
-
Hi , Can you give an example for filtering a dropdown inside edit mode ..
}, for this column i have a dropdown which has more than 50 + values.. During edit it is difficult to scroll all 50 records of dropdown .Is it possible to have searchable (Autocomplete) in this dropdown. |
Beta Was this translation helpful? Give feedback.
-
Auto complete |
Beta Was this translation helpful? Give feedback.
-
Sorry for my poor English. I was stuck on this extension for a while, but I finally figured out a solution. Please feel free to modify the code as needed. import React, { useState, useEffect } from 'react'
import { TextField, Autocomplete, Chip } from '@mui/material'
import { Column, Table, Row } from '@tanstack/react-table'
interface CustomAutocompleteFilterProps<TData extends object> {
column: Column<TData, unknown>
table: Table<TData>
}
type Options = {
label: string
value: string
}
export const CustomAutocompleteFilter = <TData extends object>({
column,
table,
}: CustomAutocompleteFilterProps<TData>) => {
const [options, setOptions] = useState<Options[]>([])
const [selectedValues, setSelectedValues] = useState<Options[]>(
(table.getColumn(column.id)?.getFilterValue() as Options[] | undefined) ?? [],
)
useEffect(() => {
// selected options
const filterValue = column.getFilterValue() as string[] | undefined
if (filterValue) {
const selectedOptions: Options[] = filterValue.map((value) => ({
label: value,
value: value,
}))
setSelectedValues(selectedOptions)
} else {
setSelectedValues([])
}
// options list
const allData = table.options.data
const uniqueValues = new Set<string>()
allData.forEach((row: TData) => {
const rowValue = row[column.id as keyof TData]
if (rowValue !== undefined) {
uniqueValues.add(rowValue as string)
}
})
const newOptions: Options[] = Array.from(uniqueValues, (value) => ({
label: value,
value: value,
}))
setOptions(newOptions)
}, [column, table])
const handleFilterChange = (values: string[]) => {
const selectedOptions: Options[] = values.map((value) => ({
label: value,
value: value,
}))
setSelectedValues(selectedOptions)
table.getColumn(column.id)?.setFilterValue(values)
}
return (
<Autocomplete
multiple
id='selectBox'
fullWidth
options={options}
defaultValue={[]}
value={selectedValues}
sx={{
'& .MuiInputBase-root': {
padding: 0,
margin: 0,
},
'& .MuiAutocomplete-inputRoot': {
flexWrap: 'nowrap',
overflow: 'hidden',
whiteSpace: 'nowrap',
},
}}
onChange={(_, values) => handleFilterChange(values.map((value) => value.value))}
renderInput={(params) => (
<TextField
{...params}
label='Filter'
size='small'
variant='standard'
InputLabelProps={{
shrink: true,
sx: {
display: selectedValues.length > 0 || params.inputProps.value ? 'none' : 'block',
},
}}
/>
)}
renderOption={(props, option) => {
// Need to add "any" type to avoid compiler errors
const { key, ...restProps }: any = props
return (
// Separating key from props to avoid console warnings when spreading props including key
// <li {...props}>
<li key={option.value} {...restProps}>
{option.label}
</li>
)
}}
renderTags={(tagValue, getTagProps) =>
tagValue.map((option, index) => {
const { key, onDelete, ...tagProps } = getTagProps({ index })
return <Chip key={key} label={option.label} onDelete={onDelete} {...tagProps} />
})
}
isOptionEqualToValue={(option, value) => option.value === value.value}
getOptionLabel={(option) => option.label || ''}
/>
)
}
// Separating key from props to avoid console warnings when spreading props including key
export const customAutocompleteFilterFn = (row: Row<any>, columnId: string, value: string[]) => {
return value.length === 0 || value.includes(row.getValue(columnId))
} To make it work, you just need to inject the filter and filterFn into your column definition. export const columns = [
{
accessorKey: 'hogehoge',
header: 'ColumnHeaderText',
Filter: CustomAutocompleteFilter,
filterFn: customAutocompleteFilterFn,
size: 110,
enableSorting: false,
enableColumnActions: false,
enableColumnFilter: true,
}
] |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In My project we are using Material React Table. We have a column which is of type dropdown . That column has more values. Hence we need searchable dropdown (Auto Complete).I searched multiple posts and didn't get any example for this case.
Beta Was this translation helpful? Give feedback.
All reactions