基于使用MDB-react.js的第一个下拉列表选择

时间:2019-04-21 10:15:25

标签: reactjs bootstrap-material-design

当您在stackoverflow中有很多关于基于其他下拉列表的反应下拉列表的示例时,这个问题听起来像是重复的问题。 由于某些奇怪的原因,我无法在代码中解决该问题。 我不确定由于MDBreact框架是否会发生这种情况。大家的帮助将不胜感激。

import {
  MDBContainer,
  MDBBtn,
  MDBModal,
  MDBModalBody,
  MDBModalHeader,
  MDBModalFooter
} from "mdbreact";


import "../../App.css";

class ModalInvenAddItems extends Component {
  constructor(props) {
    super(props);
    this.state = {
      modal: false,
      tools: [
        {
          name: "machine",
          lists: [
            "Lathe",
            "Milling m/c",
            "drillingm/c",
            "hand grinder",
            "hand drill",
            "pedestal grinder",
            "belt sander",
            "hand chainsaw  ",
            "Angle Grinder"
          ]
        },
        {
          name: "Hand-tools",
          lists: [
            "spanners",
            "screw drivers",
            "hammers",
            "allenkeys",
            "punch",
            "hacksaw",
            "files",
            "pliers",
            "center punches",
            "scribes"
          ]
        },
        {
          name: "cutting-tools",
          lists: [
            "Auger Bits",
            "Taper shank Twist drill",
            "strisght shank Twist drill",
            "Countersink Drills",
            "mill cutters",
            "lathe tools",
            "taps",
            "threadcutting tools",
            "oil stones",
            "emery papers"
          ]
        },
        {
          name: "Measurment-Instruments",
          lists: [
            "vernier-calipers",
            "ruler",
            "compass",
            "x-callipers",
            "spirit level",
            "tape"
          ]
        },
        {
          name: "cleaning-tools",
          lists: ["brushes", "Dustbins", "Vacuum Cleaner", "Rugs","Dustpans "]
        },
        {
          name: "fastners",
          lists: [
            "screws",
            "wood screws",
            "machine screws",
            "thread cutting m/c screws",
            "nuts",
            "Hex",
            "heavy hex",
            "flange",
            "washers",
            "flat",
            "fender",
            "external tooth lock",
            "split lock",
            "internal Tooth lock",
            "bolts",
            "revet"
          ]
        },
        {
          name: "fluids",
          lists: ["cutting oil", "lubrication", "glues"]
        },
        {
          name: "Clamping-tools",
          lists: [
            "F-clamps",
            "bar-clamps",
            "c-clamps",
            "vices",
            "t-bolts",
            "step blocks",
            "parallel blocks"
          ]
        },
        {
          name: "safety-equipments",
          lists: ["goggles", "ear-muffs"]
        }
      ],
      selectedCategory: "select the category",
      selectedList: []
    };
    this.toggle = this.toggle.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }

  toggle = () => {
    this.setState({
      modal: !this.state.modal
    });
  };

  handleChange(e) {
    this.setState({ selectedCategory: e.target.value });
  }

  render() {
    let divstyle = {
      color: "blue",
      fontStyle: "italic",
      fontWeight: "semi-bold"
    };

    let toolgroup = this.state.tools.filter(tool => {
      return tool.name === this.state.selectedCategory;
    });

    return (
      <MDBContainer>
        <a
          style={divstyle}
          className="btnadditem"
          title="Add new items"
          onClick={this.toggle}
        >
          <i className="fas fa-plus" />
        </a>
        <MDBModal
          id="modalAdditems"
          isOpen={this.state.modal}
          toggle={this.toggle}
        >
          <MDBModalHeader toggle={this.toggle}>
            Add new Inventory Item
          </MDBModalHeader>
          <MDBModalBody>
            <div className="form-group">
              <label>Description</label>
              <input type="email" className="form-control invModal" />
              <label>Category</label>
              <div>
                <select
                  className="browser-default custom-select"
                  onChange={this.handleChange}
                  value={this.state.selectedCategory}
                >
                  {this.state.tools.map((toolCategory, i) => {
                    return <option key={i}>{toolCategory.name}</option>;
                  })}
                </select>
              </div>
              <div>
                <select>
                  {toolgroup[0].lists.map((list, i) => {
                    return <option key={i}>{list}</option>;
                  })}
                </select>
              </div>
            </div>
          </MDBModalBody>
          <MDBModalFooter>
            <MDBBtn color="secondary" onClick={this.toggle}>
              Close
            </MDBBtn>
            <MDBBtn color="primary" onClick={this.postdata}>
              Save changes
            </MDBBtn>
          </MDBModalFooter>
        </MDBModal>
      </MDBContainer>
    );
  }
}

export default ModalInvenAddItems;

我收到的错误是“ TypeError:无法读取未定义的属性'列表'”

如果我的问题听起来很愚蠢,请提前抱歉。我是reactjs的新手

1 个答案:

答案 0 :(得分:1)

您需要验证数据,首先在应用运行lists时未定义并引发错误。将您的渲染方法更改为以下内容。

render() {
let divstyle = {
  color: "blue",
  fontStyle: "italic",
  fontWeight: "semi-bold"
};

let toolgroup = this.state.tools.filter(tool => {
  return tool.name === this.state.selectedCategory;
});

// validate lists and set it to empty array, if toolgroup is empty 
const lists = toolgroup && toolgroup.length && toolgroup[0].lists || [];

// create select option values
const items = this.state.tools.map((toolCategory, i) => (
  <option key={i}>{toolCategory.name}</option>
));

// create second select option items
const toolgroupItems = lists.map((list, i) => (
  <option key={i}>{list}</option>
));

return (
  <MDBContainer>
    <a
      style={divstyle}
      className="btnadditem"
      title="Add new items"
      onClick={this.toggle}
    >
      <i className="fas fa-plus" />
    </a>
    <MDBModal
      id="modalAdditems"
      isOpen={this.state.modal}
      toggle={this.toggle}
    >
      <MDBModalHeader toggle={this.toggle}>
        Add new Inventory Item
      </MDBModalHeader>
      <MDBModalBody>
        <div className="form-group">
          <label>Description</label>
          <input type="email" className="form-control invModal" />
          <label>Category</label>
          <div>
            <select
              className="browser-default custom-select"
              onChange={this.handleChange}
              value={this.state.selectedCategory}
            >
              {items}
            </select>
          </div>
          <div>
            <select>
              {toolgroupItems}
            </select>

          </div>
        </div>
      </MDBModalBody>
      <MDBModalFooter>
        <MDBBtn color="secondary" onClick={this.toggle}>
          Close
        </MDBBtn>
        <MDBBtn color="primary" onClick={this.postdata}>
          Save changes
        </MDBBtn>
      </MDBModalFooter>
    </MDBModal>
  </MDBContainer>
 );
}

working example