Skip to content

Instantly share code, notes, and snippets.

@ragingprodigy
Created December 11, 2014 12:17
Show Gist options
  • Save ragingprodigy/671142d37a7c588383ba to your computer and use it in GitHub Desktop.
Save ragingprodigy/671142d37a7c588383ba to your computer and use it in GitHub Desktop.
Select2 AngularJs directive example with custom query call to append data to the request. Note: Providing ajax settings is not needed (and not used) when query is provided. Left here as a full example.
# The "query" configuration is adapted from this gist: https://gist.github.com/techniq/4609063
.directive 'remoteSelectPlugin', ['$http', ($http) ->
{
restrict: 'A'
link: (scope, elem, attr) ->
angular.element(elem).select2({
placeholder: attr.remoteSelectPlugin
minimumInputLength: 1
query: (options) ->
$http.get "http://example.com"
.then (response) ->
options.callback({more:false, results: response.data})
formatResult: (judgment) ->
'<p>' + judgment.case_title2 + '</p>' + '<span class="label label-primary">' + judgment.suitno + '</span>' + '<span class="label label-success pull-right">' + judgment.date + '</span>'
formatSelection: (judgment) ->
"#{judgment.case_title2} - #{judgment.suitno}"
dropdownCssClass: "bigdrop"
})
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment