【问题标题】:Material-ui select fields and rendering lists based on the selected valuesMaterial-ui根据选择的值选择字段和渲染列表
【发布时间】:2016-05-05 12:31:22
【问题描述】:

我有一个 Material-ui SelectField 填充有 MenuItems。我的用例是我希望能够从 SelectField 中选择一个值,然后将其显示在字段下方生成的列表中。

我将如何着手开始这样做,以实现我想要的结果?

任何帮助将不胜感激

感谢您的宝贵时间

【问题讨论】:

  • 到目前为止你尝试过什么?
  • 我正在考虑从选择字段中获取选择的值,然后将该值放入 ListItem 以作为列表的一部分显示。问题在于我可能想用字段中的值填充列表。在我更新值后保持旧值是我正在努力解决的问题。由于我刚刚完成了表单本身的设计(它有很多活动部件!),所以目前没有任何代码可以显示。
  • 所以在选择其中的项目后 - 该项目应该添加到您所在州的列表中,对吗?如果用户选择了两次相同的项目怎么办?
  • 它会在列表中出现一次。理想情况下,如果您单击选择字段中的某个项目,它将显示在列表中,但如果您在提交表单之前不想再将其从列表中删除,您也可以将其从列表中删除

标签: reactjs material-ui


【解决方案1】:

因此,据我了解,您只想将所选项目添加到列表中并使用 ListListItem 显示项目。

考虑:
options - 您的项目列表(显示在选择字段中的项目)
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>
} 

【讨论】:

  • 嗨,安德烈,对不起,我没有很快回复你,被困在火车上很久了。一旦我有机会测试您的解决方案,我会回复您,但这与我的想法一致,我只是无法在脑海中解决!
  • 经过一些调整以适应它,经过测试并成功运行。再次感谢您的帮助!
  • 如何从列表中删除项目?
  • onRemoveItem(id) { this.setState({ selectedItems: this.state.selectedItems.filter(x =&gt; x.id !== id)}}
【解决方案2】:

我在我的项目中做了相同的概念,但用于不同的目的

在我的项目中,有一个带有下拉元素的卡片元素。在卡片媒体中,我使用了random image loader([lorempixel.com][1]),因此我正在更改链接http://lorempixel.com/500/400/[category](例如: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} />

这是我写的代码,希望对你有帮助。如果你想要什么,请告诉我

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多