React - 带有对象的语义 ui 下拉列表
React - semantic ui dropdown with object
我正在映射对象数组并在带有 semantic-ui-react 的下拉选择中使用名称。
我有一些模拟数据
mock.onGet("/dataschemas").reply(200, {
data: [
{
id: "2147483599",
selfUri: "/dataschemas/2147483599",
name: "Book Catalog"
},
{
id: "2147483600",
selfUri: "/dataschemas/2147483600",
name: "Business Articles"
}
]
});
在 cDM 中,我将对象更新为 dataschemas
async componentDidMount() {
await this.getSchemas();
}
getSchemas = async () => {
try {
const { data } = await axios.get("/dataschemas");
console.log(data);
const dataschemas = data.data;
this.setState({ dataschemas: dataschemas });
console.log("This is the dataschema list:", dataschemas);
} catch (error) {
console.error(Error(`Error fetching results list: ${error.message}`));
}
};
我的更改处理函数如下所示:
handleSchemaChange = (e, { value }) => this.setState({ name: value });
然后在渲染中我使用 <Dropdown>
组件
render() {
const { dataschemas } = this.state;
return (
<div>
<div>
<label>Select a dataschema: </label>
<Dropdown
placeholder="Select data schema"
clearable
fluid
selection
multiple={true}
options={dataschemas}
header="PLEASE SELECT A DATASCHEMA"
value={dataschemas.filter(({ name }) => name === this.state.name)}
onChange={this.handleSchemaChange}
/>
</div>
</div>
);
}
我无法让数据模式 names
显示在下拉列表中,也无法让标签在进行选择时更新。我不知道我是否缺少道具或没有正确更新状态,有什么想法吗?
Codesandbox here
如 https://react.semantic-ui.com/modules/dropdown/ 中所述,您应该使用以下结构在下拉列表中显示对象。
{
"key": "",
"text": "",
"value": ""
}
示例:将其用作下拉列表中的选项值
options={dataschemas.map(ds => {
return {
key: ds.id,
text: ds.name,
value: ds.selfUri
}
})}
我正在映射对象数组并在带有 semantic-ui-react 的下拉选择中使用名称。
我有一些模拟数据
mock.onGet("/dataschemas").reply(200, {
data: [
{
id: "2147483599",
selfUri: "/dataschemas/2147483599",
name: "Book Catalog"
},
{
id: "2147483600",
selfUri: "/dataschemas/2147483600",
name: "Business Articles"
}
]
});
在 cDM 中,我将对象更新为 dataschemas
async componentDidMount() {
await this.getSchemas();
}
getSchemas = async () => {
try {
const { data } = await axios.get("/dataschemas");
console.log(data);
const dataschemas = data.data;
this.setState({ dataschemas: dataschemas });
console.log("This is the dataschema list:", dataschemas);
} catch (error) {
console.error(Error(`Error fetching results list: ${error.message}`));
}
};
我的更改处理函数如下所示:
handleSchemaChange = (e, { value }) => this.setState({ name: value });
然后在渲染中我使用 <Dropdown>
组件
render() {
const { dataschemas } = this.state;
return (
<div>
<div>
<label>Select a dataschema: </label>
<Dropdown
placeholder="Select data schema"
clearable
fluid
selection
multiple={true}
options={dataschemas}
header="PLEASE SELECT A DATASCHEMA"
value={dataschemas.filter(({ name }) => name === this.state.name)}
onChange={this.handleSchemaChange}
/>
</div>
</div>
);
}
我无法让数据模式 names
显示在下拉列表中,也无法让标签在进行选择时更新。我不知道我是否缺少道具或没有正确更新状态,有什么想法吗?
Codesandbox here
如 https://react.semantic-ui.com/modules/dropdown/ 中所述,您应该使用以下结构在下拉列表中显示对象。
{
"key": "",
"text": "",
"value": ""
}
示例:将其用作下拉列表中的选项值
options={dataschemas.map(ds => {
return {
key: ds.id,
text: ds.name,
value: ds.selfUri
}
})}