2

I'm learning angularjs and was struggling with selection as well. I know this question is already answered but wanted to share some more code nevertheless.

In my test I have two listboxes: tournament name and tournament city

tournament city should be unique

When i select tournament name it will show up corresponding tournament name list it coming correct only When i seclect city in selectbox it wont populate any thing

app.controller('MainCtrl', function($scope) {
   $scope.tournaments = [
     { id: 1, name: 'Banglore Cup', city:'Banglore' },
     { id: 2, name: 'Banglore Cup1', city:'Mumbai' },
     { id: 3, name: 'Banglore Cup2', city:'Banglore' },
     { id: 4, name: 'Mumbai Cup1', city:'Mumbai' },
     { id: 5, name: 'Mumbai Cup2', city:'Banglore' }
   ];
});



        <div class="row">
            <div class="col s4 input-field ">
                <select class="browser-default"  name="show-filter" ng-model="tournamentFilter.id" ng-options="eachTournament.id as eachTournament.name for eachTournament in tournaments">
                    <option value="" selected> All</option>
                </select>
            </div>
            <div class="col s4 input-field ">
                <select class="browser-default" name="show-filter" ng-model="tournamentFilter.city" ng-options="eachTournament.city as eachTournament.city for eachTournament in tournaments| unique:'city'">
                    <option value="" selected> All</option>
                </select>
            </div>
        </div>
    </div>
    <div class="col s12">
        <table class="list_tournament">
            <tbody ng-repeat="eachTournament in tournaments | filter:{id:tournamentFilter.id||undefined} | orderBy:'name'">
                <tr class="row nomargin">
                    <td class="col s4 m3 tournament_list_location">
                        <a href="#/viewtournament/{{eachTournament.id}}">{{eachTournament.name}}</a>
                    </td>
                    <td class="tournament_list_location col s12 m3">
                        {{eachTournament.city}}
                    </td>
                </tr>
            </tbody>
        </table>
    </div>

Can you help me to getting data

9
  • please add a fiddle or plunker Commented Dec 29, 2016 at 7:57
  • 1
    @cherry plnkr.co/edit/I4iCnEoRBDF8OWaX9vry?p=preview Commented Dec 29, 2016 at 8:00
  • @SaEChowdary thankyou i actually i dont know how to create fiddle or plnkr.please help me my question Commented Dec 29, 2016 at 8:03
  • See the above plunk @Aravi Commented Dec 29, 2016 at 8:05
  • @SaEChowdary yea i seen but entire city list is not populated why?.Can you undestand my use case.Both should be individual.Each tournament have only one city.But same city have multiple tournaments Commented Dec 29, 2016 at 8:10

1 Answer 1

1

Use this custom filter from here to get only unique values : Updated Plunkr

app.filter('unique', function() {
    return function(input, key) {
        var unique = {};
        var uniqueList = [];
        for(var i = 0; i < input.length; i++){
            if(typeof unique[input[i][key]] == "undefined"){
                unique[input[i][key]] = "";
                uniqueList.push(input[i]);
            }
        }
        return uniqueList;
    };
});

Html: Change the filter like below :

<tbody ng-repeat="eachTournament in tournaments | filter:{id:tournamentFilter.id, city: tournamentFilter.city||undefined} | orderBy:'name'">
Sign up to request clarification or add additional context in comments.

4 Comments

but initially when i select city nothing happen
City is getting selected right ? What is your expectation in that situation ?
the problem is when i click any one city or name then after select all it wont populate understand checkit your plunker
Nice work suzo you help me alot thank you please correct your plunker tournament name select is closed before optioning ok....Thank you verymuch

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.