Currently, I am in the process of designing the user interface for a basic todo application. While my backend is already set up, I am encountering difficulties implementing the necessary functions on the front end. Specifically, I am struggling with adding a search and update feature to the app. Any assistance or guidance in this matter would be greatly appreciated.
index.js
function TodoCtrl($scope) {
$scope.todos = [
{id:1, text:'Mow the lawn', selected:false},
{id:2, text:'Wash the car', selected:false}
];
$scope.getTotalTodos = function () {
return $scope.todos.length;
};
$scope.addTask = function () {
$scope.todos.push({text:$scope.text, id:$scope.id, date_created:Date.now, selected:false});
$scope.text= '';
$scope.id= '';
};
$scope.removeTask = function () {
$scope.todos = _.filter($scope.todos, function(todo){
return !todo.selected;
});
};
$scope.showDetails = function(task_id) {
var found = $filter('filter')($scope.todos, {id: task_id}, true);
if (found.length) {
$scope.selected = JSON.stringify(found[0]);
} else {
$scope.selected = 'Not found';
}
}
$scope.updateTask = function (task_id) {
// search $scope.todos for the item to update
var indexOfTask;
for(var i=0;i<$scope.todos.length;i++) {
if($scope.todos[i].id===$scope.id)
indexOfTask = i;
$scope.todos[i] = todo;
$scope.todos.push();
$scope.text= '';
$scope.id= '';
}
// update the todo
};
}
index.html
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Todo App</title>
<header>Todo App</header>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="//use.edgefonts.net/vast-shadow:n4:all.js"></script>
<script src="//use.edgefonts.net/vast-shadow:n4:all;megrim.js"></script>
</head>
<body>
<div class="todo-wrapper" ng-controller="TodoCtrl">
<h2>You have <span class="emphasis">{{getTotalTodos()}}</span> tasks</h2>
<ul>
<li ng-repeat="todo in todos">
<input type="checkbox" ng-model="todo.selected"/>
<span class="selected-{{todo.selected}}">{{todo.id}}: {{todo.text}} {{todo.date_created}}</span>
</li>
</ul>
<form>
<input class="add-input" placeholder="id" type="text" ng-model="id" ng-model-instant />
<input class="add-input2" placeholder="task name" type="text" ng-model="text" ng-model-instant />
<button class="add-btn" ng-click="addTask()"><h2>Add</h2></button>
<button class="search-btn" ng-click="showDetails(task_id)"><h2>Search</h2></button>
</form>
<button class="update-btn" ng-click="updateTask(task)"><h3>Update Task</h3></button>
<button class="clear-btn" ng-click="removeTask()">Remove Task</button>
</div>
</body>
</html>
<script src='http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js'></script>
<script src="js/index.js"></script>
</body>
</html>