通过Material-UI扩展面板获取API数据

时间:2018-08-23 20:01:25

标签: reactjs api material-ui fetch-api

我正在尝试基于获取API生成扩展面板,这已经成功完成了。我遇到的问题是,当我尝试在扩展面板上生成数据时,它会显示所有数据集,而我希望它在每个扩展面板上显示一组数据。我使用了material-ui

中的扩展面板组件
class FetchData extends Component {
    constructor(props) {
        super(props);
        this.state = {
            isLoaded: false,
            items: [],
        }
    }

componentDidMount() {
    fetch(url , {
        method: 'get',
        mode: 'cors',
        headers: { 
        'X-API-KEY': API_KEY,
        'Access-Control-Allow-Origin': '*',
        'Accept': 'application/json',
        'Content-Type': 'application/json'
        }

    })
        .then(res => res.json())
        .then(json => {
            this.setState({
                isLoaded: true,
                items: json,
            })
        })
    };

    render() {
        const { classes } = this.props;

        var{ isLoaded, items } = this.state;
        if(!isLoaded) {
            return <div>Loading...</div>
        }

        else{

        return (
            <div className="container">
             <ul>
            {items.map((item) => (
                <li key={item.device_id} > 
                  <Expansion/>

                </li>
            ))}
        </ul>
        </div>

            );
        }
    }
 }

导出默认的FetchData;

扩展面板代码

const styles = theme => ({
  root: {
    width: '100%',
  },
  heading: {
    fontSize: theme.typography.pxToRem(15),
  },
  secondaryHeading: {
    fontSize: theme.typography.pxToRem(15),
    color: theme.palette.text.secondary,
  },
  icon: {
    verticalAlign: 'bottom',
    height: 20,
    width: 20,
  },
  details: {
    alignItems: 'center',
  },
  column: {
    flexBasis: '33.33%',
  },
  helper: {
    borderLeft: `2px solid ${theme.palette.divider}`,
    padding: `${theme.spacing.unit}px ${theme.spacing.unit * 2}px`,
  },
  link: {
    color: theme.palette.primary.main,
    textDecoration: 'none',
    '&:hover': {
      textDecoration: 'underline',
    },
  },
});

function DetailedExpansionPanel(props) {
  const { classes } = props;
  return (
    <div className={classes.root}>
      <ExpansionPanel>
        <ExpansionPanelSummary expandIcon={<img src={ExpandMoreIcon} alt="EMI"/>} >
          <div className={classes.column}>
          Devices = <FetchID />
          </div>
          <div className={classes.column}>

          </div>
        </ExpansionPanelSummary>
        <ExpansionPanelDetails className={classes.details}>
          <div className={classes.column} />
          <div className={classes.column}>
            <Chip label="Barbados" className={classes.chip} onDelete={() => {}} />
          </div>
          <div className={classNames(classes.column, classes.helper)}>
            <Typography variant="caption">
              Select your destination of choice
              <br />
              <a href="#sub-labels-and-columns" className={classes.link}>
                Learn more
              </a>
            </Typography>
          </div>
        </ExpansionPanelDetails>
        <Divider />
        <ExpansionPanelActions>
          <Button size="small">Cancel</Button>
          <Button size="small" color="primary">
            Save
          </Button>
        </ExpansionPanelActions>
      </ExpansionPanel>
    </div>
  );
}

DetailedExpansionPanel.propTypes = {
  classes: PropTypes.object.isRequired,
};


export default withStyles(styles)(DetailedExpansionPanel);

0 个答案:

没有答案