Search code examples
javascriptangularjsangularjs-ng-repeat

How do I remove the limit of a limitTo filter on mouseover event?


I have an array of objects called articles each of which contains an array of strings called category. Each article is represented in the DOM with an ngRepeat directive which contains a second ngRepeat directive to represent each category. The second ngRepeat has a limitTo filter that limits the number of categories to 2. When the user mouses over the base article element the limit should be removed and all strings in the category array should be visible.

My problem is that when a user mouses over one element the full array of categories for every object in the articles array is revealed. How can I get the DOM to reveal only the full categories for the element the mouse event takes place on?

Plunkr: https://plnkr.co/edit/PW51BBnQEv589rIdnaCK?p=preview


Solution

  • You can pass your article on which you hover and set in a scope variable. Than simply update your ng-if check to :

    ng-if="hoverMode === true && hoveredArticle === article"
    

    Working example :

    // Code goes here
    
    angular
     .module('myApp', [])
     .controller('myController', ($scope) => {
       
       $scope.articles = [ { date: 'some', category: [ {name: "Sports"}, {name: "News"}, {name: "Cinema"} ] }, { date: 'some', category: [ {name: "A"}, {name: "B"}, {name: "C"} ] }, { date: 'some', category: [ {name: "D"}, {name: "E"}, {name: "F"} ] } ]
     
      $scope.hoverMode = false;
    
      $scope.showAllcat = function(article) {
       $scope.hoveredArticle = article;
       $scope.hoverMode = true;
      }
    	 
      $scope.hideAllcat = function() {
       $scope.hoveredArticle = null;
       console.log('hover working');
       $scope.hoverMode = false;
      }
     
     
     
     });
    <!DOCTYPE html>
    <html ng-app='myApp'>
    
    <head>
      <script data-require="angular.js@4.0.0" data-semver="4.0.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>
      <script data-require="angular.js@4.0.0" data-semver="4.0.0" src="script.ts"></script>
      <script data-require="angular.js@4.0.0" data-semver="4.0.0" src="system.config.js"></script>
      <script data-require="angular.js@4.0.0" data-semver="4.0.0" src="tsconfig.json"></script>
      <link rel="stylesheet" href="style.css" />
      <script src="script.js"></script>
    </head>
    
    <body ng-controller='myController'>
      <table>
        <tbody>
          <tr>
            <th>Date</th>
            <th>Categories</th>
          </tr>
          <tr ng-repeat="article in articles">
            <td><span>{{ article.date }}</span></td>
            <td ng-if="hoverMode === false || hoveredArticle !== article">
                <span ng-repeat="cat in article.category | limitTo: 2">&nbsp;
                    <span class="label label-warning"
                          ng-mouseover="showAllcat(article)">{{ cat.name}}
                    </span>
                </span>
            </td>
            <td ng-if="hoverMode === true && hoveredArticle === article">
                <span ng-repeat="cat in article.category">&nbsp;
                    <span class="label label-warning"
                          ng-mouseleave="hideAllcat()">{{ cat.name}}
                    </span>
                </span>
            </td>
          </tr>
    
    
        </tbody>
      </table>
    </body>
    
    </html>