Sha256: 7dd1f374a94b7df38c29ac5b1bb5874ecb2e0cfeba57ab996f243422e8800c74

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

<script type="text/javascript">
	function splitAutocomplete( val ) {
		return val.split( /,\s*/ );
	}
	function extractLastAutocomplete( term ) {
		return splitAutocomplete( term ).pop();
	}


  $(function() {
    $('<%= dom_element %>')
	// don't navigate away from the field on tab when selecting an item
		.bind( "keydown", function( event ) {
			if ( event.keyCode === $.ui.keyCode.TAB &&
					$( this ).data( "autocomplete" ).menu.active ) {
				event.preventDefault();
			}
		})
		.autocomplete({
	        minLength: 0,
			source: function( request, response ) {
				// delegate back to autocomplete, but extract the last term
				response( $.ui.autocomplete.filter(
					[<%= encode_results_to_autocomplete( data, field_name ) %>], extractLastAutocomplete( request.term ) ) );
			},
			focus: function() {
				// prevent value inserted on focus
				return false;
			},
			select: function( event, ui ) {
			  	$(this).val('');
				$('<%= ul %>').append( "<li><span>" + ui.item.label + '</span><input type="hidden" value="' + ui.item.value + '" id="<%= dom_element_id %>_' + ui.item.value + '" name="<%= dom_element_id %>[' + ui.item.value + ']" /><a href="#" class="<%= dom_element_id %>_remove_autocomplete">(X)</a></li>' );
				
				// call the callback function if exists
				if(typeof <%= "#{dom_element_id}_on_autocomplete_select" %> == 'function') {
					<%= "#{dom_element_id}_on_autocomplete_select" %>(event, ui);
				}
				
				return false;
			}
	    });
	
	  $( ".<%= dom_element_id %>_remove_autocomplete").on( "click", function() {
	    $(this).parent().remove();
	  });
  });

</script>

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mdd-3.1.0 app/views/template/mdwa/_autocomplete_multiple_tag.html.erb