javascript - How do I move up/down a highlight selected row from a table in angularjs? -
i'm new in angularjs, i'm trying table move up/down selected highlight row buttons..
for example, after select first row need change second position down button...
i need help!
var foodapp = angular.module('foodapp',[]); foodapp.controller('foodctrl',function($scope){ $scope.selectedrow = null; $scope.fooditems = [{ name:'noodles', price:'10', quantity:'1' }, { name:'pasta', price:'20', quantity:'2' }, { name:'pizza', price:'30', quantity:'1' }, { name:'chicken tikka', price:'100', quantity:'1' }]; $scope.setclickedrow = function(index){ $scope.selectedrow = index; } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <style> .selected { background-color:black; color:white; font-weight:bold; } </style> <html> <head> <link href="css/bootstrap.min.css" rel="stylesheet" media="screen" /> <link href="css/bootstrap-theme.min.css" rel="stylesheet" media="screen"/> </head> <body ng-app="foodapp" ng-controller="foodctrl"> <table class="table table-bordered" > <tr> <th>#</th> <th>name</th> <th>price</th> <th>quantity</th> </tr> <tr ng-repeat="item in fooditems" ng-class="{'selected':$index == selectedrow}" ng-click="setclickedrow($index)"> <td>{{$index}}</td> <td>{{item.name}}</td> <td>{{item.price}}</td> <td>{{item.quantity}}</td> </tr> </table> <div> selectedrow = {{selectedrow}} </div> <script src="js/angular.js"></script> <script src="js/foodctrl.js"></script> </body> </html>
use ng-click
directives , add appropriate handlers controller.
var foodapp = angular.module('foodapp', []); foodapp.controller('foodctrl', function($scope) { $scope.selectedrow = null; $scope.fooditems = [{ name: 'noodles', price: '10', quantity: '1' }, { name: 'pasta', price: '20', quantity: '2' }, { name: 'pizza', price: '30', quantity: '1' }, { name: 'chicken tikka', price: '100', quantity: '1' }]; $scope.setclickedrow = function(index) { $scope.selectedrow = index; } $scope.moveup = function(num) { if (num > 0) { tmp = $scope.fooditems[num - 1]; $scope.fooditems[num - 1] = $scope.fooditems[num]; $scope.fooditems[num] = tmp; $scope.selectedrow--; } } $scope.movedown = function(num) { if (num < $scope.fooditems.length - 1) { tmp = $scope.fooditems[num + 1]; $scope.fooditems[num + 1] = $scope.fooditems[num]; $scope.fooditems[num] = tmp; $scope.selectedrow++; } } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <style> .selected { background-color: black; color: white; font-weight: bold; } </style> <html> <head> <link href="css/bootstrap.min.css" rel="stylesheet" media="screen" /> <link href="css/bootstrap-theme.min.css" rel="stylesheet" media="screen" /> </head> <body ng-app="foodapp" ng-controller="foodctrl"> <table class="table table-bordered" ng-keydown="key($event)"> <tr> <th>#</th> <th>name</th> <th>price</th> <th>quantity</th> </tr> <tr ng-repeat="item in fooditems" ng-class="{'selected':$index == selectedrow}" ng-click="setclickedrow($index)"> <td>{{$index}}</td> <td>{{item.name}}</td> <td>{{item.price}}</td> <td>{{item.quantity}}</td> </tr> </table> <button type="button" ng-click="moveup(selectedrow)">move up</button> <button type="button" ng-click="movedown(selectedrow)">move down</button> <div> selectedrow = {{selectedrow}} </div> </body> </html>
Comments
Post a Comment