问题
I need to know how can I remove a single gridster.js widget by his dynamically created id with gridster.add_widget. Every new widget created has an own button to remove that single widget, but I can't make it work.
Here is my Code
$(document).ready(function(){
var count = 0;
var newid = count + 1;
$(document).on("click", "#newGrid", function() {
var gridster = $(".gridster ul").gridster().data('gridster');
gridster.add_widget('<li id="block"'+newid'>Hello, now delete me <span id="remove"'+newid'>x</span></li>',2 ,1);
});
$(document).on('click', '#remove'+newid, function() {
var gridster = $(".gridster ul").gridster().data('gridster');
gridster.remove_widget( '#block'+newid );
});
});
For adding widgets it works fine, but I can't remove widgets.
回答1:
Actually you don't need ID for widget removing. Please check my script.
var gridster = $('.gridster ul').gridster().data('gridster');
$(document).on( "click", ".gridster ul li", function() {
$(this).addClass("activ");
gridster.remove_widget($('.activ'));
});
回答2:
In fact, you just need to wrap your '#block'+newid
in a jQuery element, as stated in the function comment:
gridster.remove_widget( $( '#block'+newid ) );
Function comment (in jquery.gridster.js):
/**
* Remove a widget from the grid.
*
* @method remove_widget
* @param {HTMLElement} el The jQuery wrapped HTMLElement you want to remove.
* @param {Boolean|Function} silent If true, widgets below the removed one
* will not move up. If a Function is passed it will be used as callback.
* @param {Function} callback Function executed when the widget is removed.
* @return {Class} Returns the instance of the Gridster Class.
*/
fn.remove_widget = function(el, silent, callback) {...}
回答3:
- newid var should be increased after click add
- li tag not had id correctly
please check code below, should work with you
$(document).ready(function(){
var count = 0;
var newid = count + 1;
$(document).on("click", "#newGrid", function() {
var gridster = $(".gridster ul").gridster().data('gridster');
gridster.add_widget('<li id="block'+newid+'">Hello, now delete me <span class="remove">x</span></li>',2 ,1);
newid++;
});
$(document).on('click', '.remove', function() {
var gridster = $(".gridster ul").gridster().data('gridster');
gridster.remove_widget( $(this).parent());
});
});
回答4:
Since it works with jQuery objects, you don't need to add any classes or ids to identify the item you want to remove. Just create a click event and reference to $(this).
$('body').on( "click", ".gridster ul > li", function() {
gridster.remove_widget($(this));
});
Or something more useful, where you have a "remove button" inside the li:
$('body').on( "click", ".gridster ul > li .remove", function() {
gridster.remove_widget($(this).closest('li'));
});
回答5:
this works for me, i use a timestamp as unique id
ADD AN ITEM:
$( "#addMod").click(function(e) {
gridster1.add_widget(
'<li class="crItem" id="' + $.now() + '">DESCRIPTION
<i class="fi-wrench"></i>
<i class="fi-x" id="delItem"></i></li>', 1, 1
);
});
REMOVE AN ITEM:
$('#cashRegisterScreen').on('click', '#delItem', function() {
var id=$(this).parent().attr("id");
gridster.remove_widget($(".gridster ul").find("[id='" + id + "']"));
});
来源:https://stackoverflow.com/questions/20705110/how-to-remove-a-single-widget-from-gridster-js-by-his-dynamically-created-id