-
Notifications
You must be signed in to change notification settings - Fork 0
/
books.html
95 lines (92 loc) · 3.11 KB
/
books.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Books</title>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"
crossorigin="anonymous"
>
</head>
<body>
<article class="container">
<h1>Good reads</h1>
<div class="text-right">
<form class="form-inline">
<div class="form-group">
<input type="text" class="form-control" id="friend" placeholder="Jane">
</div>
<button type="button" id="search" class="btn btn-default">Search</button>
</form>
</div>
<br>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>
<a id="sort" class="header" href="#">Friend</a>
</th>
<td class="header">Books</td>
</tr>
</thead>
<tbody id="friends">
</tbody>
</table>
</article>
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous">
</script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS"
crossorigin="anonymous">
</script>
<script>
var tbody = $('#friends');
var sortName = $('#sort');
var search = $('#search');
var friends = [
{name: 'Oliver', books: ['Ansible for DevOps', 'Servers for hackers']},
{name: 'Barry', books: ['Working effectively with unit tests', '50 quick ideas for your tests']},
{name: 'Jessica', books: ['Understanding the 4 rules of simple design', 'Principles of package design']},
{name: 'Clark', books: ['Selling test driven projects']}
];
var buildTableRows = function (friends) {
var table = '';
friends.forEach(function (friend) {
table += '<tr><td>' + friend.name + '</td><td>' + friend.books.join(', ') + '</td></tr>';
});
return table;
};
var sortFriendsByName = function(friends) {
friends.sort(function (friendA, friendB) {
if (friendA.name < friendB.name) {
return -1;
}
if (friendA.name > friendB.name) {
return 1;
}
return 0;
});
};
var filterFriendsByName = function (friends, name) {
return friends.filter(function (friend) {
return friend.name.indexOf(name) !== -1;
});
};
var sortTable = function (event) {
event.preventDefault();
sortFriendsByName(friends);
tbody.html(buildTableRows(friends));
};
tbody.html(buildTableRows(friends));
search.on('click', function(){tbody.html(buildTableRows(filterFriendsByName(friends, $('#friend').val())))});
search.on('click', function(){(console.log(filterFriendsByName(friends, $('#friend').val())))});
sortName.on('click', sortTable);
</script>
</body>
</html>