Created
November 23, 2015 14:03
-
-
Save Deiru2k/3bdf1487138b56592079 to your computer and use it in GitHub Desktop.
Ajax select in angular.js using ui-select
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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"> | |
| ✖ | |
| </div> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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