React下拉选择:如何访问比显示的数组更多的字段?

huangapple go评论81阅读模式
英文:

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 = () =&gt; {
   const [allRaws, setAllRaws] = useState([])
   const [rawID, setRawsID] = useState(&#39;&#39;)
   
   setAllRaws([
    {&quot;ID&quot; : 1, &quot;name&quot; : &quot;raw1&quot;, &quot;description&quot; : &quot;description 1&quot;},
    {&quot;ID&quot; : 2, &quot;name&quot; : &quot;raw2&quot;, &quot;description&quot; : &quot;description 2&quot;},
    {&quot;ID&quot; : 3, &quot;name&quot; : &quot;raw3&quot;, &quot;description&quot; : &quot;description 3&quot;},
   ])

    const handleSelectRaws = (event, value) =&gt;{
        if (value != null){
            const id = value.split(&quot;*&quot;).pop()
            setRawsID(id)
        }
    }

   return (
       &lt;AutocompleteForm 
                array={allRaws.map((raw) =&gt; ([String(raw.name)]+ &quot;: &quot; + String(raw.description) + 
                 &quot;*&quot; + String(raw.id))} 
                label=&quot;Pick set of raws&quot; 
                onChange={handleSelectRaws} /&gt;}
   )

}

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 (
     &lt;Autocomplete
       disablePortal
       options={array}
       sx={{ width: 300 }}
       renderInput={(params) =&gt; &lt;TextField {...params} label={label}
        /&gt;}
        onChange={onChange}
     /&gt;
   )
 }

答案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 = [
  {&quot;id&quot; : 1, &quot;name&quot; : &quot;raw1&quot;, &quot;description&quot; : &quot;description 1&quot;},
  {&quot;id&quot; : 2, &quot;name&quot; : &quot;raw2&quot;, &quot;description&quot; : &quot;description 2&quot;},
  {&quot;id&quot; : 3, &quot;name&quot; : &quot;raw3&quot;, &quot;description&quot; : &quot;description 3&quot;}
]

function Form() {
  const [allRaws, setAllRaws] = useState(initialState)
  const [rawID, setRawsID] = useState(null)

  const handleSelectRaws = (event, value) =&gt; {
    if (value != null) {
      setRawsID(value.id)
    }
  }

  return (
    &lt;AutocompleteForm 
      array={allRaws} 
      label=&quot;Pick set of raws&quot; 
      onChange={handleSelectRaws} 
    /&gt;
  )
}
export default function AutocompleteForm(props) {
  const {label, array, onChange} = props

  return (
    &lt;Autocomplete
      disablePortal
      options={array}
      sx={{ width: 300 }}
      renderInput={(params) =&gt; &lt;TextField {...params} label={label}/&gt;}
      onChange={onChange}
      getOptionLabel={(option) =&gt; `${option.name}: ${option.description}`} 
    /&gt;
  )
}

huangapple
  • 本文由 发表于 2023年6月6日 04:22:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定