根据材料用户界面

时间:2017-12-01 16:30:18

标签: javascript reactjs material-ui

我有3个带有Material UI的下拉组件,我想要做的是禁用第二个和第三个组件,并在从上一个下拉列表中选择一个选项后启用它们。例如,第二个下拉列表将在从第一个下拉列表中选择内容后启用,第三个下拉列表将在我从第二个下拉列表中选择一个选项时启用,依此类推。

这是我的代码:

import React from 'react';
import DropDownMenu from 'material-ui/DropDownMenu';
import MenuItem from 'material-ui/MenuItem';

import cr from '../styles/general.css';


export default class CreateLinksave extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      startDate: '',
      endDate: '',
      DivisionData: [],
      StoreGroupingData: [],
      OfferTypeData: [],
      DivisionState: '',
      OfferTypeState: '',
      StoreGroupingState: ''
    };
    this.handleChange = this.handleChange.bind(this);
    this.renderStoreGroupingOptions = this.renderStoreGroupingOptions.bind(this);
    this.renderDivisionOptions = this.renderDivisionOptions.bind(this);
    this.renderOfferTypeOptions = this.renderOfferTypeOptions.bind(this);
    this.handleChangeDivision = this.handleChangeDivision.bind(this);
    this.handleChangeStoreGrouping = this.handleChangeStoreGrouping.bind(this);
    this.handleChangeDiscountType = this.handleChangeDiscountType.bind(this);
  }

  componentDidMount() {
    const divisionWS = 'http://localhost:8080/services/Divisions/getAll';
    const offerTypeWS = 'http://localhost:8080/services/OfferType/getAll';
    const storeGroupingWS = 'http://localhost:8080/services/Rules/getRuleTextDtoQuery/Y/ENG';

    fetch(divisionWS)
      .then(Response => Response.json())
      .then(findResponse => {
        console.log(findResponse);

        this.setState({
          DivisionData: findResponse,
          DivisionState: findResponse[0].divDeptShrtDesc
        });
      });

    fetch(storeGroupingWS)
      .then(Response => Response.json())
      .then(findResponse => {
        console.log(findResponse);

        this.setState({
          StoreGroupingData: findResponse,
          StoreGroupingState: findResponse[0].ruleDesc
        });
      });

    fetch(offerTypeWS)
      .then(Response => Response.json())
      .then(findResponse => {
        console.log(findResponse);

        this.setState({
          OfferTypeData: findResponse,
          OfferTypeState: findResponse[0].offerTypeDesc
        });
      });
  }

  handleChange(event, index, value) {this.setState({value});}

  handleChangeDivision(event, index, value) {
    this.setState({ DivisionState: (value) });
  }

  handleChangeStoreGrouping(event, index, value) {
    this.setState({ StoreGroupingState: (value) });
  }

  handleChangeDiscountType(event, index, value) {
    this.setState({ OfferTypeState: (value) });
  }

  renderDivisionOptions() {
    return this.state.DivisionData.map((dt, i) => {
      return (
        <MenuItem
          key={i}
          value={dt.divDeptShrtDesc}
          primaryText={dt.divDeptShrtDesc} />
      );
    });
  }

  renderStoreGroupingOptions() {
    return this.state.StoreGroupingData.map((dt, i) => {
      return (
        <MenuItem
          key={i}
          value={dt.ruleDesc}
          primaryText={dt.ruleDesc} />
      );
    });
  }

  renderOfferTypeOptions() {
    return this.state.OfferTypeData.map((dt, i) => {
      return (
        <MenuItem
          key={i}
          value={dt.offerTypeDesc}
          primaryText={dt.offerTypeDesc} />
      );
    });
  }

  render() {

    return (
      <div className={cr.container}>
        <div className={cr.rows}>
          <div>
            <DropDownMenu
              value={this.state.DivisionState}
              onChange={this.handleChangeDivision}>
              {this.renderDivisionOptions()}
            </DropDownMenu>
            <br/>
            <DropDownMenu
              value={this.state.StoreGroupingState}
              onChange={this.handleChangeStoreGrouping}>
              {this.renderStoreGroupingOptions()}
            </DropDownMenu>
            <br/>
            <DropDownMenu
              value={this.state.OfferTypeState}
              onChange={this.handleChangeDiscountType}>
              {this.renderOfferTypeOptions()}
            </DropDownMenu>
            <br/>
          </div>
        </div>
      </div>
    );
  }
}

还有一件事,现在当我获取来自WS的结果我在位置0显示数据时,我想要改变的是具有默认选项而不是0位置。

一些帮助会很好。

问候。

1 个答案:

答案 0 :(得分:1)

首先不要在获取成功时设置DivisionStateStoreGroupingStateOfferTypeState,例如

this.setState({
    DivisionData: findResponse,
    // DivisionState: findResponse[0].divDeptShrtDesc <= Remove this 
});

接下来,在每个下拉列表中呈现默认选项,例如

<DropDownMenu
    value={this.state.DivisionState}
    onChange={this.handleChangeDivision}>
    <MenuItem value={''} primaryText={'Select option'} />
    {this.renderDivisionOptions()}
</DropDownMenu>

如果未设置上一个

,则最后设置disabled属性为true
    <DropDownMenu
        value={this.state.DivisionState}
        onChange={this.handleChangeDivision}>
        <MenuItem value={''} primaryText={'Select option'} />
        {this.renderDivisionOptions()}
    </DropDownMenu>
    <br/>
    <DropDownMenu
        disabled={!this.state.DivisionState}
        value={this.state.StoreGroupingState}
        onChange={this.handleChangeStoreGrouping}>
        <MenuItem value={''} primaryText={'Select option'} />
        {this.renderStoreGroupingOptions()}
    </DropDownMenu>
    <br/>
    <DropDownMenu
        disabled={!this.state.StoreGroupingState}
        value={this.state.OfferTypeState}
        onChange={this.handleChangeDiscountType}>
        <MenuItem value={''} primaryText={'Select option'} />
        {this.renderOfferTypeOptions()}
    </DropDownMenu>
    <br/>