Search code examples
javascriptcssangularjsangularjs-ng-clickng-class

ng-class have class on first row of table and remove it on ng-click


I am using angular js to display a table. When the page loads I want the first row of the table to be highlighted, then when you click on another row of the table I want to remove the hightlighted class.

I've managed to get it to add the class on the top row using

 ng-class="{'selected-row':$first}"

but then I'm not able to remove it. I'd lke to be able to pass a variable from $scope, like this, so that I can change the value on click...

controller....

 $scope.changeClass = function(){
  if ($scope.myClass === "selected-row"){
    $scope.myClass = "off";
    window.alert('change class here to remove highlight!!!');
  } else {
    $scope.myClass = "selected-row";
    window.alert('change it back!!');
  }

html....

 <tr ng-repeat="row in $data" ng-click="changeClass()" ng-class="{myClass:$first}">  

This doesn't work and I can't figure out the syntax (if it's possible!!) Any ideas?

Plunker here: https://plnkr.co/edit/Zs1r6DQcc3vaUDjzkUh8?p=preview


Solution

  • You can use ng-class along with an array which keeps track of your row index to achieve this

    // Code goes here
    var app = angular.module('ngTableApp', ['ngTable'])
              .controller('selectFilterController', function($scope, $filter, $q, NgTableParams) {
                var data = [{name: "Moroni", age: 50},
                            {name: "Tiancum", age: 43},
                            {name: "Jacob", age: 27},
                            {name: "Nephi", age: 29},
                            {name: "Enos", age: 34},
                            {name: "Tiancum", age: 43},
                            {name: "Jacob", age: 27},
                            {name: "Nephi", age: 29},
                            {name: "Enos", age: 34},
                            {name: "Tiancum", age: 43},
                            {name: "Jacob", age: 27},
                            {name: "Nephi", age: 29},
                            {name: "Enos", age: 34},
                            {name: "Tiancum", age: 43},
                            {name: "Jacob", age: 27},
                            {name: "Nephi", age: 29},
                            {name: "Enos", age: 34}
                        ];
    
    
        $scope.class = "selected-row";
        $scope.changeClass = function(index){
         $scope.selected=[];  
         $scope.selected[index]=true;
        };
        $scope.changeClass(0); 
                $scope.names = [{id: "", title: ""}, {id: 'Moroni', title: 'Moroni'}, {id: 'Enos', title: 'Enos'}, {id: 'Nephi', title: 'Nephi'}];
                $scope.tableParams = new NgTableParams({page: 1, count: 10}, {data: data});
                
              })
    /* Styles go here */
    .selected-row{
      border:solid purple 3px;
    }
    
    .off{
      border:none;
    }
    
    .table-striped > tbody > tr:hover{
      border:solid purple 3px;
    }
    
    .table-striped > tbody > tr:active{
      border:solid purple 3px;
    }
    
    
    .table-striped > tbody > tr:focus{
      border:solid purple 3px;
    }
    <!DOCTYPE html>
    <html>
    
      <head>
        <link data-require="bootstrap@3.3.2" data-semver="3.3.2" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ng-table/0.8.3/ng-table.min.css" />
        <link rel="stylesheet" href="style.css" />
        
        <script data-require="angular.js@1.3.7" data-semver="1.3.7" src="https://code.angularjs.org/1.3.7/angular.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/ng-table/0.8.3/ng-table.min.js"></script>
        <script src="script.js"></script>
      </head>
    
      <body ng-app="ngTableApp">
        <h1>NgTable with select filters</h1>
        <div ng-controller="selectFilterController">
          <table ng-table="tableParams" class="table table-striped ng-scope ng-table" show-filter="true">
            <tbody>
              <tr ng-repeat="row in $data" ng-click="changeClass($index)"  ng-class="{'selected-row':selected[$index]}">
              <!--<tr ng-repeat="row in $data" ng-click="changeClass()" ng-class="{myClass:$first}">  -->
                <td data-title="'Name'" filter="{name: 'select'}" filter-data="names" sortable="'name'">{{ row.name }}</td>
                <td data-title="'Age'" filter="{age: 'text'}" sortable="'age'">{{ row.age }}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </body>
    
    </html>