添加/删除小部件以及调整大小不起作用

时间:2018-03-01 10:02:35

标签: javascript jquery gridster

我使用了基本的网格代码然后我已经声明了按钮添加和删除小部件的功能它工作正常 但是当我将调整大小功能添加到上面的代码时,没有任何工作(我的意思是调整大小,添加和删除小部件)

我的js代码正在关注

<script type="text/javascript">
    var gridster;

    gridster = $(".gridster ul").gridster({
        widget_base_dimensions: [100, 100],
        widget_margins: [5, 5],
        helper: 'clone',
        resize: {
            enabled: true
        }           


    }).data('gridster');



$(document).on( "click",".delete-button", function() {
var gridster = $(".gridster ul").gridster().data('gridster');
gridster.remove_widget($(this).parent());
});



$(document).on( "click",".add-button", function() {
var gridster = $(".gridster ul").gridster().data('gridster');      
gridster.add_widget('<li><button class="delete-button" style="float: 
right;">-</button><h3></h3></li>',1,1);
});

以上代码无效

工作代码中没有这个

resize: {
        enabled: true
    }    

1 个答案:

答案 0 :(得分:1)

参考解决方案here,使用resizable()

更新了代码

还添加了css文件jquery.gridster.css

&#13;
&#13;
$(function() {
  var gridster;
  var grid_size = 100;
  var grid_margin = 5;
  gridster = $(".gridster ul").gridster({
    widget_base_dimensions: [100, 100],
    widget_margins: [5, 5],
    helper: 'clone',
    resize: {
      enabled: true
    }


  }).data('gridster');



  $(document).on("click", ".delete-button", function() {
    var gridster = $(".gridster ul").gridster().data('gridster');
    gridster.remove_widget($(this).parent());

  });



  $(document).on("click", ".add-button", function() {
    gridster
      .add_widget('<li><button class="delete-button" style="float: right;">-</button><h3></h3></li>', 1, 1);
    gridster.resizable({
      grid: [grid_size + (grid_margin * 2), grid_size + (grid_margin * 2)],
      animate: false,
      minWidth: grid_size,
      minHeight: grid_size,
      containment: '#layouts_grid ul',
      autoHide: true,
      stop: function(event, ui) {
        var resized = $(this);
        setTimeout(function() {
          resizeBlock(resized);
        }, 300);
      }
    });
  });
})
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.gridster/0.5.6/jquery.gridster.css" rel="stylesheet"/>
<link href="https://dsmorse.github.io/gridster.js/demos/assets/css/demo.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.gridster/0.5.6/jquery.gridster.min.js"></script>
<h1>Add and Remove Widget Dynamically</h1>

<p>When you remove a widget,the widget below it will always remain in the same column.Widget from next column wont come in place of widget removed.Gridster is designed that way</p>
<a href="https://github.com/ducksboard/gridster.js/issues/338" target="_blank"> Click to know more</a>


<div class="gridster">
  <ul>
    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>1</h3>
    </li>
    <li data-row="1" data-col="2" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>2</h3>
    </li>
    <li data-row="1" data-col="3" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>3</h3>
    </li>
    <li data-row="1" data-col="4" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>4</h3>
    </li>
    <li data-row="2" data-col="1" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>5</h3>
    </li>
    <li data-row="2" data-col="2" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>6</h3>
    </li>
    <li data-row="2" data-col="3" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>7</h3>
    </li>
    <li data-row="2" data-col="4" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>8</h3>
    </li>
    <li data-row="3" data-col="1" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>9</h3>
    </li>
    <li data-row="3" data-col="2" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>10</h3>
    </li>
    <li data-row="3" data-col="3" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>11</h3>
    </li>
    <li data-row="3" data-col="4" data-sizex="1" data-sizey="1"><button class="delete-button" style="float: right;">-</button>
      <h3>12</h3>
    </li>



</div>


<button class="add-button" style="float:top;">Add Widget</button>
&#13;
&#13;
&#13;