首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ResponsiveReactGridLayout上使用react-grid-layout将项目放置在错误的位置

在ResponsiveReactGridLayout上使用react-grid-layout将项目放置在错误的位置
EN

Stack Overflow用户
提问于 2021-04-17 07:11:55
回答 1查看 872关注 0票数 0

我是第一次接触react-grid-layout,我已经使用这个示例https://react-grid-layout.github.io/react-grid-layout/examples/15-drag-from-outside.html创建了一个拖放示例。当项目被拖放时,它总是被放在网格的第一列。警报中的X和Y是正确的,我的代码中有什么错误?这是我的代码:

代码语言:javascript
复制
    onDrop = (layout, layoutItem, _event) => {
          alert(`Dropped element props:\n${JSON.stringify(layoutItem, ['x', 
          'y', 'w', 'h'], 2)}`);
             this.setState(prevState => ({
                  layouts: {
                  ...prevState.layouts,
                  [prevState.currentBreakpoint]: [
                   ...prevState.layouts[prevState.currentBreakpoint],
                   layoutItem
                ]
             }
         }));
      };

    render() {
    return (

        <div>
            <div className="toolBar">
                <div
                    className="droppable-element"
                    draggable={true}
                    unselectable="on"
                >
                    Button 1
                </div>
            </div>
            <div>
                <ResponsiveReactGridLayout
                    {...this.props}
                    layouts={this.state.layouts}
                    onBreakpointChange={this.onBreakpointChange}
                    onLayoutChange={this.onLayoutChange}
                    onDrop={this.onDrop}
                    droppingItem={this.props.item}
                    measureBeforeMount={false}
                    useCSSTransforms={this.state.mounted}
                    compactType={this.state.compactType}
                    preventCollision={true}
                    isDroppable={true}
                >
                    {this.generateDOM()}
                </ResponsiveReactGridLayout>
            </div>
        </div>
    );
}
EN

回答 1

Stack Overflow用户

发布于 2021-04-21 03:56:30

我想通了。我将data-grid={el}添加到应该删除的元素中。这就是代码。

代码语言:javascript
复制
   import React from "react";
   import _ from "lodash";
   import { Responsive, WidthProvider } from "react-grid-layout";
   const ResponsiveReactGridLayout = WidthProvider(Responsive);

    export default class DragFromOutsideLayout extends React.Component {
       static defaultProps = {
       className: "layout",
       rowHeight: 30,
       onLayoutChange: function () { },
       cols: { lg: 8, md: 5, sm: 6, xs: 4, xxs: 2 },
       verticalCompact: false,
       preventCollision: true
     };

  state = {
    currentBreakpoint: "lg",
    compactType: "horizontal",
    mounted: false,
    layouts: { lg: generateLayout() },
    newCounter: 0
};

generateDOM(el) {
    return (
        <div key={el.i} data-grid={el}>
            <span className="text">{el.i}</span>
        </div>
    );
   }

  onBreakpointChange = breakpoint => {
    this.setState({
        currentBreakpoint: breakpoint
    });
  };

 onCompactTypeChange = () => {
    const { compactType: oldCompactType } = this.state;
    const compactType =
        oldCompactType === "horizontal"
            ? "vertical"
            : oldCompactType === "vertical"
                ? null
                : "horizontal";
    this.setState({ compactType });
};
onDrop = (layout, layoutItem, _event) => {
    this.setState({
        layouts: {
            lg: this.state.layouts.lg.concat({
                i: this.state.newCounter.toString(),
                x: layoutItem.x,
                y: layoutItem.y,
                w: layoutItem.w,
                h: layoutItem.h
            })
        },
        newCounter: this.state.newCounter + 1
    });
  };

 render() {
    return (
        <div>
            <div className="toolBar">
                <div
                    className="droppable-element"
                    draggable={true}
                    unselectable="on"
                >
                    Button 1
                    </div>
            </div>
            <div className="space"></div>
            <div className="gridL">
                <span>Drop the item here</span>
                <ResponsiveReactGridLayout
                    {...this.props}
                    onDrop={this.onDrop}
                    droppingItem={this.props.item}
                    measureBeforeMount={false}
                    useCSSTransforms={this.state.mounted}
                    compactType={this.state.compactType}
                    preventCollision={true}
                    isDroppable={true}
                >
                    {_.map(this.state.layouts.lg, el => this.generateDOM(el))}
                </ResponsiveReactGridLayout>
            </div>
        </div>
    );
  }
}

function generateLayout() {
   return _.map(_.range(0, 0), function (item, i) {
      var y = Math.ceil(Math.random() * 4) + 1;
       return {
         x: Math.round(Math.random() * 5) * 2,
         y: Math.floor(i / 6) * y,
         w: 2,
         h: y,
         i: i.toString(),
         static: Math.random() < 0.05
      };
   });
  }
const containerElement = document.getElementById('root');
ReactDOM.render(<DragFromOutsideLayout />, containerElement);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67133178

复制
相关文章

相似问题

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