0

当您在 StackOverflow 中有很多关于基于其他下拉列表的反应下拉列表的示例时,这个问题可能听起来像是一个重复的问题。出于某种奇怪的原因,我无法解决我的代码中的问题。我不太确定这是否由于 MDBract 框架而发生。我对反应也很陌生。伙计们的任何帮助将不胜感激。

import React, { Component } from "react";
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 的新手

4

1 回答 1

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>
 );
}

工作示例

于 2019-04-21T11:13:15.087 回答