Bailey Smith
Bailey Smith

Reputation: 2933

Applying ng-click to a table column

Is it possible to apply ng-click in AngularJS to a table column? I tried the following, which doesn't seem to do the trick.

<table> 
    <colgroup>
      <col ng-repeat="item in items" ng-click="myFunction(item)"> </col>
    </colgroup>

    <thead>
      <tr>
        <th ng-repeat="item in items"> {{item.title}} </th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td ng-repeat="item in items"> {{item.info}} </td>
      </tr>
    </tbody>
 </table>

Upvotes: 2

Views: 2972

Answers (1)

michael
michael

Reputation: 16361

It is not possible do it in the way you have tried. You may register the ng-click on every td element. Another possibility may be registering the ng-click at the table element and use the original dom event to determin wich column was clicked. You can access the event in this way: ng-click="myFunction($event)" then in the controler:

$scope.myFunction = function(e){
   console.log(e);
}

see this post How to find row and col number of a cell in table if you would like to go this way...

Upvotes: 4

Related Questions