在选择框中处理“其他”选项的最佳方法是什么?

时间:2022-11-16 18:08:19

I'm needing to implement a select box that has an "other" option. If this is selected the user should be able to type in a different option into a Text Field. My concern is for how to implement the save into the database. I'm looking for unique and efficient ways to handle this implementation.

我需要实现一个具有“其他”选项的选择框。如果选择此选项,则用户应该能够在文本字段中键入其他选项。我关心的是如何实现保存到数据库中。我正在寻找独特而有效的方法来处理这种实现。

I believe the last time I implemented this i had two columns in my table, one for the select box and one for the text field input.

我相信上次我实现这个时,我的表中有两列,一列用于选择框,另一列用于文本字段输入。

Can anyone think of a better way to do this?

谁能想到更好的方法呢?

2 个解决方案

#1


2  

I would have a reference table that populates your select box with fields: id, item, primary. Where 'primary' is a bool indicating if it should be included in your drop down list or not.

我会有一个参考表,用你的字段填充你的选择框:id,item,primary。其中'primary'是bool,表示它是否应该包含在您的下拉列表中。

Then when users input Other items, the items will be added to your reference table with 'primary' field being false.

然后,当用户输入其他项目时,这些项目将被添加到您的参考表格中,其中“primary”字段为false。

The nice thing about this implementation is that it's conceptually clean - there is only one field that stores your item values, and if many of your users are adding a specific field for Other, you can just just flip the 'primary' bool to true and it will appear in your main drop down list.

这个实现的好处在于它在概念上是干净的 - 只有一个字段存储您的项目值,如果您的许多用户正在为其他人添加特定字段,您只需将'primary'bool翻转为true即可它将出现在您的主下拉列表中。

#2


1  

Assuming you have some kind of a meta data table that drives your select box options, you can add logic that inserts the new "other" option into that table and then reference your newly added item's primary key in your main table that stores the form entered data.

假设您有一些驱动您的选择框选项的元数据表,您可以添加逻辑,将新的“其他”选项插入该表,然后在主表中引用新添加的项目的主键,该主键存储输入的表单数据。

If you don't want to muddy up the meta data table with every "other" submission, then I believe your approach is good - just a separate nullable column that stores "other" value.

如果您不想在每次“其他”提交时混淆元数据表,那么我相信您的方法很好 - 只是一个存储“其他”值的单独的可空列。

#1


2  

I would have a reference table that populates your select box with fields: id, item, primary. Where 'primary' is a bool indicating if it should be included in your drop down list or not.

我会有一个参考表,用你的字段填充你的选择框:id,item,primary。其中'primary'是bool,表示它是否应该包含在您的下拉列表中。

Then when users input Other items, the items will be added to your reference table with 'primary' field being false.

然后,当用户输入其他项目时,这些项目将被添加到您的参考表格中,其中“primary”字段为false。

The nice thing about this implementation is that it's conceptually clean - there is only one field that stores your item values, and if many of your users are adding a specific field for Other, you can just just flip the 'primary' bool to true and it will appear in your main drop down list.

这个实现的好处在于它在概念上是干净的 - 只有一个字段存储您的项目值,如果您的许多用户正在为其他人添加特定字段,您只需将'primary'bool翻转为true即可它将出现在您的主下拉列表中。

#2


1  

Assuming you have some kind of a meta data table that drives your select box options, you can add logic that inserts the new "other" option into that table and then reference your newly added item's primary key in your main table that stores the form entered data.

假设您有一些驱动您的选择框选项的元数据表,您可以添加逻辑,将新的“其他”选项插入该表,然后在主表中引用新添加的项目的主键,该主键存储输入的表单数据。

If you don't want to muddy up the meta data table with every "other" submission, then I believe your approach is good - just a separate nullable column that stores "other" value.

如果您不想在每次“其他”提交时混淆元数据表,那么我相信您的方法很好 - 只是一个存储“其他”值的单独的可空列。