Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clickable bootstrap row with angular

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?

The following code does not work for me (the ng-click part):

Table:

    <table class="table table-hover">
        <thead>
            <tr>
                <th>Name</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="ingredient in ingredients" ng-click="setSelected({{$index}});">
                <td>{{ ingredient.name }}</td>
                <td>{{ ingredient.status }}</td>
            </tr>
        </tbody>
    </table>

Controller:

$scope.setSelected = function(index) {
    $scope.selected = $scope.ingredients[index];
    console.log($scope.selected);
};
like image 314
Klaasvaak Avatar asked Jul 15 '13 15:07

Klaasvaak


People also ask

How do I make a row clickable in bootstrap?

Building tables using Bootstrap Grid System is much easier and provide a lot more flexibility than using <table> tag. To make the entire row as clickable in this case, one can use a link <a> tag to wrap its content.

How do I make a row in a table clickable?

Using <a> tag inside <td> One more way to make the whole row clickable is to add an <a> inside every <td> element.

How do you link a row to a table in HTML?

Via data attributesrowlink and attribute data-link="row" to a <table> or <tbody> element. For other options append the name to data-, as in data-target="a. mainlink" A cell can be excluded by adding the . rowlink-skip class to the <td> .

How do I add a link to a bootstrap table?

bootstrapTable('insertRow', { index: 0, row: { id: obj.id, name: obj.name, action: obj.id } }); } function ActionFormatter(value) { return '<a href="javascript:void(0)" onclick="Details('+ value +')">Details</a>'; } function Details(id){ ... }


1 Answers

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>
like image 174
marko Avatar answered Nov 15 '22 04:11

marko