Skip to content

Instantly share code, notes, and snippets.

@Deiru2k
Created November 23, 2015 14:03
Show Gist options
  • Select an option

  • Save Deiru2k/3bdf1487138b56592079 to your computer and use it in GitHub Desktop.

Select an option

Save Deiru2k/3bdf1487138b56592079 to your computer and use it in GitHub Desktop.
Ajax select in angular.js using ui-select
<div class="row search-select-group">
<div class="search-select"
ng-class="{'col-xs-12': !ajs.allowClear, 'col-xs-11': ajs.allowClear}">
<ui-select ng-model="ajs.model" append-to-body="true">
<ui-select-match allow-clear="ajs.allowClear"
placeholder="{{ajs.placeholder}}">
{{$select.selected[ajs.displayField]}}
</ui-select-match>
<ui-select-choices
repeat="objs[ajs.modelField] as obj in ajs.lookup | filter: $select.search"
refresh="ajs.match($select.search)"
refresh-delay=0>
<div ng-bind-html="obj[ajs.displayField] | highlight: $select.search">
</div>
</ui-select-choices>
</ui-select>
</div>
<div class="col-xs-1 search-clear-select"
ng-if="ajs.allowClear"
ng-click="ajs.model = undefined">
&#10006;
</div>
</div>
app.controller(`AjaxSelectController`, function($API) {
if (angular.isDefined(this.model)) {
const query = {
filter: [`${this.modelField} eq ${this.model}`],
};
$API.get(this.resource, formatQuery(query)).then((response) => {
this.activities = response.body.Data;
});
}
this.match = (filter) => {
const query = {
top: this.top || 5,
filter: [`substringof('${filter}', ${this.queryParam})`],
};
$API.get(this.resource, formatQuery(query)).then(({body}) => {
this.lookup = body.Data;
});
};
});
app.directive(`ajaxSelect`, () => {
return {
restrict: `E`,
templateUrl: `/views/directives/ajax-select.html`,
controller: `AjaxSelectController as ajs`,
bindToController: true,
scope: {
resource: `@`,
queryParam: `@`,
top: `@`,
modelField: `@`,
displayField: `@`,
placeholder: `@`,
model: `=`,
allowClear: `=`,
},
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment