HTML5 DragDrop dataTransfer.types not set after event.dataTransfer.setData(mimeType, angular.toJson(item)) in chrome

家住魔仙堡 提交于 2019-12-25 09:09:16

问题


I am using druska/native_js_drag_and_drop_helper.js to automate angular-drag-and-drop-lists

Issue : Even after event.dataTransfer.setData(mimeType,angular.toJson(item))

dataTransfer.types is not set or probably not allowed to read in dragstart and drop event

Observation : during manual drag drop dragstart event

dataTransfer {dropEffect: "none", effectAllowed: "move", items: DataTransferItemList, types: Array(1), files: FileList}
    dropEffect:"move"
    effectAllowed:"move"
    files:FileList
    items:DataTransferItemList
    types:Array(0)
    __proto__:DataTransfer

DataTransferItemList {0: DataTransferItem, length: 1}
    length: 0
    __proto__: DataTransferItemList

 DataTransferItem {kind: "string", type: "application/x-dnd"}
    kind: ""type: ""
    __proto__: DataTransferItem

Observation:when using custom event

dataTransfer:Object
    data:Object
        application/x-dnd:"[{"id":"706","title":"Ab 2","drag":false,"selected":true}]"
        __proto__:Object
    dropEffect:"move"
    effectAllowed:"move"
    getData:function (type)
    setData:function (type,val)
    files:Object
    items:Object
    types:Array(0)

I am using

var event = new CustomEvent("CustomEvent", {"cancelable": true})
        event.initCustomEvent(type, true, true, null);
        event.dataTransfer = {
            data: {
            },
            setData: function(type,val) {
                this.data[type] = val
            },
            getData: function(type) {
                return this.data[type]
            },
            dropEffect: 'move',
            effectAllowed:'move',
            types: [],
            items:{},
            files:{}


        }

I am not able to initialize dataTransfer object. dataTransfer.items and dataTransfer.types being read only property , i don't know how to correctly initialize them .MDN dataTransfer


回答1:


Issue was setData dint had code to set DataTransfer.types that was being used by angular-drag-and-drop-lists during drag drop.

        setData: function(type,val) {
            this.data[type] = val
            this.types[0] = type
        }

Above changes fixed the issue .Drag drop working fine now using native js



来源:https://stackoverflow.com/questions/43391567/html5-dragdrop-datatransfer-types-not-set-after-event-datatransfer-setdatamimet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!