Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have done an autocomplete from a json file in an input tag, the json file has multiple attributes, i want a separate input carry a different attribute from the json file, eg: one one input should fetch the "driver_name" and other should fetch the "vehicle_number", the second input tag needs to be filled automatically, once we select the data from the first input tag

[
{
"id": 1,
"driver_name": "Rohit",
"driver_phone": "9176649143",
"vehicle_type": "indica",
"vehicle_number": "TN 06 AR 4556"
},
{
"id": 2,
"driver_name": "john",
"driver_phone": "9176648143",
"vehicle_type": "fiat",
"vehicle_number": "CA 06 AR 4556"
}
]

HTML

<div ng-controller="TypeaheadCtrl">
    <div class="input-group m-bot15">
      <input ng-model="tripsheet.vehicle_no" type="text" class="form-control input-lg" placeholder="Vehicle No" typeahead="Unidade.vehicle_number as Unidade.vehicle_number for Unidade in getUnidades($viewValue)"> 
    </div>

    <div class="input-group m-bot15">
      <input ng-model="tripsheet.driver_name" type="text" class="form-control input-lg" placeholder="Vehicle No" typeahead="Unidade.driver_name as Unidade.driver_name for Unidade in getUnidades($viewValue)"> 
    </div>
</div>

JS

function TypeaheadCtrl($scope, $http) {

 $scope.Selected = undefined;
 $scope.getUnidades = function($viewValue) {
    return $http.get(urldr + '/all').then(function(response){
      return response.data;
    });
 };
}
share|improve this question
    
Pass in a 2nd argument as the filter key, loop your data on return and only return values within the filter –  tymeJV Jun 22 at 14:00
    
i tried but it does'nt work –  user3689593 Jun 22 at 14:33

1 Answer 1

Should be able to just add a filter to the typeahead function:

$scope.getUnidades = function($viewValue, filter) {
    return $http.get(urldr + '/all').then(function(response){
        var values = [];
        for (var i = 0; i < response.data.length; i++) {
            if (response.data[i][filter].indexOf($viewValue) > -1) 
                values.push(response.data[i])
        }
        return values;
    });
};

And filter is the property name to filter on

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.