I have used basic gridster code and then I have declared functions to add and remove widgets by button It works fine But when I add the resize functionality to the above code none of it works(I mean resize,add and delete widgets)
My js code is following
<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);
});
Above code is not working
The working code is just not having this in it
resize: {
enabled: true
}
Referring to the solution here, have updated your code with the resizable()
Also added the css file jquery.gridster.css
.
$(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);
}
});
});
})
<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>