a45b
a45b

Reputation: 519

Angular js get table td index on click any td on the table

I want to get the index of the td from the table which will be dynamic in number of column and rows. So I can't put ng-click on each td.

I get that using jquery like this ...

            <table border="1">
              <tr>
                <td>0</td>
                <td>1</td>
                <td>2</td>
                <td>3</td>
              </tr>
              <tr>
                <td>0</td>
                <td>1</td>
                <td>2</td>
                <td>3</td>
              </tr>
              <tr>
                <td>0</td>
                <td>1</td>
                <td>2</td>
                <td>3</td>
              </tr> 
            </table>
       <script>
            $("table tr").on("click", "td", function(){
                var col = this.cellIndex;
                //alert(col);
                $('table tr').find('td:eq('+col+'),th:eq('+col+')').remove();
            });
       </script>

So how to get that in my controller

            app.controller('MyCtrl1', function($scope){


            });

I get a dynamic table from where I need only few column. The $scope i will ng-repeat in the table, I want to edit that dynamical and post that data to database.

Upvotes: 0

Views: 2382

Answers (1)

callmekatootie
callmekatootie

Reputation: 11228

You can place a single ng-click directive on your table and pass the '$event' property as an argument to the called function:

<table ng-click="tableClicked($event)">
    <!-- Rest of the table contents -->
</table>

In your controller, you can then define the function to accept the event parameter:

$scope.tableClicked = function (event) {
    //event has all the details of the click event
};

Upvotes: 1

Related Questions