0

in the below function the element data contains multiple record sets. What I want to do is filter it so that it returns only the row which has id same as 'selectedModelDrv'. I tried but getting all sorts of errors, can someone tell me the angular way of doing it?

vm.driverModelSelectorGo = function () {

        var selectedModelDrv = vm.driver.driverModelId;
        return datacontext.lookup.getLookupList(datacontext.lookupLists.driverModel).then(function (data) {
            //Filter 'data' so that data.driverModelId == selectedModelDrv
        })
    };

Regards Max

1
  • Lodash .filter? lodash.com/docs#filter - or just loop through the data add an if conditional and push the item to a new array if it fits your conditions? Commented Mar 23, 2015 at 9:07

2 Answers 2

1

Inject $filter to your controller

function myCtrl($scope, $filter)
{
}

Then wherever you want to use that filter, just use it like this:

 $filter('filter');

Like this :

vm.driverModelSelectorGo = function () {    
        var selectedModelDrv = vm.driver.driverModelId;
        return datacontext.lookup.getLookupList(datacontext.lookupLists.driverModel).then(function (data) {
            $scope.filteredData = $filter('filter')(data,{id:selectedModelDrv});
        })
    };
Sign up to request clarification or add additional context in comments.

Comments

0

If data is an array, then you can use Array.prototype.filter to filter the data array and return only results matching your condition.

vm.driverModelSelectorGo = function () {
     var selectedModelDrv = vm.driver.driverModelId;
     return datacontext.lookup.getLookupList(datacontext.lookupLists.driverModel).then(function (data) {
        return data.filter(function(value) {
           return value.driverModelId == selectedModelDrv;
         });
     })
 };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.