Search code examples
javascriptangularjsangular6laravel-5.7angular-ui-select

how to set the updated value in searched field by default after updation in angularjs ui-select directive?


I am facing problem in angularjs ui-select directive. It works fine, it shows me full data in dropdown. When I select a value from search field and want to update it the it is updated but it can't show by default in the search field. I have to manually search again to see the updated value. Let me paste the code...

Here is the angularjs code

$scope.consignee = [];
$http.get("get-consignee", {
 }).then(function(response){

 $scope.consignee = response.data;
 //$scope.consignee.selected = $scope.consignee[0];
 });

Here is the ui-select code

 <ui-select ng-model="consignee.selected" theme="select2">

 <ui-select-match placeholder="Select Consignee">

    <% $select.selected.CONSIGNEE_NAME %>

 </ui-select-match>

 <ui-select-choices ng-repeat="e in consignee | filter: $select.search">

   <div><% e.CONSIGNEE_NAME %></div>

 </ui-select-choices>

 </ui-select>

Let say I have 5 consignee names in dropdown like!

 1. hamad
 2. test2
 3. yasin Gul
 4. hamid
 5. munir

So the problem is when I use this $scope.consignee.selected = $scope.consignee[0]; then at 0 index it gives me hamad name after updation even if I update test2 or yasin Gul it gives me hamad by default set in search field. I know I have 0,1,2,3,4 indexes but I want it dynamic not manual. And I want to set only that name which I update. If I update test2 so that it should give me test2 by default set in searched field after updation and same for yasin Gul etc.. Any help would be appreciated Thanks

enter image description here


Solution

  • cons_id is your selected database id.

    $scope.consignee = [];
    $http.get("get-consignee", {
     }).then(function(response){
    
     $scope.consignee = response.data;
     var index = $scope.consignee.findIndex(x => x.CONSIGNEE_ID==cons_id); // use this
     $scope.consignee.selected = $scope.consignee[index]; 
    });