跪拜 Guibai
← All articles
Frontend · JavaScript · Interview

One Search Box, Zero Advanced Filters: Match Select Options by Both ID and Name

By 我叫黑大帅 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Admin UIs keep a single search input without a separate filter form, and users can filter by either a human-readable name or a machine ID. The pattern is small and eager to reuse on any table-heavy page that already knows its select options.

Summary

Global search in admin lists usually does substring matching across all fields, so a query for "Zhang San" also drags in rows whose notes mention "Zhang San's customer". The typical fix, an advanced filter form, adds extra clicks and complexity. A different solution keeps one search box but makes it option-aware.

When the user types, the box builds suggestions by scanning select-type fields and their predefined option lists. It matches each option's display name and its internal ID, and for ID matches it shows the ID in the label. Text and number fields still fall back to "contains" suggestions. The result is a merged dropdown where exact option matches appear first, and the user picks one to create an EQUAL filter condition, while the backend receives the option's ID.

Takeaways
— Match select options on both id and name fields; use name for display matches and id for exact backend values.
— Limit suggestions to 10 to keep the input responsive when a field has hundreds of options.
— Combine option suggestions (exact matches) ahead of text contains suggestions so the most precise results sit on top.
— The FilterCondition structure carries field, operator, value, and displayText so the search logic remains schema-driven and reusable across list pages.
— No extra API call is needed for matching because the page already holds the full option list.
Conclusions

The interaction reverses the classic "choose field, then choose value" flow into "type a value, then pick a field", which cuts the number of steps for well-known entries.

Putting the match logic front-end avoids extra network round trips and keeps the search itself instant, which matters when lists have many records.

This pattern works because enum fields in backend systems are usually stored as IDs while users think in names; the dual attribute is the key enabler.

Concepts & terms
FilterCondition
A neutral object that describes a single filter: which field, which operator (CONTAIN or EQUAL), the backend value, and the text shown on the filter tag. This lets the search box generate conditions without hard-coding business fields.
SelectOption
The pair of id and name attached to each dropdown choice, where id is the value stored in the database and name is the human-readable label displayed in the UI.
Option-aware suggestion
A suggestion built by matching the user's input against a select field's predefined options, on both the id and name, so typing either a name or a code produces an exact-match filter instead of a contains search.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗