javascript - I can not populate JQuery Autocomplete input with a json request to cakephp -
i using jquery autocomplete send json request cakephp application, javascript code http://jqueryui.com/autocomplete/
$(function() { function split( val ) { return val.split( /,\s*/ ); } function extractlast( term ) { return split( term ).pop(); } $( "#tag-string" ) // don't navigate away field on tab when selecting item .bind( "keydown", function( event ) { if ( event.keycode === $.ui.keycode.tab && $( ).autocomplete( "instance" ).menu.active ) { event.preventdefault(); } }) .autocomplete({ source:function(request, response){ $.getjson("/articles/gettags/" + request.term + ".json", { term : extractlast(request.term) }, 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; }, select: function( event, ui ) { var terms = split( this.value ); // remove current input terms.pop(); // add selected item terms.push( ui.item.value ); // add placeholder comma-and-space @ end terms.push( "" ); this.value = terms.join( ", " ); return false; } }); });(jquery);
if run xmlhttprequests on chrome js console (where "c" term search for)
$.getjson("/articles/gettags/c.json")
i response text:
responsetext: "{↵ "tags": [↵ {↵ "tag": "calles"↵ },↵ {↵ "tag": "circulacion"↵ },↵ {↵ "tag": "cognicion"↵ },↵ {↵ "tag": "creatividad"↵ }↵ ]↵}"
but input field not populate values. (i've uploaded image because easiest way explaint it) on "tag string" input field
finally solved. json response send jquery autocomplete must formated "value" , "label".
var ac = [ { label: "one thing", value: "one-thing" }, { label: "two thing", value: "two-thing" },
]
Comments
Post a Comment