Javascript 获得;ID";使用材质ui自动完成选定值的
我将自动完成组件与材质ui一起使用Javascript 获得;ID";使用材质ui自动完成选定值的,javascript,reactjs,autocomplete,material-ui,Javascript,Reactjs,Autocomplete,Material Ui,我将自动完成组件与材质ui一起使用 搜索字段中“customerList”中的数据没有问题。但我无法获取所选数据的“ID”编号。我想获取所选数据的ID号 此外,还会出现如下错误。我怎样才能解决这个问题 从'@material ui/lab/Autocomplete'导入自动完成; 类指定FormContainerBasic扩展React.PureComponent{ 建造师(道具){ 超级(道具); } render(){ 常量textEditorProps=(字段)=>({ 变体:“概述
从'@material ui/lab/Autocomplete'导入自动完成;
类指定FormContainerBasic扩展React.PureComponent{
建造师(道具){
超级(道具);
}
render(){
常量textEditorProps=(字段)=>({
变体:“概述”,
onChange:({target:change})=>this.changeAppointment({
字段:[字段],更改:change.value
}),
值:displayAppointmentData[字段]| |“”,
className:classes.textField
});
const customerList=[
{id:“1”,客户名称:“John”,客户名称:“test0”},
{id:“2”,customerName:'Bob',CustomerUserName:'test1},
{id:“3”,客户名称:“steve”,客户名称:“test2”},
{id:“4”,客户名称:“steve”,客户名称:“test3”},
{id:“4”,客户名称:“natalia”,客户名称:“test4”}
];
返回(
{
if(top100Films.customerName==未定义){
console.log(“未定义”)
}否则{
console.log(“option.customerName”)
}
返回option.customerName+“”+option.customerUserName;
})}
defaultValue={displayAppointmentData['customerName']}
onInputChange={(事件,newInputValue)=>{
这是我的约会({
字段:['customerName',更改:newInputValue,
值:DisplayAppointData[newInputValue]| |“”,
});
}}
/>
);
}
}
编辑
从下面的示例中可以看到,onChange方法的newValue参数返回一个对象
import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
export default function ComboBox() {
return (
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={option => option.title}
style={{ width: 300 }}
renderInput={params => (
<TextField {...params} label="Combo box" variant="outlined" />
)}
onChange={(event, newValue) => {
console.log(JSON.stringify(newValue, null, ' '));
}}
/>
);
}
const top100Films = [
{ id: 0, title: "The Shawshank Redemption", year: 1994 },
{ id: 1, title: "The Godfather", year: 1972 },
{ id: 2, title: "The Godfather: Part II", year: 1974 },
{ id: 3, title: "The Dark Knight", year: 2008 },
{ id: 4, title: "12 Angry Men", year: 1957 }
];
从“React”导入React;
从“@material ui/core/TextField”导入TextField;
从“@material ui/lab/Autocomplete”导入自动完成;
导出默认函数组合框(){
返回(
不幸的是,您的代码只是一个片段,很难确定为什么它在您的情况下不起作用。是的,我可以获得我已经选择的值。但是我无法获得我选择的值的ID号。我想获得ID号用户将看到“customerName”他们选择的字段。在后台;将接收所选的ID号。{ID:“1”,customerName:“John”,CustomerUserName:“test0”},这方面运气好吗?我也面临同样的问题。需要获取对象的ID,而不是显示值。
import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
export default function ComboBox() {
return (
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={option => option.title}
style={{ width: 300 }}
renderInput={params => (
<TextField {...params} label="Combo box" variant="outlined" />
)}
onChange={(event, newValue) => {
console.log(JSON.stringify(newValue, null, ' '));
}}
/>
);
}
const top100Films = [
{ id: 0, title: "The Shawshank Redemption", year: 1994 },
{ id: 1, title: "The Godfather", year: 1972 },
{ id: 2, title: "The Godfather: Part II", year: 1974 },
{ id: 3, title: "The Dark Knight", year: 2008 },
{ id: 4, title: "12 Angry Men", year: 1957 }
];