0

How to write the directive to highlight the selected row in a table,

I have 20 plus tables and i need to write a common function for this table.

I am writing the same code in controllers,

$scope.selectedRow = null;
$scope.rowHighilited = function(row){
  $scope.selectedRow = row;
};
<table>
<tr>
<th>
  first row
</th>
</tr>
<tr data-ng-click="rowHighilited($index)">row1</tr>
</table>

1 Answer 1

1

Try something like this:

JS:

  $scope.selectedRow = null;
  $scope.rowHighilited = function (idSelected) {
     $scope.selectedRow = idSelected;
  };

HTML :

  <tr ng-repeat="row in rows" ng-click="rowHighilited(row.id)" ng-class="{selected: row.id === selectedRow}">
    <td> ...</td>
  </tr>

You can learn more in the ngClass documentation

Sign up to request clarification or add additional context in comments.

1 Comment

I am using similar way, but i need to avoid multiple times repeating the same code

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.