Skip to content

Commit 8c6ad08

Browse files
committed
Merge pull request Serhioromano#7 from joom/master
onBeforeRemove event added
2 parents 4b31f0b + ec220f9 commit 8c6ad08

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,26 @@ This event is triggered as a duplicate tag is being added. This method can retur
270270

271271
-----------------------------------------------------
272272

273+
###onBeforeRemove
274+
275+
This event is triggered before a tag is removed. The function for this event should return true or false. If the function returns true then the tag is removed, however, if the function returns false then the tag will not be removed. If there is a removal request to the server, it will happen after this function returns true. By default, the tag is removed, but this event can be useful if you want to make certain tags unremovable. One parameter is passed to the callback:
276+
277+
- `pill` - HTML element. headsup: every HTML element has `data-tag-id` attribute populated from `item.id` of the [tags feed](#feed-format) element.
278+
279+
**Example**
280+
281+
$('#bs-tags').tags({
282+
onBeforeRemove: function(pill){
283+
if($(pill).data('tag-id') === 'Apple') {
284+
return false; // "Apple" tags are unremovable.
285+
} else {
286+
return true;
287+
}
288+
}
289+
});
290+
291+
-----------------------------------------------------
292+
273293
## Examples
274294
### Load default values
275295

js/bootstrap-tags.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@
5454
onLoadSuggestions: function(values) {
5555
return values;
5656
},
57-
onDuplicate: null
57+
onDuplicate: null,
58+
onBeforeRemove: function(pill) {
59+
return true;
60+
}
5861
}
5962

6063

@@ -291,7 +294,13 @@
291294

292295
Tags.prototype.removeTag = function(tag) {
293296
var $self = this;
294-
$(tag).closest('[data-tag-id]').animate({width: 0, "padding-right": 0, "padding-left": 0}, 200, 'swing', function() {
297+
var $tag = $(tag).closest('[data-tag-id]');
298+
299+
if($self.options.onBeforeRemove($tag) === false) {
300+
return;
301+
}
302+
303+
$tag.animate({width: 0, "padding-right": 0, "padding-left": 0}, 200, 'swing', function() {
295304
var $this = $(this);
296305
if($self.options.remove_url) {
297306
$.ajax({

js/bootstrap-tags.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)