1

I have the following data in a table:

data = [
  {id: 1, name: 'Manchester United', type: 'Soccer', featured: true, country: 'England'},
  {id: 2, name: 'Manchester City', type: 'Soccer', featured: false, country: 'England'},
  {id: 3, name: 'Dallas Mavericks', type: 'Basketball', featured: true, country: 'USA'},
  {id: 4, name: 'Indian Cricket Team', type: 'Cricket', featured: true, country: 'India'},
  {id: 5, name: 'Australian Cricket Team', type: 'Cricket', featured: false, country: 'Australia'},
  {id: 6, name: 'Los Angeles Lakers', type: 'Basketball', featured: true, country: 'USA'},
  {id: 7, name: 'Los Angeles Clippers', type: 'Basketball', featured: false, country: 'USA'}
];

I want to add a drop down box with countries. Selecting a country should filter the table to show only sports from that country.

The problem is that I am unable to show the unique countries in the dropdown.

Kindly Help...Thanks in advance.

9
  • 1
    Try to obtain a list of countries from your data with a plain old JavaScript function, and then just put this function in your controller. Commented Jul 18, 2014 at 19:17
  • I am using the following code : <select data-ng-model="countryModel" ng-options="dd.country for dd in data | unique: data.country"> <option value="">--Choose Country--</option> </select> I was thinking of using a filter (for e.g. 'unique') , how to create a filter is a problem :( Commented Jul 18, 2014 at 19:20
  • I was thinking of creating a filter, but unable to do that :( Commented Jul 18, 2014 at 19:21
  • Using a filter here is plain wrong. Loop through data once and build the list of unique countries. Commented Jul 18, 2014 at 19:23
  • This is a bit offtopic, this is a javascript question and not an angular question. Try using from javascript libraries like lowdash to preprocess your data. Commented Jul 18, 2014 at 19:24

1 Answer 1

2

I modified your plnkr, http://plnkr.co/edit/60GO0zhSGoEUm7tu6mbc?p=preview

Filter: as it is from ng-options and unique filter not displaying angular.js

Select part: <select data-ng-model="countryModel" ng-options="dd.country as dd.country for dd in data | unique:'country'">

ng-repeat part <tr data-ng-repeat="dat in data | filter: countryModel" data-ng-style="set_color(dat)">

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.