Clickable bootstrap row with angular

前端 未结 4 990
忘了有多久
忘了有多久 2021-02-13 12:20

I have got a table, styled with bootstrap. The content of this table is filled using Angular.js. How do I make a row clickable so it will call a function in the scope?

T

相关标签:
4条回答
  • 2021-02-13 12:28

    HTML:

    <table class="table-hover">
    

    CSS:

    .table-hover > tbody > tr:hover {
        background-color: #f5f5f5;
    }
    

    And if also whats made the tr selectable:

    HTML:

    <tr ng-click="doSomething()">
    

    CSS:

    tr[ng-click] {
        cursor: pointer;
    }
    

    View JSFiddle Sample

    0 讨论(0)
  • 2021-02-13 12:33

    Angular 6

    HTML:

    <tr (click)="doSomething()">
    

    CSS:

    tr:hover {
        cursor: pointer;
    }
    
    0 讨论(0)
  • 2021-02-13 12:39

    You could just pass the ingredient in argument

    ng-click="setSelected(ingredient)"

    and in controller

       $scope.setSelected = function(my_ingredient) {
           $scope.selected = my_ingredient;
           console.log($scope.selected);
       };
    
    0 讨论(0)
  • 2021-02-13 12:45

    Suggestion and the fiddle:

    <tr ng-repeat="ingredient in ingredients" ng-click="setSelected();">
        <td>{{ ingredient.name }}</td>
        <td>{{ ingredient.status }}</td>
    </tr>
    
    <script>
    var myApp = angular.module('myApp',[]);
    
    function MyCtrl($scope) {
    
        $scope.ingredients = [
            {'name': 'potato'},
            {'name': 'tomato'}
        ];
    
        $scope.setSelected = function() {
            $scope.selected = this.ingredient;
            console.log($scope.selected);
        };
    
    }
    </script>
    
    0 讨论(0)
提交回复
热议问题