简体   繁体   English

拖放序列化项目

[英]Drag and Drop serialize items

I'm trying to make an exam application which has drag & drop question type that the user might dragging correct elements to correct droppable boxes. 我正在尝试制作一个具有拖放问题类型的考试应用程序,用户可能会拖动正确的元素来纠正可放置的框。 Totally, I need get dropped elements/items id's as array or another and insert to mysql with php. 总的来说,我需要删除元素/项的ID作为数组或另一个,并使用php插入mysql。 I did something are below: 我做了以下事情:

Javascript Java脚本

$(".dragging-items .draggable-item").draggable({ 
    helper: "clone",
    opacity: 0.5,
    scroll:true,
    refreshPositions: true,
    start: function( event, ui ) {
            (this).addClass("dragging");
},
    stop: function( event, ui ) {
            $(this).removeClass("dragging");
    },

});

$(".dragging-items").droppable({
    hoverClass: "drop-hover",
    drop:function(event, ui) {
        ui.draggable.detach().appendTo($(this));
    }
});

$(".droppable-list").droppable({
    drop:function(event, ui) {
    ui.draggable.detach().appendTo($(this));
        var drag_id = $(ui.draggable).attr("id");
        var drop_id = event.target.id;
        var $tbox   = $("input[name=droppable-list-"+ drop_id +"]");
        var current_data = $tbox.val();
        var new_data = current_data + drag_id + ", ";
        $tbox.val(new_data);
    },
});

HTML 的HTML

<ul class="dragging-items">
<li class="draggable-item" id="1"> Item-1 </li>
<li class="draggable-item" id="2"> Item-2 </li>
<li class="draggable-item" id="3"> Item-</li>
</ul>

<div class="drag-drop">

    <div class="droppable-title"> Box 1 </div>
    <div class="droppable-item">
          <div class="droppable-list" id="1"></div>
     </div>
    <input type="text" class="droppable-input" name="droppable-list-1"> 
    </div>

    <div class="droppable-title"> Box 2 </div>
    <div class="droppable-item">
          <div class="droppable-list" id="2"></div>
     </div>
    <input type="text" class="droppable-input" name="droppable-list-2"> 
    </div>

</div>

JsFiddle https://jsfiddle.net/7e568v2y/ JsFiddle https://jsfiddle.net/7e568v2y/

I found a number of oddities in CSS that may want to be corrected. 我在CSS中发现了许多可能需要纠正的怪异之处。 A lot of missed ; 很多错过了; , but that's not the main focus. ,但这不是主要重点。 Also, you have a number of repeated ID attributes and these must be unique in HTML. 另外,您有许多重复的ID属性,并且这些属性在HTML中必须是唯一的。 Consider the following code. 考虑下面的代码。

$(function() {
  function dropToArray(dropObj) {
    var results = [];
    $(".draggable-item", dropObj).each(function(i, el) {
      results.push($(el).data("drag-id"));
    });
    return results;
  }

  function updateDropList(dropObj) {
    var dropped = dropToArray(dropObj);
    var $tbox = $("input[name=droppable-list-" + dropObj.attr("id") + "]");
    $tbox.val(dropped.join(","));
  }

  $(".dragging-items .draggable-item").draggable({
    helper: "clone",
    opacity: 0.5,
    scroll: true,
    refreshPositions: true,
    start: function(event, ui) {
      $(this).addClass("dragging");

    },
    stop: function(event, ui) {
      $(this).removeClass("dragging");
    },
  });

  $(".dragging-items").droppable({
    hoverClass: "drop-hover",
    drop: function(event, ui) {
      ui.draggable.detach().appendTo($(this));
    }
  });

  $(".droppable-list").droppable({
    drop: function(event, ui) {
      var $this = $(this);
      if ($this.find(".droppable-list").length >= 2) {
        ui.draggable.draggable("option", "revert", true);
        return;
      } else {
        ui.draggable.detach().appendTo($(this));
        setInterval(function() {
          $(".droppable-list").each(function(i, el) {
            updateDropList($(el));
          });
        }, 100);
      }
    },
  });
});

Always best to create a few simple functions if you're going to be doing something often. 如果要经常做一些事情,最好总是创建一些简单的函数。 I created two, one to iterate the container and build an array of the specific ID. 我创建了两个,一个用于迭代容器并构建特定ID的数组。 To help ensure the same data comes back, I added the data-drag-id attribute. 为了确保返回相同的数据,我添加了data-drag-id属性。 Now we can have unique IDs and still retain the ID Number for each item. 现在,我们可以拥有唯一的ID,并且仍然保留每个商品的ID号。

Working Example: https://jsfiddle.net/Twisty/t1a40Lsn/22/ 工作示例: https : //jsfiddle.net/Twisty/t1a40Lsn/22/

As you can see, when an item is dragged and dropped, it updates the field for all the lists. 如您所见,当拖放一个项目时,它将更新所有列表的字段。 This addresses an issue if the user moves it from one box to another. 如果用户将其从一个盒子移到另一个盒子,则可以解决此问题。 Since the data is in an Array, we can simple use .join() to make a nice well formatted string of data. 由于数据位于数组中,因此我们可以简单地使用.join()来制作一个格式良好的字符串。

Hope that helps. 希望能有所帮助。

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

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