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);
};
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.
Using <a> tag inside <td> One more way to make the whole row clickable is to add an <a> inside every <td> element.
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> .
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){ ... }
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With