简体   繁体   English

拖放带有限制的HTML

[英]Drag & Drop HTML with Restictions

I'm trying without much success to create a HTML5 / Bootstrap based drag and drop facility with restrictions that can be preset. 我正在尝试创建基于HTML5 / Bootstrap的拖放功能,但可以预先设置限制,但没有取得很大的成功。

The best way of explaining this would be a food menu creator. 解释此问题的最佳方法是食物菜单创建者。 So what I want is to have a list of ingredients on the left side in a 3 column div and the website user can drag items from the list of preset ingredients and drop them into the menu calendar which will be shown on the right of the page in a 9 col div. 因此,我要在3列div的左侧具有配料列表,网站用户可以从预设配料列表中拖动项目并将其拖放到菜单日历中,该菜单日历将显示在页面右侧在9 col div。

I can do this using the following script - http://mdn.github.io/drag-and-drop/copy-move-DataTransfer.html 我可以使用以下脚本进行此操作-http: //mdn.github.io/drag-and-drop/copy-move-DataTransfer.html

JavaScript: JavaScript:

function dragstart_handler(ev) {
 console.log("dragStart");
 // Change the source element's background color to signify drag has started
 ev.currentTarget.style.border = "dashed";
 // Add the id of the drag source element to the drag data payload so
 // it is available when the drop event is fired
 ev.dataTransfer.setData("text", ev.target.id);
 // Tell the browser both copy and move are possible
 ev.effectAllowed = "copyMove";
}
function dragover_handler(ev) {
 console.log("dragOver");
 // Change the target element's border to signify a drag over event
 // has occurred
 ev.currentTarget.style.background = "lightblue";
 ev.preventDefault();
}
function drop_handler(ev) {
  console.log("Drop");
  ev.preventDefault();
  // Get the id of drag source element (that was added to the drag data
  // payload by the dragstart event handler)
  var id = ev.dataTransfer.getData("text");
  // Only Move the element if the source and destination ids are both "move"
  if (id == "src_move" && ev.target.id == "dest_move")
    ev.target.appendChild(document.getElementById(id));
  // Copy the element if the source and destination ids are both "copy"
  if (id == "src_copy" && ev.target.id == "dest_copy") {
   var nodeCopy = document.getElementById(id).cloneNode(true);
   nodeCopy.id = "newId";
   ev.target.appendChild(nodeCopy);
  }
}
function dragend_handler(ev) {
  console.log("dragEnd");
  // Restore source's border
  ev.target.style.border = "solid black";
  // Remove all of the drag data
  ev.dataTransfer.clearData();
}

HTML: HTML:

<h1>Drag and Drop: Copy and Move elements with <code>DataTransfer</code></h1>
 <div draggable="true" id="src_copy" ondragstart="dragstart_handler(event);" ondragend="dragend_handler(event);">
     Select this element and drag to the <strong>Copy Drop Zone</strong>.
 </div>
 <div id="dest_copy" ondrop="drop_handler(event);" ondragover="dragover_handler(event);"><strong>Copy Drop Zone</strong></div>
 <div draggable="true" id="src_move" ondragstart="dragstart_handler(event);" ondragend="dragend_handler(event);">
     Select this element and drag to the <strong>Move Drop Zone</strong>.
 </div>
 <div id="dest_move" ondrop="drop_handler(event);" ondragover="dragover_handler(event);"><strong>Move Drop Zone</strong></div>

However, what I need is restrictions. 但是,我需要的是限制。 So for example if a certain menu says you can only use 6 eggs, I need eggs to appear in the left side ingredient column and when someone drags and drops 6 eggs, then they cannot add any more regardless that eggs may still be in the ingredient list. 因此,例如,如果某个菜单上说您只能使用6个鸡蛋,则我需要将鸡蛋显示在左侧的成分栏中,并且当某人拖放6个鸡蛋时,无论该成分中是否仍然包含鸡蛋,他们都无法再添加清单。 Ideally I would like it to disappear after X amounts of eggs have been used / selected. 理想情况下,我希望它在使用/选择了X个鸡蛋后消失。 So if we set eggs at 6 then if eggs are dropped into the right side 6 times, the item disappears from the left column. 因此,如果我们将鸡蛋设置为6,然后将鸡蛋丢入右侧6次,则该项目将从左侧栏中消失。

I also need the user to be able to drag back out of the drop-zone (right side) and place back inside the left column if for instance they change their mind. 我还需要用户能够拖出拖放区(右侧)并将其放回左侧列中(例如,如果他们改变主意)。 Finally, I need it to have a convert to pdf option at the end so when someone fills their menu, they can click a button and convert the displayed html into a pdf or print it. 最后,我需要它在最后有一个转换为pdf选项,以便当某人填写菜单时,他们可以单击一个按钮并将显示的html转换为pdf或进行打印。

I am deliberating trying it myself before outsourcing to Freelancer - but I am pretty novice to using dataTransfer and appropriate handlers. 我正在考虑自己尝试将其外包给Freelancer,但是对于使用dataTransfer和适当的处理程序的新手来说,我是一个新手。

Anyone know of anything out there or any thoughts on how to achieve what I want? 任何人都知道那里的任何东西或对如何实现我想要的东西有任何想法吗?

You will need to change your on drop event (ondrop="drop_handler(event);") to check whether the requirements are met for the item to be entered in the list. 您将需要更改您的on drop事件(ondrop =“ drop_handler(event);”),以检查是否满足要输入到列表中的项目的要求。

Same with dragging the item back out. 将项目拖回去也一样。 Check which list the item is dragged from and add to the other list. 检查项目从哪个列表中拖动并添加到另一个列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM