Reactjs 材质ui根据选定值选择字段和渲染列表
我有一个材料界面Reactjs 材质ui根据选定值选择字段和渲染列表,reactjs,material-ui,Reactjs,Material Ui,我有一个材料界面SelectField,里面填充了MenuItems。我的用例是,我希望能够从SelectField中选择一个值,然后将其显示在字段下方生成的列表中 我将如何着手开始这样做,以实现我想要的结果 任何帮助都将不胜感激 感谢您抽出时间因此,据我所知,您只想在列表中添加所选项目,并使用列表和列表项目显示项目 考虑: 选项-您的项目列表(选择字段中显示的项目) onChangeSelectField-SelectFieldonChange回调 //You could store the
SelectField
,里面填充了MenuItem
s。我的用例是,我希望能够从SelectField
中选择一个值,然后将其显示在字段下方生成的列表中
我将如何着手开始这样做,以实现我想要的结果
任何帮助都将不胜感激
感谢您抽出时间因此,据我所知,您只想在列表中添加所选项目,并使用
列表
和列表项目
显示项目
考虑:选项-您的项目列表(选择字段中显示的项目)
onChangeSelectField-
SelectField
onChange回调
//You could store the item`s ids inside an array. Ex:
onChangeSelectField(event, index, value) {
this.setState({
selectedItem: value,
selectedItems: this.state.selectedItems.concat(value)
})
}
//And inside the render function
render() {
<List>
{this.state.selectedItems.map((itemID) => {
var item = this.state.options.find(x => x.id === itemID)
return (
<ListItem primaryText={item.text} />
)
})}
</List>
}
//可以将项目的ID存储在数组中。前任:
onChangeSelectField(事件、索引、值){
这是我的国家({
selectedItem:value,
selectedItems:this.state.selectedItems.concat(值)
})
}
//和渲染函数内部
render(){
{this.state.selectedItems.map((itemID)=>{
var item=this.state.options.find(x=>x.id==itemID)
返回(
)
})}
}
因此,根据我的理解,您只需要将所选项目添加到列表中,并使用列表
和列表项目
显示项目
考虑:选项-您的项目列表(选择字段中显示的项目)
onChangeSelectField-
SelectField
onChange回调
//You could store the item`s ids inside an array. Ex:
onChangeSelectField(event, index, value) {
this.setState({
selectedItem: value,
selectedItems: this.state.selectedItems.concat(value)
})
}
//And inside the render function
render() {
<List>
{this.state.selectedItems.map((itemID) => {
var item = this.state.options.find(x => x.id === itemID)
return (
<ListItem primaryText={item.text} />
)
})}
</List>
}
//可以将项目的ID存储在数组中。前任:
onChangeSelectField(事件、索引、值){
这是我的国家({
selectedItem:value,
selectedItems:this.state.selectedItems.concat(值)
})
}
//和渲染函数内部
render(){
{this.state.selectedItems.map((itemID)=>{
var item=this.state.options.find(x=>x.id==itemID)
返回(
)
})}
}
我在项目中使用了相同的概念,但用于不同的目的
在我的项目中,有一个card元素和dropdown元素。在卡片媒体中,我使用了随机图像加载器([lorempixel.com][1])
,因此我在linkhttp://lorempixel.com/500/400/[类别]
(例如:http://lorempixel.com/500/400/nature
),因此它将更新链接,并在更改下拉列表时显示不同类别的图像
下面是我如何更新下拉列表更改的链接
onChange事件正在调用handleChange函数
onChange={this.handleChange}
在handleChange函数中,我正在更新状态
this.setState({
text: event.nativeEvent.target.innerText,
value: value
});
<img src={"http://lorempixel.com/500/400/"+this.state.text} />
在这里,我正在更新状态
this.setState({
text: event.nativeEvent.target.innerText,
value: value
});
<img src={"http://lorempixel.com/500/400/"+this.state.text} />
这是我写的代码,希望它能帮助你。如果你想要什么,请告诉我
import React from "react";
import Card from "material-ui/Card";
import CardActions from "material-ui/Card/CardActions";
import CardTitle from "material-ui/Card/CardTitle";
import CardHeader from "material-ui/Card/CardHeader";
import CardMedia from "material-ui/Card/CardMedia";
import CardText from "material-ui/Card/CardText";
import Drawer from "material-ui/Drawer";
import DropDownMenu from "material-ui/DropDownMenu";
import IconMenu from "material-ui/IconMenu";
import IconButton from "material-ui/IconButton";
import Menu from "material-ui/Menu";
import MenuItem from "material-ui/MenuItem";
import MoreVertIcon from "material-ui/svg-icons/navigation/more-vert";
const styles = {
margin: {
margin: "20px 300px"
},
float: {
float: "right"
},
};
class About extends React.Component {
constructor(props, context) {
super(props, context);
this.handleChange = this.handleChange.bind(this);
this.state = {
text: "nature",
value: 3
};
}
handleChange(event, index, value){
this.setState({
text: event.nativeEvent.target.innerText,
value: value
});
}
render(){
const IconMenuRight = (
<DropDownMenu
style={styles.float}
value={this.state.value}
iconButtonElement={<IconButton><MoreVertIcon /></IconButton>}
onChange={this.handleChange}>
<MenuItem value={1}>sports</MenuItem>
<MenuItem value={2} primaryText="city" />
<MenuItem value={3} primaryText="nature" />
<MenuItem value={4} primaryText="animals" />
<MenuItem value={5} primaryText="abstract" />
<MenuItem value={6} primaryText="cats" />
<MenuItem value={7} primaryText="food" />
<MenuItem value={8} primaryText="nightlife" />
<MenuItem value={9} primaryText="people" />
<MenuItem value={10} primaryText="technics" />
<MenuItem value={11} primaryText="transport" />
</DropDownMenu>
)
return(
<Card style={styles.margin}>
<CardHeader
avatar="http://lorempixel.com/400/200/people"
title="http://lorempixel.com/"
subtitle="A random image loader. Change the drop down value to see the diffent category image.">
{IconMenuRight}
</CardHeader>
<CardMedia>
<img src={"http://lorempixel.com/500/400/"+this.state.text} />
</CardMedia>
<CardTitle
title={"React js"}
subtitle="React is the entry point to the React library. If you're using one of the prebuilt packages it's available as a global; if you're using CommonJS modules you can require() it."/>
<CardText>
Webdesign or Print. Its simple and absolutely free! Just put the custom url in your code like this:
to get your FPO / dummy image.
</CardText>
</Card>
)
}
}
export default About;
从“React”导入React;
从“物料界面/卡片”导入卡片;
从“物料界面/卡片/卡片操作”导入卡片操作;
从“物料界面/卡片/卡片标题”导入卡片标题;
从“物料界面/卡片/卡片头”导入卡片头;
从“物料界面/卡片/卡片媒体”导入卡片媒体;
从“物料界面/卡片/卡片文本”导入卡片文本;
从“物料界面/抽屉”导入抽屉;
从“物料界面/下拉菜单”导入下拉菜单;
从“物料界面/图标菜单”导入图标菜单;
从“物料界面/图标按钮”导入图标按钮;
从“物料界面/菜单”导入菜单;
从“物料界面/菜单项”导入菜单项;
从“材质ui/svg图标/导航/更多垂直”导入MoreVertIcon;
常量样式={
保证金:{
保证金:“20px 300px”
},
浮动:{
浮动:“对”
},
};
类关于扩展React.Component{
构造函数(道具、上下文){
超级(道具、背景);
this.handleChange=this.handleChange.bind(this);
此.state={
案文:“自然”,
价值:3
};
}
handleChange(事件、索引、值){
这是我的国家({
文本:event.nativeEvent.target.innerText,
价值:价值
});
}
render(){
常数IconMenuRight=(
体育
)
返回(
{IconMenuRight}
网页设计或打印。它简单且绝对免费!只需将自定义url放入代码中,如下所示:
获取FPO/虚拟图像。
)
}
}
导出默认值约为;
我在项目中使用了相同的概念,但用于不同的目的
在我的项目中,有一个card元素和dropdown元素。在卡片媒体中,我使用了随机图像加载器([lorempixel.com][1])
,因此我在linkhttp://lorempixel.com/500/400/[类别]
(例如:http://lorempixel.com/500/400/nature
),因此它将更新链接,并在更改下拉列表时显示不同类别的图像
下面是我如何更新下拉列表更改的链接
onChange事件正在调用handleChange函数
onChange={this.handleChange}
在handleChange函数中,我正在更新状态
this.setState({
text: event.nativeEvent.target.innerText,
value: value
});
<img src={"http://lorempixel.com/500/400/"+this.state.text} />
在这里,我正在更新状态
this.setState({
text: event.nativeEvent.target.innerText,
value: value
});
<img src={"http://lorempixel.com/500/400/"+this.state.text} />
这是我写的代码,希望它能帮助你。如果你想要什么,请告诉我
import React from "react";
import Card from "material-ui/Card";
import CardActions from "material-ui/Card/CardActions";
import CardTitle from "material-ui/Card/CardTitle";
import CardHeader from "material-ui/Card/CardHeader";
import CardMedia from "material-ui/Card/CardMedia";
import CardText from "material-ui/Card/CardText";
import Drawer from "material-ui/Drawer";
import DropDownMenu from "material-ui/DropDownMenu";
import IconMenu from "material-ui/IconMenu";
import IconButton from "material-ui/IconButton";
import Menu from "material-ui/Menu";
import MenuItem from "material-ui/MenuItem";
import MoreVertIcon from "material-ui/svg-icons/navigation/more-vert";
const styles = {
margin: {
margin: "20px 300px"
},
float: {
float: "right"
},
};
class About extends React.Component {
constructor(props, context) {
super(props, context);
this.handleChange = this.handleChange.bind(this);
this.state = {
text: "nature",
value: 3
};
}
handleChange(event, index, value){
this.setState({
text: event.nativeEvent.target.innerText,
value: value
});
}
render(){
const IconMenuRight = (
<DropDownMenu
style={styles.float}
value={this.state.value}
iconButtonElement={<IconButton><MoreVertIcon /></IconButton>}
onChange={this.handleChange}>
<MenuItem value={1}>sports</MenuItem>
<MenuItem value={2} primaryText="city" />
<MenuItem value={3} primaryText="nature" />
<MenuItem value={4} primaryText="animals" />
<MenuItem value={5} primaryText="abstract" />
<MenuItem value={6} primaryText="cats" />
<MenuItem value={7} primaryText="food" />
<MenuItem value={8} primaryText="nightlife" />
<MenuItem value={9} primaryText="people" />
<MenuItem value={10} primaryText="technics" />
<MenuItem value={11} primaryText="transport" />
</DropDownMenu>
)
return(
<Card style={styles.margin}>
<CardHeader
avatar="http://lorempixel.com/400/200/people"
title="http://lorempixel.com/"
subtitle="A random image loader. Change the drop down value to see the diffent category image.">
{IconMenuRight}
</CardHeader>
<CardMedia>
<img src={"http://lorempixel.com/500/400/"+this.state.text} />
</CardMedia>
<CardTitle
title={"React js"}
subtitle="React is the entry point to the React library. If you're using one of the prebuilt packages it's available as a global; if you're using CommonJS modules you can require() it."/>
<CardText>
Webdesign or Print. Its simple and absolutely free! Just put the custom url in your code like this:
to get your FPO / dummy image.
</CardText>
</Card>
)
}
}
export default About;
从“React”导入React;
从“物料界面/卡片”导入卡片;
从“物料界面/卡片/卡片操作”导入卡片操作;
从“物料界面/卡片/卡片标题”导入卡片标题;
从“物料界面/卡片/卡片头”导入卡片头;
从“物料界面/卡片/卡片媒体”导入卡片媒体;
从“物料界面/卡片/卡片文本”导入卡片文本;
从“物料界面/抽屉”导入抽屉;
从“物料界面/下拉菜单”导入下拉菜单;
从“物料界面/图标菜单”导入图标菜单;
从“物料界面/图标按钮”导入图标按钮;
从“物料界面/菜单”导入菜单;
从“物料界面/菜单项”导入菜单项;
从“材质ui/svg图标/导航/更多垂直”导入MoreVertIcon;
常量样式={
保证金:{
边距:“2