jquery autocomplete dynamic source from data attribute -
there few topics on same subject, non satisfied me!
i want define source of autocomplete through data attribute, so:
<input data-behaviour='autocomplete' data-source='/path/to/source'>
i'm unable programmatically through source
method:
$ -> $('[data-behaviour~=autocomplete]').autocomplete source: (req, resp) -> return $(this).data('source')
here non-working snippet play with...
edit: workaround i've found set through create
method:
$ -> $('[data-behaviour~=autocomplete]').autocomplete create: (event, ui) -> $(this).autocomplete( "option", "source", $(this).data('source') )
check snippet
but don't approach, i'm pretty sure better can done
i'm not familiar coffeescript; jquery ui automplete have source
option allow make custom stuff; in case can use third option (function) using request
, setting response
parameter.
function: third variation, callback, provides flexibility , can used connect data source autocomplete. callback gets 2 arguments: request object, single term property, refers value in text input. example, if user enters "new yo" in city field, autocomplete term equal "new yo". response callback, expects single argument: data suggest user. data should filtered based on provided term, , can in of formats described above simple local data. it's important when providing custom source callback handle errors during request. must call response callback if encounter error. ensures widget has correct state. when filtering data locally, can make use of built-in $.ui.autocomplete.escaperegex function. it'll take single string argument , escape regex characters, making result safe pass new regexp().
ref: http://api.jqueryui.com/autocomplete/#option-source
code:
$ -> thisel = $('[data-behaviour~=autocomplete]'); $('[data-behaviour~=autocomplete]').autocomplete source: (req, resp) -> resp(thisel.data('source'));
is example have implement filtering pattern.
Comments
Post a Comment