diff --git a/docs/60b9f73e-fefd-40ea-aabd-8088357eedd8.dmp b/docs/60b9f73e-fefd-40ea-aabd-8088357eedd8.dmp new file mode 100644 index 0000000..35db5ee Binary files /dev/null and b/docs/60b9f73e-fefd-40ea-aabd-8088357eedd8.dmp differ diff --git a/docs/index.html b/docs/index.html index f6302a7..fc0d31f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -33,6 +33,25 @@

Example

<input type="text" name="tags" data-provide="tag">
+ +

Heres a javascript example semi-colon separated values. Separated by semi-colon (186), return/enter (13) and tab (9)

+
+ +
+
+<script>
+    $(function() {
+        var usStates = ["Alabama","Alaska","Arizona","Arkansas","California","Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Dakota","North Carolina","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"];
+        
+        $('#tags').tag({
+          autocompleteOnKey: 186,
+          addKeys: [186, 13, 9],
+          source: usStates,
+          separator: ';'
+        })
+    })
+</script>
+

Usage

Via data attributes

@@ -77,6 +96,24 @@

Options

false Autocomplete on comma type with the first suggested value (if any). + + autocompleteOnKey + int + -1 + Autocomplete on this keyCode (188 for comma) type with the first suggested value (if any). + + + addKeys + array + [188, 13, 9] + Add the tag if any of these keys are pressed. Default Comma (188), Return/Enter (13) and Tab (9) + + + separator + string + ',' + The main separator of the input value. Comma as default +

Methods

@@ -92,6 +129,10 @@

Events

+ + add + This event is fired immediately before a tag will be added. The event handler will pass an object obj.value for you to get or modify. + added This event is fired immediately after a tag has been successfully added. The event handler will be passed the value that was added. @@ -103,9 +144,12 @@

Events

-$('#myTag').on('added', function (e, value) {
+$('#myTag').on('add', function (e, t) {
+  t.value = t.value.split(' - ')[0];
+  // do other things before we add...
+}).on('added', function(e, value) {
   // do something...
-})
+} @@ -121,5 +165,17 @@

Events

}) }(window.jQuery) + \ No newline at end of file diff --git a/js/bootstrap-tag.js b/js/bootstrap-tag.js index bcbc80f..284f35b 100644 --- a/js/bootstrap-tag.js +++ b/js/bootstrap-tag.js @@ -24,7 +24,7 @@ var Tag = function ( element, options ) { this.element = $(element) this.options = $.extend(true, {}, $.fn.tag.defaults, options) - this.values = $.grep($.map(this.element.val().split(','), $.trim), function ( value ) { return value.length > 0 }) + this.values = $.grep($.map(this.element.val().split(this.options.separator), $.trim), function ( value ) { return value.length > 0 }) this.show() } @@ -63,12 +63,12 @@ that.skip = false }) .on('keydown', function ( event ) { - if ( event.keyCode == 188 || event.keyCode == 13 || event.keyCode == 9 ) { + if ( $.inArray(event.keyCode, that.options.addKeys) !== -1 && event.keyCode != 8 ) { if ( $.trim($(this).val()) && ( !that.element.siblings('.typeahead').length || that.element.siblings('.typeahead').is(':hidden') ) ) { if ( event.keyCode != 9 ) event.preventDefault() that.process() - } else if ( event.keyCode == 188 ) { - if ( !that.options.autocompleteOnComma ) { + } else if ( event.keyCode == 188 || event.keyCode == that.options.autocompleteOnKey ) { + if ( !that.options.autocompleteOnComma && that.options.autocompleteOnKey === -1 ) { event.preventDefault() that.process() } @@ -136,6 +136,10 @@ , add: function ( value ) { var that = this + var addValue = {value: value}; + this.element.trigger('add', addValue); + value = addValue.value; + if ( !that.options.allowDuplicates ) { var index = that.inValues(value) if ( index != -1 ) { @@ -151,7 +155,7 @@ this.values.push(value) this.createBadge(value) - this.element.val(this.values.join(', ')) + this.element.val(this.values.join(that.options.separator + ' ')) this.element.trigger('added', [value]) } , remove: function ( index ) { @@ -164,7 +168,7 @@ } } , process: function () { - var values = $.grep($.map(this.input.val().split(','), $.trim), function ( value ) { return value.length > 0 }), + var values = $.grep($.map(this.input.val().split(this.options.separator), $.trim), function ( value ) { return value.length > 0 }), that = this $.each(values, function() { that.add(this) @@ -190,7 +194,10 @@ allowDuplicates: false , caseInsensitive: true , autocompleteOnComma: false + , autocompleteOnKey: -1 , placeholder: '' + , addKeys: [188, 13, 9] + , separator: ',' , source: [] }