-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
76 lines (72 loc) · 2.44 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
<style>
.delete:hover {
cursor: pointer;
}
.completed {
text-decoration: line-through;
}
</style>
</head>
</head>
<body>
<!-- x-data is where we decalre the componets state using objects -->
<div x-data="{todos:[], userInput: ''}">
<form
x-on:submit.prevent
>
<!-- x-model allows you to bind the value of an iput element to alpine data (userInput) -->
<input
x-model="userInput"
placeholder="add a todo item"
type="text"
>
<button
@click="
if(userInput.trim().length)
todos.push({name: userInput, completed: false });
userInput=''
"
>
Add
</button>
</form>
<!-- x-text binds the state, userInput and displays in the broswer as a paragraph -->
<p x-text="userInput"></p>
<!-- For loop -->
<template x-for="todo in todos">
<div>
<input
type="checkbox"
x-model="todo.completed"
>
<!-- xbind class can also remove e-binf and use :class{'name of class': condition to check against} -->
<span
x-text="todo.name";
x-bind:class="{'completed':todo.completed}"
>
</span>
<button
@click="todos = todos.filter(currentTodo => todo !== currentTodo)" class="delete">X</button>
</div>
</template>
<span x-text="todos.filter((todo) => todo.completed).length"></span>
<span>/</span>
<span x-text="todos.length"></span>
<span>Todos Completed</span>
<!-- x-show render an element based on a condition -->
<button
@click="todos=[]"
x-show="todos.length > 1"
>
Delete All
</button>
</div>
</body>
</html>