Skip to content

Instantly share code, notes, and snippets.

@fillano
Created August 7, 2015 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fillano/5840fa93d22b6294449b to your computer and use it in GitHub Desktop.
Save fillano/5840fa93d22b6294449b to your computer and use it in GitHub Desktop.
just for a reformation of code
$(function() {
function split( val ) {
//alert(val);
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$("#S1")
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "XXX.PHP", {
searchTerm: extractLast( request.term )
}, response );
//alert(response);
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 1 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
formatItem: function(row) {
return "<div style='height:12px'><div style='float:left'>" + //row[0] +
"</div>" +
// row[1] + "/" +
"" + row[2] +"</div>";
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment