Avoid drop once that dropped items count reach some limit

一曲冷凌霜 提交于 2019-12-10 11:04:57

问题


I am using Jquery UI Draggable. I'm trying to avoid items from be dropped once that the div which will receive that items reach a limit. As you can see below, here is my code.

 $( "#grid-list" ).droppable({
        over: function(event, ui) {
          count = 0;
          $("#grid-list").find("li").each(function () {
            count++;

            if(count > $("#grid-list").data("albumlist")){
              alert("limit reached");
            }
          });
        }
      });

JS Fiddle example: http://jsfiddle.net/r1Lnxby9/


回答1:


Edit, Updated

Try removing ondragstart="drag(event)" , draggable="true" from html , drag not appear defined at js; setting tolerance:"touch" at .droppable() options ; substituting $("> li", this).length for count , $("#grid-list").each() ; >= for > if expected result is 1 element maximum within .droppable() element ; call event.preventDefault() , event.stopPropagation() within over event

  $("#list-albuns, #grid-list").sortable({
    connectWith: ".connectedSortable"
  }).disableSelection();

  $("#grid-list").droppable({
    tolerance: "touch",
    over: function(event, ui) {
      if ($("> li", this).length >= $(this).data("albumlist")) {
        event.preventDefault();
        event.stopPropagation();
        alert("limit");
      }
    }
  });
ul li {
  display: inline-block;
  list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js">
</script>

<ul id="list-albuns" class="connectedSortable ui-sortable">
  <li id="0" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="0">
    <img id="dragimg0" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/51552793.png" alt="Appetite for Destruction">
  </li>
  <li id="2" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="2">
    <img id="dragimg2" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/97176319.png" alt="Use Your Illusion II">
  </li>
  <li id="3" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="3">
    <img id="dragimg3" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/89710469.png" alt="40 Seasons - The Best Of Skid Row">
  </li>
  <li id="5" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="5">
    <img id="dragimg5" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/56005467.jpg" alt="Danger Danger">
  </li>
  <li id="6" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="6">
    <img id="dragimg6" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/85595741.jpg" alt="Screw It [Italy Bonus Tracks]">
  </li>
  <li id="7" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="7">
    <img id="dragimg7" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/90791541.jpg" alt="Detonator">
  </li>
</ul>Drop Here:
<ul id="grid-list" data-albumlist="1" class="connectedSortable" style="width:500px; height:200px;display:block; border:2px solid black;"></ul>

jsfiddle http://jsfiddle.net/r1Lnxby9/2/



来源:https://stackoverflow.com/questions/31105873/avoid-drop-once-that-dropped-items-count-reach-some-limit

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