-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlooping.html
More file actions
68 lines (61 loc) · 1.7 KB
/
Copy pathlooping.html
File metadata and controls
68 lines (61 loc) · 1.7 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Alpine JS</title>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
</head>
<body>
<div x-data="{
numbers: [1,2,3,4,5]
}">
<template x-for="number in numbers">
<span x-text="number"></span>
</template>
</div>
<hr>
<!--With key index-->
<div x-data="{
fruits: ['oranges', 'bananas', 'apple' , 'mangoes' , 'grapes']}">
<template x-for="fruit, index in fruits">
<div>
<span x-text="index+1"></span>
<span x-text="fruit"></span>
</div>
</template>
</div>
<hr>
<div x-data="{
people: [{id: 1, name: 'Alex'}, {id:2, name: 'Bob'} ,{id: 3, name:'John'} ,{id: 4, name: 'Jane'}]}">
<template x-for="person in people" :key="person.id">
<div>
<span x-text="person.name"></span>
</div>
</template>
</div>
<hr>
<div x-data="{
people: [
{id: 1, name: 'Alex', points: 32},
{id:2, name: 'Bob', points: 25},
{id: 3, name:'John', points: 44},
{id: 4, name: 'Jane', points: 20}
],
get peopleSortedByPoint() {
return this.people.sort((a,b) => b.points - a.points)
},
increasePoints(id) {
this.people.find((p) => p.id === id).points++
}
}">
<template x-for="person in peopleSortedByPoint" :key="person.id">
<div>
<span x-text="person.name"></span> (<span x-text="person.points"></span>)
<button @click="increasePoints(person.id)">Increase Points</button>
</div>
</template>
</div>
</body>
</html>