Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set multiple data on dataTransfer.setData

It's possible to set multiple using dataTransfer?

HTML

<tr class="notfirst"
    draggable="true"
    ondragstart="drag(event, '@Model.getFormat(item)', '@Model.getColor(item)')"
    ondragover="allowDrop(event)"
>

JS

function drag(ev, format, color) {
    ev.dataTransfer.setData("text", format);
    ev.dataTransfer.setData("text", color); 
}

I can correctly pass format and color to my JS function but I cant add both to dataTransfer because color replaces format. I tried also to pass an object but didn't work either.

like image 880
Daniel Alves Avatar asked May 17 '26 17:05

Daniel Alves


2 Answers

You may push your data to a JSON object, then convert this object to a string with JSON.stringify(). After that you will be able to pass the whole string to setData.

   let  obj = {prd_iId: 50 ,prd_sName: 'Ray Ban'};
   obj = {...obj, prd_sModel:'Aviator'};     
   e.dataTransfer.setData("text/plain", JSON.stringify(obj));

That will do what you want.

like image 127
Karim El Avatar answered May 20 '26 06:05

Karim El


possible to set multiple using dataTransfer?

Yes.

If you don't want to serialize data you can simply set your data with different format arguments:

  ev.dataTransfer.setData('formatText', format);
  ev.dataTransfer.setData('colorText', color); 

get data like this

  const format = ev.dataTransfer.getData('formatText');
  const color  = ev.dataTransfer.getData('colorText'); 
like image 41
Ethicist Avatar answered May 20 '26 05:05

Ethicist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!