代码之家  ›  专栏  ›  技术社区  ›  Borja

拖放div

  •  0
  • Borja  · 技术社区  · 7 年前

    $('.component-container').sortable({
      cursor: 'move',
      items: ".component-section",
      placeholder: 'ui-state-highlight',
      start: function(e, ui) {
        ui.placeholder.width(ui.item.find('.component-section').width());
        ui.placeholder.height(ui.item.find('.component-section').height());
        ui.placeholder.addClass(ui.item.attr("class"));
      }
    });
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
    
    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="fila" class="row center expandir component-container" style="text-align: center;">
      <div class="col-md-6 col-xs-12" id="firstCol">
        <div class="col-md-12 col-sm-12 component-section" id="panel1">
        </div>
        <div class="col-md-12 col-sm-12 component-section" id="panel3">
        </div>
      </div>
      <div class="col-md-6 col-xs-12" id="secondCol">
        <div class="col-md-12 col-sm-12 component-section" id="panel2">
        </div>
      </div>
    </div>

    我已经使它可以拖放。但它有一个bug,它允许我在同一列中删除三个div,但它不允许我将其放回空列中。你知道我怎样才能把这个div放在空列中吗?

    谢谢

    1 回复  |  直到 7 年前
        1
  •  0
  •   Arvind Muthuraman    7 年前

    拖放组件节时,可以使用dom检查firstCol和secondCol子级。如果数字是2(在给定的条件下),请取消删除操作(可以使用可排序/可拖动插件完成)。

    使用您添加的plunker代码编辑1/

    $('.component-container').sortable({
          connectWith: '.component-container',  // Add this
          cursor: 'move',
          items: ".component-section",
          placeholder: 'ui-state-highlight',
          start: function(e, ui) {
            ui.placeholder.width(ui.item.find('.component-section').width());
            ui.placeholder.height(ui.item.find('.component-section').height());
            ui.placeholder.addClass(ui.item.attr("class"));
          }
        });
    });
    

    从html中,删除 .component-container 从当前位置初始化并将其添加到firstCol和secondCol div

    <div id="fila" class="row center expandir " style="text-align: center;">  - removed component-container from here
    
    
    <div class="col-md-6 col-xs-12 component-container" id="firstCol">  - added component-container here
    
    <div class="col-md-6 col-xs-12 component-container" id="secondCol">  - added component-container here