Filter response in jQuery autocomplete -
i'm trying implement jquery.autocomplete use enter tags (much here on so). however, don't want show tags have in list. if have added foo don't want show suggestion. list of tags fetched server. however, have issues altering ui.content object.
i use response method found here:
response: (event, ui)-> # fetch added tags current = $('input#photo_tag_list').val().split(',') # filter out added tags result ui.content = ui.content.filter (el) -> $.inarray(el.label, current) == -1 however, above doesn't work. still shows tags. if inspect ui.content can see looks correct... above async or something?!
the weird thing if do:
ui.content.push({label: "foo", value:"bar"}) foo shows in autocomplete list. if this:
ui.content = [] it still shows returned. expect empty list.
why be? how modify result list correctly?
update got work based on andrew whitaker's solution.
this happening because of way javascript passes parameters. ui.content copy of reference array. cannot modify reference , expect results visible outside of function. better explained here.
for simplified example, check out:
function modify(item) { item.arr = []; } var obj = { text: 'hi', arr: [1,2,3,4] }; modify(obj); console.log(obj); fiddle: http://jsfiddle.net/9swwa/1/
the log statement show obj.arr still [1, 2, 3, 4]. have same problem.
so around can use splice remove items array. unfortunately you're going have add bit more code:
i = ui.content.length - 1 while >= 0 currentitem = ui.content[i] index = $.inarray(currentitem, current) ui.content.splice i, 1 if index -1 i--
Comments
Post a Comment