通过对多个列表做出反应来实现拖放操作

时间:2016-10-10 15:50:19

标签: javascript reactjs drag-and-drop

我正在尝试实施拖放功能,并对多个列表做出反应,但我似乎无法让它发挥作用!

我的颜色对象在掉落后会发生变化,但它不会将项目放在准确的位置。另外,在draggig中,占位符在ul有效dropzone之外也是可见的。

var colors = {
    "a": ["Red", "Green", "Blue"],
    "b": ["Yellow", "Black", "White", "Orange"]
};
var placeholder = document.createElement("li");
placeholder.className = "placeholder";
var List = React.createClass({
    getInitialState: function() {
        return {
            data: this.props.data
        };
    },
    dragStart: function(key, index, e) {
        this.dragged = e.currentTarget;
        this.contentToBeDragged_src = e.currentTarget.parentNode;
        e.dataTransfer.effectAllowed = 'move';
        // Firefox requires dataTransfer data to be set
        e.dataTransfer.setData("text/html", e.currentTarget);
    },
    dragEnd: function(key, index, e) {
        console.log(key + ' ' + index);
        this.dragged.style.display = "block";
        //this.dragged.parentNode.removeChild(placeholder);
        // Update data
        var data = this.state.data;
        var from = Number(this.dragged.dataset.id);
        var to = Number(this.over.dataset.id);
        data[index].splice(0, data[index].splice(from, 1)[0]);
        this.setState({
            data: data
        });
    },
    dragOver: function(e) {
        e.preventDefault();
        //this.dragged.style.display = "none";
        if (e.target.className == "placeholder") return;
        this.over = e.target;
        // Inside the dragOver method
        var relY = e.clientY - this.over.offsetTop;
        var height = this.over.offsetHeight / 2;
        var parent = e.target.parentNode;
        if (relY > height) {
            this.nodePlacement = "after";
            parent.insertBefore(placeholder, e.target.nextElementSibling);
        } else if (relY < height) {
            this.nodePlacement = "before"
            parent.insertBefore(placeholder, e.target);
        }
    },
    render: function() {
        var context = this;
        var lists = Object.keys(context.state.data).map(function(key) {
            return <ul onDragOver = {
                    context.dragOver
                } > {
                    context.state.data[key].map(function(item, i) {
                        return ( <
                            li data - id = {
                                i
                            }
                            key = {
                                i
                            }
                            draggable = "true"
                            onDragEnd = {
                                (evt) => context.dragEnd(key, i, evt)
                            }
                            onDragStart = {
                                (evt) => context.dragStart(key, i, evt)
                            } >
                            {
                                item
                            } <
                            /li>
                        )
                    }, context)
                } <
                /ul>
        });

        return <div > {
            lists
        } < /div>

    }
});

ReactDOM.render( <
    List data = {
        colors
    }
    />, document.getElementById('app')
);

这是我尝试的jsfiddle。 jsfiddle

非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

关于丢弃部分,问题出在dragEnd(),你写的是:

data[index].splice(0, data[index].splice(from, 1)[0]);

第一个问题是,当您使用data[index](不需要索引)时,请使用data[key]。第二个问题是你的移动操作,这可能不会达到你的预期。以下是该行应该是什么样的:

data[key].splice(to, 0, data[key].splice(from, 1)[0]);