-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.js
More file actions
166 lines (134 loc) · 3.59 KB
/
context.js
File metadata and controls
166 lines (134 loc) · 3.59 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class Context {
constructor() {
this.dataBase = [{
id: 1,
name: 'Vasya',
surname: "Ivanov",
numb: `(099)33-78-130`
},
{
id: 2,
name: 'Dima',
surname: "Petrov",
numb: `(099)33-78-130`
},
{
id: 3,
name: 'Grisha',
surname: "Adianow",
numb: `(099)33-78-130`
},
{
id: 4,
name: 'Jora',
surname: "Shewshenko",
numb: `(099)33-78-130`
},
{
id: 5,
name: 'Jora',
surname: "Ivanov",
numb: `(099)33-78-130`
},
{
id: 6,
name: 'Dima',
surname: "Dimonov",
numb: `(099)33-78-130`
}
]
}
searchUsers(username) {
let allUsers = [];
this.dataBase.forEach((value, index, arr)=> {
if (value.name == username) {
allUsers.push(this.dataBase[index]);
}
})
this.dataBase = allUsers
this.buildHtmlTags()
return this.dataBase
}
createTh(arr){
const innerTr = arr.map(value =>
`<td OnClick = "myContext.sortUsers('${value}')">${value}</td>`
).join('');
return `<thead><tr>
${innerTr}
</tr></thead>`
}
buildHtmlTags (){
let main = document.getElementById('app')
main.innerHTML = `<header class="header">
<div class="container top-radius">
<h2>Contacts</h2>
</div>
</header>
<main>
<div class="container">
<form class="form-inline search-form">
<div class="form-group">
<label class="sr-only" for="search">Search</label>
<input type="text" class="form-control" id= "search" placeholder="Search">
</div>
</form>
<table class="table table-hover contacts">
${this.createTh(['name','surname', 'number'])}
<tbody id="tbody_id">
</tbody>
</table>
</div>
</main>`
this.outputDataArray(this.dataBase)
let formToSearch = document.querySelector(".form-control")
window.addEventListener("keydown", e => {
if (e.keyCode == 13) {
this.searchUsers(`${formToSearch.value}`)
}
// console.log(`${formToSearch.value}`);
})
this.checkUser()
}
createElement (value, key, index){
let td = document.createElement('td');
let rtById = document.getElementById(`tr_${index}`)
rtById.appendChild(td)
td.textContent = value[key]
}
outputDataArray(options) {
let tabelBody = document.getElementById("tbody_id")
options.forEach((value, index, arr) => {
let tr = document.createElement('tr')
tr.setAttribute('id', `tr_${index}`);
tr.setAttribute('class', `tr_class`);
tabelBody.appendChild(tr);
this.createElement(value, 'name', index)
this.createElement(value, 'surname', index)
this.createElement(value, 'numb', index)
})
}
sortUsers(property){
var newArray = this.dataBase.sort((a, b) => {
return a[property] == b[property] ? 0 : a[property] < b[property] ? -1 : 1;
})
let tabelBody = document.getElementById("tbody_id")
tabelBody.innerHTML = "";
return this.outputDataArray(newArray)
}
checkUser(){
let allTr = document.querySelectorAll(".tr_class")
allTr.forEach((tr, index, arr)=>{
tr.addEventListener("click", a =>{
let allTd = tr.querySelectorAll("td")
let innerTdName = allTd[0].innerHTML
let innerTdSurname = allTd[1].innerHTML
let innerTdNumber = allTd[2].innerHTML
console.log(innerTdName);
console.log(innerTdSurname);
console.log(innerTdNumber);
})
})
}
}
let myContext = new Context();
myContext.buildHtmlTags()