One Search Box, Zero Advanced Filters: Match Select Options by Both ID and Name
Searching "Zhang San" also pulls up notes? Without adding advanced filters, how to make a single search box achieve "direct option targeting"!
In the list pages of admin systems, to balance interface simplicity and fast lookup, we usually put a global search box.
But the biggest problem with traditional global search is match-on-contains
When the user only wants to search for "name = Zhang San", unrelated records whose notes contain "Zhang San's customer" also get pulled in, and the noise gets severe when the data volume is large.
If we introduce an advanced filter form, the interaction chain becomes longer.
How can we achieve precise categorical filtering with just a single search box, without adding an extra form?
Unified data structure abstraction
Because the search filter component needs to be reused across multiple business list pages,
we cannot hard-code concrete business fields like "owner" or "approver" in the code,
but instead drive the search logic through a generic field configuration (Schema):
// Structure of a single dropdown option (contains both the id needed by the backend and the name used for display)
interface SelectOption {
id: string; // unique identifier, e.g. '1024', 'A001'
name: string; // display text, e.g. '张三-zhs', '李四'
}
// List field configuration
interface FieldConfig {
key: string; // field name, e.g. 'owner'
label: string; // Chinese field name, e.g. '负责人'
type: 'text' | 'number' | 'select'; // field type
options?: SelectOption[]; // dropdown options, only for select type
}
// Final standard filter condition
interface FilterCondition {
field: string;
operator: 'CONTAIN' | 'EQUAL';
value: string; // filter value sent to the backend
displayText: string; // text displayed on the filter tag
}
Optimization step - Suggestion
The first step to solving the global contains noise is to hand the decision to the user.
When the user types a keyword, the search box shows a suggestion menu in real time, dynamically appending the input to all text/number fields to produce candidates of the form field + contains + input:
function buildTextSuggestions(keyword: string, fields: FieldConfig[]): FilterCondition[] {
const trimmed = keyword.trim();
if (!trimmed) return [];
return fields
.filter((field) => field.type === 'text' || field.type === 'number')
.map((field) => ({
field: field.key,
operator: 'CONTAIN',
value: trimmed,
displayText: `${field.label} 包含 "${trimmed}"`,
}));
}
Although this solves the "searching the whole table randomly" problem,
it only understands pure strings and cannot distinguish enum values (id vs. name):
For select-type fields (e.g., 负责人, 审批人), the database stores id (e.g., 1024, A001), while the UI displays name (e.g., 张三-zhs, 李四).
If we rely on simple string containment, the user still cannot directly type "张三" or the employee number "A001" in the search box to accurately select a specific person!
They are forced back to the tedious "choose field -> choose equals -> expand dropdown to find person" flow. Wuwu~
Since the page already has the options list for select fields at initialization,
and the user's input is also available, we can do the matching entirely on the front end!
Instead of introducing complex NLP or pinyin libraries, we use the dual attributes id and name that each option already has, and match them separately.
Typing "张三" or "zhs": hits the option's
name, generating负责人 = 张三-zhs(actual parameter sent:id: '1024').Typing "A001": hits the option's
id, generating审批人 = 李四 (A001)(actual parameter sent:id: 'A001').
// Core matching implementation
function buildOptionSuggestions(
keyword: string,
fields: FieldConfig[],
limit = 10
): FilterCondition[] {
const normalized = keyword.trim().toLowerCase();
if (!normalized) return [];
const suggestions: FilterCondition[] = [];
for (const field of fields) {
if (field.type !== 'select' || !field.options?.length) continue;
for (const option of field.options) {
const nameHit = option.name.toLowerCase().includes(normalized);
const idHit = option.id.toLowerCase().includes(normalized);
if (nameHit || idHit) {
// If it's an ID match, show the ID in the display text so the user can confirm
const label = idHit && !nameHit
? `${option.name} (${option.id})`
: option.name;
suggestions.push({
field: field.key,
operator: 'EQUAL',
value: option.id, // the backend must receive the id
displayText: `${field.label} = ${label}`,
});
// Stop traversal immediately when the limit is reached, to avoid blocking input responsiveness when there are too many options
if (suggestions.length >= limit) {
return suggestions;
}
}
}
}
return suggestions;
}
In the input event of the input box, merge the two layers of suggestions,
with precise option shortcuts at the top and fallback text contains suggestions after them.
function onSearchInput(keyword: string, fields: FieldConfig[]) {
const optionCandidates = buildOptionSuggestions(keyword, fields, 10);
const textCandidates = buildTextSuggestions(keyword, fields);
// Merge and render the dropdown list
return [...optionCandidates, ...textCandidates];
}
Actually: It seems like we are adding smart suggestions to the search box, but in essence we are folding the heavy advanced filter form into a dropdown layer invisibly, through the reverse interaction of "type first, then select".
QA?: What if the dropdown option data volume is huge, or remote search via API is required?
Top 1 from juejin.cn, machine-translated. The original thread is authoritative.
Reliable. I really like this approach of typing first and then selecting. Folding the advanced filters into a single dropdown really saves a lot of trouble on the backend list pages.