首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-grid-layout中的网格项目的初始位置是随机生成的

react-grid-layout中的网格项目的初始位置是随机生成的
EN

Stack Overflow用户
提问于 2018-08-08 18:54:12
回答 1查看 1.3K关注 0票数 3

这是我用来生成react-grid-layout的代码。

我在这里试图实现的是一个动态仪表板,它是可配置的,也可以调整大小和拖动。

在初始渲染时,我尝试将仪表板卡片显示在一行中,因为所有四个仪表板卡片的Y坐标都相同。请参考图片。

代码语言:javascript
复制
import React, {Component} from 'react';
import PropTypes from 'prop-types';
//import { LayoutForm } from './layout-constant';
import { Responsive,WidthProvider} from 'react-grid-layout';
const ResponsiveGridLayout = WidthProvider(Responsive);
import GridLayout from 'react-grid-layout';
import {
  Row,
  Col,
  Card,
  CardHeader,
  CardBody,
  Progress
} from 'reactstrap';
import LineChart from './LineChart';
import ColumnChart from './ColumnChart';
import '6d-dynamic-dashboard/lib/styles/6d_dashboard.css';
import '../../node_modules/react-grid-layout/css/styles.css';
import '../../node_modules/react-resizable/css/styles.css';
const ReactHighcharts = require('react-highcharts');

class DynamicDashboard extends Component {
  constructor(props){
    super(props);
    this.state = {
      dispStyle : {}  
    }
    this.child = React.createRef();
    console.log("props",props);
  }

  renderMicroDash = () => {
      return(
        this.props.dashProps.microDash.map((dash) => {
          const className = dash.customWidth ? "pl-3" : dash.colProps;
          switch(dash.type){
            case 100 :
            return(
              <div style={{width : dash.customWidth}} className={className} key="a" 
              data-grid={{x: 0, y: 0, w: 3, h: 3.5, minW: 2, maxW: 4}}>
                <Card body className="mb-5 mt-4">
                  <Row>
                    <Col xl="8" lg="8" sm="8" xs="8" className="text-left">
                      <label>{dash.label}</label>
                      <h1 style={dash.textStyle}>{dash.sampleFigure}</h1>
                    </Col>
                    <Col xl="4" lg="4" sm="4" xs="4">
                      <i className={dash.icon}></i>
                    </Col>
                  </Row>
                </Card>
              </div>
            )
            case 101 :
            return(
              <div style={{width : dash.customWidth}} className={className} key="x" 
              data-grid={{x: 2, y:0, w: 3, h: 2, minW: 2, maxW: 4}}>
                <Card body className="mb-5 mt-4" style={dash.borderStyle}>
                  <Row>
                    <Col xl="12" lg="12" sm="12" xs="12" className="text-left">
                      <label>{dash.label}</label>
                      <h1 style={dash.textStyle}>{dash.sampleFigure}</h1>
                    </Col>
                  </Row>
                </Card>
              </div>
            )
            case 102 :
            return(
              <div style={{width : dash.customWidth}} className={className} key="c" 
              data-grid={{x: 4, y:0, w: 3, h: 2, minW: 2, maxW: 4}}>
                <Card body className="mb-5 mt-4">
                  <Row>
                  <Col xl="12" lg="12" sm="12" xs="12" className="text-left">
                      <label>{dash.label}</label>
                      {/* <h1 style={dash.textStyle}>{dash.sampleFigure}</h1> */}
                      <Progress animated value={75} color="success">$385</Progress>
                  </Col>
                  </Row>
                </Card>
              </div>
            )
            case 103 :
            return(
              <div style={{width : dash.customWidth}} className={className} key="g" 
              data-grid={{x: 6 , y:0, w: 3, h: 2, minW: 2}}>
                <Card body className="mb-5 mt-4" style={dash.cardStyle}>
                  <Row>
                    <Col xl="12" lg="12" sm="12" xs="12" className="text-left">
                      <label >{dash.label}</label>
                      <h1 style={dash.textStyle}>{dash.sampleFigure}</h1>
                      <span className="float-right">Overall Activity</span>
                    </Col>
                  </Row>
                </Card>
              </div>
            )
          }

        })
      )
  }
  render() {

      return (

      <div className="container col-md-12" >

          <ResponsiveGridLayout className="layout"
          cols={{ lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 }} 
          rowHeight={30} width={1200}
          measureBeforeMount={true}>
                {this.renderMicroDash()}
          </ResponsiveGridLayout>  
      </div>);
    }
}
export default DynamicDashboard;

如您所见,所有四张卡片的初始x坐标都不同,y坐标为0。因此,它们不是显示在一行中,而是显示如下:Initial Render Image

拖放和调整大小完全可以正常工作。

请帮助我理解我做错了什么,或者如何通过React-grid-layout解决我的卡片的初始随机放置。

EN

回答 1

Stack Overflow用户

发布于 2021-10-20 06:23:58

我知道这很古老,但为了防止其他人偶然发现这一点,我们让x以2的步长递增,而项的宽度是3。试着以大于宽度的步长递增x,应该没问题。所以一组可能的x值可以是0,4,8,12。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51744907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档