英文:
React dropdown select: how to have access to more fields than the array displayed?
问题
我有一个包含ID、名称和描述字段的JSON数组。我想要创建一个下拉选择框,其中显示名称和描述,并在点击时将关联的ID存储到rawID状态中。我尚未找到一种方法可以在不显示ID的情况下执行此操作并提取它。但是,这对用户来说不太清晰,因为框中的每个选项都会像这样显示:“raw1:description1 * id号”。是否有一种直接将ID传递到onClick函数的方法?
const form = () => {
const [allRaws, setAllRaws] = useState([])
const [rawID, setRawsID] = useState('')
setAllRaws([
{"ID" : 1, "name" : "raw1", "description" : "description 1"},
{"ID" : 2, "name" : "raw2", "description" : "description 2"},
{"ID" : 3, "name" : "raw3", "description" : "description 3"},
])
const handleSelectRaws = (event, value) =>{
if (value != null){
const id = value.split("*").pop()
setRawsID(id)
}
}
return (
<AutocompleteForm
array={allRaws.map((raw) => ([String(raw.name)]+ ": " + String(raw.description) +
"*" + String(raw.id)))
label="选择一组原始数据"
onChange={handleSelectRaws} />
)
}
autocompleteForm组件的定义如下:
它以一个要显示的字符串数组、一个告诉用户该做什么的标签和一个onChange函数作为props。
export default function AutocompleteForm(props) {
const { label, array, onChange} = props
return (
<Autocomplete
disablePortal
options={array}
sx={{ width: 300 }}
renderInput={(params) => <TextField {...params} label={label}
/>}
onChange={onChange}
/>
)
}
英文:
I have an array of jsons that have the fields ID, name and description. I'd like to make a dropdown selection box that displays both the name and description, and upon clicking on it, will store the associated id to the rawID state. I haven't found a way to do this without displaying the id in the select box and extracting it. However, this doesn't look very clean to the user, since every option in the box will look like this raw1: description1 *id number
. Is there a way to directly pass the id into the onClick function?
const form = () => {
const [allRaws, setAllRaws] = useState([])
const [rawID, setRawsID] = useState('')
setAllRaws([
{"ID" : 1, "name" : "raw1", "description" : "description 1"},
{"ID" : 2, "name" : "raw2", "description" : "description 2"},
{"ID" : 3, "name" : "raw3", "description" : "description 3"},
])
const handleSelectRaws = (event, value) =>{
if (value != null){
const id = value.split("*").pop()
setRawsID(id)
}
}
return (
<AutocompleteForm
array={allRaws.map((raw) => ([String(raw.name)]+ ": " + String(raw.description) +
"*" + String(raw.id))}
label="Pick set of raws"
onChange={handleSelectRaws} />}
)
}
and the autocompleteForm component is defined as such:
It takes as props an array of strings to display, a label which tells the user what to do and a function onChange.
export default function AutocompleteForm(props) {
const { label, array, onChange} = props
return (
<Autocomplete
disablePortal
options={array}
sx={{ width: 300 }}
renderInput={(params) => <TextField {...params} label={label}
/>}
onChange={onChange}
/>
)
}
答案1
得分: 1
From the Autocomplete documentation, getOptionLabel
属性用于确定给定选项的字符串值。
您可以将allRaws
数组传递给Autocomplete
选项,无需进行任何转换,并使用getOptionLabel
指定选项标签。
const initialState = [
{"id": 1, "name": "raw1", "description": "description 1"},
{"id": 2, "name": "raw2", "description": "description 2"},
{"id": 3, "name": "raw3", "description": "description 3"}
]
function Form() {
const [allRaws, setAllRaws] = useState(initialState)
const [rawID, setRawsID] = useState(null)
const handleSelectRaws = (event, value) => {
if (value != null) {
setRawsID(value.id)
}
}
return (
<AutocompleteForm
array={allRaws}
label="Pick set of raws"
onChange={handleSelectRaws}
/>
)
}
export default function AutocompleteForm(props) {
const {label, array, onChange} = props
return (
<Autocomplete
disablePortal
options={array}
sx={{ width: 300 }}
renderInput={(params) => <TextField {...params} label={label}/>}
onChange={onChange}
getOptionLabel={(option) => `${option.name}: ${option.description}`}
/>
)
}
英文:
From the Autocomplete documentation, getOptionLabel
property is used to determine the string value for a given option.
You can pass the allRaws
array to the Autocomplete
options without any transformation and use getOptionLabel
to specify the option label.
const initialState = [
{"id" : 1, "name" : "raw1", "description" : "description 1"},
{"id" : 2, "name" : "raw2", "description" : "description 2"},
{"id" : 3, "name" : "raw3", "description" : "description 3"}
]
function Form() {
const [allRaws, setAllRaws] = useState(initialState)
const [rawID, setRawsID] = useState(null)
const handleSelectRaws = (event, value) => {
if (value != null) {
setRawsID(value.id)
}
}
return (
<AutocompleteForm
array={allRaws}
label="Pick set of raws"
onChange={handleSelectRaws}
/>
)
}
export default function AutocompleteForm(props) {
const {label, array, onChange} = props
return (
<Autocomplete
disablePortal
options={array}
sx={{ width: 300 }}
renderInput={(params) => <TextField {...params} label={label}/>}
onChange={onChange}
getOptionLabel={(option) => `${option.name}: ${option.description}`}
/>
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论