I am currently developing a to-do list using AngularJS. It's almost completed but I have a question regarding highlighting the entire row when in editing mode by adding a CSS class "yellow". Unfortunately, I'm unsure how to achieve this.
Additionally, I would like some feedback on whether my coding approach is correct or incorrect.
Here is the JSFiddle link for reference:
http://jsfiddle.net/mcVfK/1338/
Below you can find the code snippets:
HTML:
<div ng-app="myapp">
<div class="container" ng-controller="mainCtrl">
<h3>Todo List</h3>
<input type="text" class="form-control" placeholder="create your todos" ng-model="newItem">
<p class="help-block text-center red" ng-show="!newItem && empty">*Fill the field.</p>
<br>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Todo</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="todoList in todoLists">
<td>{{$index+1}}</td>
<td>{{todoList.name}}</td>
<td>{{todoList.edit}}</td>
<td><a class="btn {{disabled}} pull-right" href="" ng-click="remove(todoList)">delete</a>
<a class="btn {{disabled}} pull-right" href="" ng-click="edit($index)">edit</a> </td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-primary btn-lg btn-block" ng-click="add()" ng-hide="editMode">ADD</button>
<button type="button" class="btn btn-default btn-lg btn-block" ng-click="update(newItem)" ng-show="editMode">UPDATE</button>
</div>
</div>
JavaScript file:
var app = angular.module("myapp", []);
app.controller("mainCtrl", ["$scope", "$rootScope", function($scope, $rootScope){
$scope.empty = false;
$scope.editMode = false;
$scope.todoLists = [{name : "one", edit : "false"},{name : "two", edit : "false"}];
$scope.add = function(){
if(!$scope.newItem == ""){
$scope.todoLists.push({name:$scope.newItem, edit:"false"});
$scope.newItem = "";
$scope.empty = false;
}else{
$scope.empty = true;
};
};
$scope.remove = function(item){
var index = $scope.todoLists.indexOf(item);
$scope.todoLists.splice(index, 1);
};
$scope.edit = function(index){
$rootScope.ind = index;
$scope.newItem = $scope.todoLists[$rootScope.ind].name;
$scope.editMode = true;
$scope.disabled = "disabled";
$scope.todoLists[index].edit = "true";
};
$scope.update = function(item){
if(!$scope.newItem == ""){
$scope.todoLists[$rootScope.ind].name = item;
$scope.todoLists[$rootScope.ind].edit = "false";
$scope.editMode = false;
$scope.newItem = "";
$scope.disabled = "";
}else{
$scope.empty = true;
};
};
}]);
CSS file:
.yellow{
background:yellow;
}
.red{
color:red;
}