material icon indicating copy to clipboard operation
material copied to clipboard

autocomplete: response after Ajax call does not update dropdown list

Open alphazhe opened this issue 10 years ago • 23 comments

I am giving ajax call in md-search-text-changed. So every time text is changed ajax call is done. But ajax takes time to give response and before even coming response it shows no data found and when response comes it does not get updated in dropdown even if array provided in md-items is updated. I tried different methods to make ajax call even in md-items but it doesn't work. Then i gave md-delay of 5 sec then it rendered result but that is of previous search query. It does not update result for latest search query.

Can you at least show me proper working example of autocomplete with ajax call in md-search-text-changed so that I'll get idea where I am going wrong or it's issue of md-autocomplete.

alphazhe avatar Jan 06 '16 06:01 alphazhe

@salkz i ended up figuring it out. i needed to add setSelectedItem to the .then of the promise. it looks like this. hope it helps you:

    if(id){
      djangoAuth.request({url: "/patient-api/"+id[1]})
        .then(function(data){
          // success case
          $scope.patient = data.data;
          setSelectedItem($scope.patient.primary_insurer);
          setSelectedItem2($scope.patient.secondary_insurer);
        },function(data){
          // error case
          $scope.errors = data.data;
        });
    }else{ // create new patient case
      $scope.patient = new Object();
    }
  function setSelectedItem(value) {
     $scope.transformedQueryResults.some(function(item) {
       if (item.value == value) {
         $scope.selectedItem = item;
         $scope.searchText = item.lowercase;
         primary_insurer = item.value;
         return true;
       }
     });
     $log.info('Selected item changed to ' + JSON.stringify($scope.selectedItem));
   }

brandonwbrown avatar Aug 08 '17 15:08 brandonwbrown

@rcevs Thanks for the solution, works like charm. Saved my day !

amar-socgen avatar Jun 21 '18 06:06 amar-socgen

The advice from https://github.com/angular/material/issues/6554#issuecomment-267738012 seems to be in the Basic Usage example of the autocomplete docs now.

There is no CodePen demo in the OP, so I can't really verify that any specific bug was fixed here. But it appears that the OP solved his issue and that many others have solved similar issues. I'm going to go ahead and close this.

If you run into some problems figuring out how to make this work, please post a question on the AngularJS Material forums. If you find a verifiable bug in autocomplete, please search for existing issues and if one can't be found please open a new issue using the issue template and providing a CodePen demo.

Thank you so much to the community for helping each other solve these!

Splaktar avatar Jun 21 '18 22:06 Splaktar