0

I'm having three different sets of drop downs and also search box to apply filter for a table. My table should filter if someone enters text in textbox (or) depending on the value selected in one of the three drop downs (textbox searches and drop down searches are exclusive of each other).

This is my HTML:

    <div ng-app="myModule" ng-controller="myController">
  <nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <form class="navbar-form navbar-left form-horizontal" role="search">
          <div class="form-group">
            <label for="searchSeries" class="control-label">Find:</label>
            <input type="text" id="searchSeries" ng-model="searchText" class="form-control" placeholder="Search TV Series" />
            <div class="btn-group btn-input clearfix">
              <button type="button" class="btn btn-default dropdown-toggle form-control" data-toggle="dropdown"> <span data-bind="label">Select One</span> <span class="caret"></span> </button>
              <ul class="dropdown-menu" role="menu">
                <li><a href="#" id="action">Action</a></li>
                <li><a href="#" id="animation">Animation</a></li>
                <li><a href="#" id="comedy">Comedy</a></li>
                <li><a href="#" id="fantasy">Fantasy</a></li>
                <li><a href="#" id="drama">Drama</a></li>
                <li><a href="#" id="mystery">Mystery</a></li>
                <li><a href="#" id="romance">Romance</a></li>
                <li><a href="#" id="science-fiction">Science Fiction</a></li>
                <li><a href="#" id="mini-series">Mini Series</a></li>
                <li><a href="#" id="reality">Reality</a></li>
                <li><a href="#" id="documentary">Documentary</a></li>
                <li><a href="#" id="crime">Crime</a></li>
                <li><a href="#" id="game-show">Game Show</a></li>
                <li><a href="#" id="talk-show">Talk Show</a></li>
                <li><a href="#" id="adventure">Adventure</a></li>
                <li><a href="#" id="home-garden">Home and Garden</a></li>
                <li><a href="#" id="thriller">Thriller</a></li>
                <li><a href="#" id="sport">Sport</a></li>
                <li><a href="#" id="family">Family</a></li>
                <li><a href="#" id="children">Children</a></li>
                <li><a href="#" id="news">News</a></li>
                <li><a href="#" id="horror">Horror</a></li>
              </ul>
            </div>
            <select ng-model="properties.config" ng-options="prop.value as prop.name for prop in properties.configs">
            </select>
            <select ng-model="order.config" ng-options="template.value as template.name for template in order.configs">
            </select>
          </div>
        </form>
      </div>
      <!-- /.navbar-collapse -->
    </div>
    <!-- /.container-fluid -->
  </nav>
  <div class="container-fluid">
    <div class="table-responsive">
      <table class="table table-bordered">
        <tbody>
          <tr ng-repeat="user in users[0].tvseries | filter: searchText | filter: order.config | filter:properties.config">
            <td><img ng-src="{{user.thumbnail}}" alt="" /></td>
            <td><div>{{user.tv_show_name}}</div>
              <div>{{user.brief_description}}</div>
              <div>{{user.rating}}</div></td>
            <td><div>{{user.show_time}}</div>
              <div>{{user.genre}}</div>
              <div>{{user.current_season}}</div>
              <div>{{user.current_episode}}</div></td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>

My controllers.js:

var myModule = angular.module('myModule', []);
myModule.controller('myController', function($scope, userRepository) {
    userRepository.getAllUsers().success(function(users) {$scope.users = users;});
    $scope.order = {};
    $scope.properties = {};
      //Configuration
      $scope.order.configs = [
                                 {'name': 'Ascending',
                                  'value': 'tv_show_name'},
                                 {'name': 'Descending',
                                  'value': '-tv_show_name'}
                               ];
      $scope.properties.configs = [
                                 {'name': 'Show Time',
                                  'value': 'show_time'},
                                 {'name': 'Rating',
                                  'value': 'rating'}
                               ];
      //Setting first option as selected in configuration select
      $scope.order.config = $scope.order.configs[0].value;
      $scope.properties.config = $scope.properties.configs[0].value;
});
myModule.factory('userRepository', function($http) {
    return {
        getAllUsers: function() {
            var url = "https://api.mongolab.com/api/1/databases/tvshowsdb/collections/tvshowsdbcollections?apiKey=e2SbmkVmLOQN-wH_ga84n9prYsgU8lQ5";
            return $http.get(url);
        }
    };
});

Note that: when I apply "filter: searchText" in my html, it works fine and I face no issue. But when I try to apply two more filters "filter: order.config | filter:properties.config", my code doesn't execute. Please help me understand where I'm going wrong :(

FlashyFuddyFuddy
  • 193
  • 2
  • 3
  • 20

1 Answers1

1

Because you are adding and removing - before tv_show_name, I think that you are trying to sort the results by multiple fields? If that is the case, the syntax would be:
orderBy: [order.config, properties.config].

Here is a demo: http://plnkr.co/edit/yLYQGYGcGZMqoj6yfqoT?p=preview

Note: the demo is reading from a json file, instead of the mongolab api, because I needed to add duplicate names and start times, to show the effects of multi-level sorting. Also, notice that I changed the order of the selects to match the order in which you were applying the filters. I think it is intuitive that the first select take precedence over the next select.


Update
If you only want to sort by one of the options at a time, you can use a string variable, much like the orderBy api demo:

<div ng-click="predicate = 'tv_show_name'">Show Name ascending</div>
<div ng-click="predicate = '-tv_show_name'">Show Name descending</div>
<div ng-click="predicate = 'show_time'">Show Time</div>
<div ng-click="predicate = 'rating'">Rating</div>
...
<tr ng-repeat="user in users[0].tvseries | filter: searchText | orderBy:predicate"> 

Here is the updated Demo

Community
  • 1
  • 1
j.wittwer
  • 9,497
  • 3
  • 30
  • 32
  • Hi j.wittwer, Thanks for the response. I'm adding - before tv_show_name, so as to reverse the order of sorting. In this example, reverse sorting is Descending. Also, the sort order of one select box in exclusive of the other. They are independent of each other. I already tried with the code you had suggested in your post. But it seems to work only for ascending/descending but the rating and show time isn't working :( – FlashyFuddyFuddy Apr 14 '14 at 03:12
  • it doesn't seem to be working because the data doesn't have multiple times per show. that is why in my demo i created my own json file, and duplicated game of thrones, each time with a different rating and time. that way you can see both levels of the sort in action. if your live data doesn't have this duplication, sorting by more than one field will have no effect. – j.wittwer Apr 14 '14 at 03:21
  • you do have to look closely to see the small rating and time data shift when the second dropdown changes. in a better demo, i would have changed the description of each instance of game of thrones, so that the sort would be easier to see happen. in fact, i will update it now. – j.wittwer Apr 14 '14 at 03:25
  • I got that point clearly now. So do you say that exclusive sort is not possible at all? That is, when I click ascending, I mean only ascending of Show Name. Similarly for descending. And When I click rating, it is not necessary for the Shows to have duplicates. But it needs to compare the rating of all the Shows and sort. Also when I click on Show time, it should compare the whole list. Isn't that possible? - Ultimately I mean the drop down results are not dependent on one another. – FlashyFuddyFuddy Apr 14 '14 at 03:34
  • 1
    it is possible. just set a single variable based on the selection. dropdowns may not make sense. radio buttons would be better. or you can do it simply like this: http://plnkr.co/edit/BCgUKgtwxaR2ahscLjk3?p=preview – j.wittwer Apr 14 '14 at 03:51
  • I think this would be a better solution. Thanks a ton j.wittwer for your time :) – FlashyFuddyFuddy Apr 14 '14 at 04:07
  • nice catch on the rating predicate, @FlashyFuddyFuddy – j.wittwer Apr 14 '14 at 04:22