Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ui-select component, multiple select + remote server

How can I use ui-select to get data from a REST service in real time?

There is an example of ui-select with preloaded data, but for the same case, let's say there are 1000 users, and you want to select 5 of them and search by name for example, can you call a service in real time? Somehow like typeahead.

like image 552
David Dury Avatar asked Dec 25 '22 20:12

David Dury


1 Answers

From the documentation:

Setup your directive like so:

<ui-select multiple ng-model="yourmodel" theme="select2" ng-disabled="disabled" style="width: 800px;">
    <ui-select-choices repeat="address in addresses track by $index"
         refresh="refreshAddresses($select.search)"
         refresh-delay="0">
    </ui-select-choices>
</ui-select>

The refreshAddress function is going to be called when searching. Here is what that would look like with an async call to the server:

function MyCtrl(){
   $scope.addresses = [];
   $scope.refreshAddresses = function(address) {
     var params = {address: address, sensor: false};
       return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {params: params})
  .then(function(response) {
    $scope.addresses = response.data.results
  });
};
}

This example is calling a google api endpoint to get data. You would call your endpoint instead.

Here's a link to the documentation: https://github.com/angular-ui/ui-select/wiki/ui-select

like image 155
nweg Avatar answered Dec 29 '22 08:12

nweg