-1

This is almost everywhere. For example, I need to pick a fruit from a list of fruits from a dropdown. And the last option on that dropdown is "Other", by selecting that another text field appears to allow users to type their favorite food.

Assuming all the data comes from database, what would be the best way to implement it on the front-end?

Right now, what I can think of is giving the "Other" option a special Id in database, and checking against that in UI. Which means saving database Id in my front-end code.

The other option is just checking for the "Other" text. Which seems ugly too.

Is there any better approach to this problem?

  • Why don't you just tag the 'Other' ComboBoxItem? And interrogate the selected tag at the right time. – JᴀʏMᴇᴇ Jun 18 '18 at 8:12
0

Checking for "Other" will work just fine ... right up until you decide you want to express it differently or you want to support a language other than English. It's a fragile solution: it's quick and easy to implement but it'll break all too easily.

Storing an ID in the UI likewise is fairly easy to do. But it couples that UI to a specific set of data too. Accidentally drop that record from the database and recreate it and you could end up with a different ID. And every time you add a new drop down, that's another ID coupling the the two. It's a little less fragile, but is still fragile and starts to turn your code into spaghetti.

A more reliable solution just involves decoupling the "other" special case from those existing fields. The simplest way to do this is to add a new boolean field to those tables that denotes whether a specific record is the special case or not. This is simple, but does run the risk of more than one field being marked as "other". Another solution is to have an "Others" table that records the special case ID for a specific table. That table can then be loaded into the app at start-up. It does the same as hard-coding those special IDs in the app, but avoids the need to re-release the code if an ID changes.

  • +1 for having a boolean field to mark a special option. It's a unique idea. Although it feels weird to add a field to mark just one option as "special" – SZT Jun 18 '18 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.