0

Currently i am returning carType results like so:

Truck  
SUV  
Sedan  
Truck  
SUV 

I would like to populate a filter select box so it will have the following as options without repetition:

 Truck
 Sedan
 SUV

My current problem is that my select box displays all CarType results which includes repeated items which i would like to eliminate.

Also my display of results after selecting a filter is not working and i am not getting an error so i don't spot where my problem is exactly.

<label for="filters">Filter on Car Type</label>
        <select id="filters" ng-model="selectedType" ng-options="x.type as x.type for x in result">
            <option value="">Select Car Type</option>
        </select>

in my table where i am wanting to display results based on filter:

<tr ng-repeat="x in result|filter:selectedTDL">
            <td data-title="Car Type">{{x.type}}</td>
            <td data-title="Year">{{x.year}}</td>
        </tr>

not sure what i am missing or what is wrong for the results to display.

UPDATE: i was able to resolve the filtering portion and display based on filtered option. But not sure how to eliminate duplication in the filtering select statement.

1

1 Answer 1

1

You should filter out the duplicate values from your list yourself. You could use filters to achieve this.

var app = angular.module("sampleApp", []);

app.controller("sampleController", ["$scope",
  function($scope) {
    $scope.result = [{
      type: "SUV"
    }, {
      type: "SUV"
    }, {
      type: "Truck"
    }, {
      type: "SUV"
    }, {
      type: "Van"
    }, {
      type: "Truck"
    }];

  }
]);


app.filter('unique', function() {
  
  return function(input) {
    let output = [];
    input.forEach((item) => {
      if (!output.includes(item.type)) {
        output.push(item.type);
      }
    });
    return output;
  };
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<div ng-app="sampleApp">
  <div ng-controller="sampleController">
    <label for="filters">Filter on Car Type</label>
    <select id="filters" ng-model="selectedType" ng-options="x  for x in result | unique">
      <option value="">Select Car Type</option>
    </select>

  </div>
</div>

Sign up to request clarification or add additional context in comments.

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.