-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.js
71 lines (61 loc) · 1.64 KB
/
1.js
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
var options = {
valueNames: [ 'id', 'name', 'age', 'city' ]
};
// Init list
var contactList = new List('contacts', options);
var idField = $('#id-field'),
nameField = $('#name-field'),
ageField = $('#age-field'),
cityField = $('#city-field'),
addBtn = $('#add-btn'),
editBtn = $('#edit-btn').hide(),
removeBtns = $('.remove-item-btn'),
editBtns = $('.edit-item-btn');
// Sets callbacks to the buttons in the list
refreshCallbacks();
addBtn.click(function() {
contactList.add({
id: Math.floor(Math.random()*110000),
name: nameField.val(),
age: ageField.val(),
city: cityField.val()
});
clearFields();
refreshCallbacks();
});
editBtn.click(function() {
var item = contactList.get('id', idField.val())[0];
item.values({
id:idField.val(),
name: nameField.val(),
age: ageField.val(),
city: cityField.val()
});
clearFields();
editBtn.hide();
addBtn.show();
});
function refreshCallbacks() {
// Needed to add new buttons to jQuery-extended object
removeBtns = $(removeBtns.selector);
editBtns = $(editBtns.selector);
removeBtns.click(function() {
var itemId = $(this).closest('tr').find('.id').text();
contactList.remove('id', itemId);
});
editBtns.click(function() {
var itemId = $(this).closest('tr').find('.id').text();
var itemValues = contactList.get('id', itemId)[0].values();
idField.val(itemValues.id);
nameField.val(itemValues.name);
ageField.val(itemValues.age);
cityField.val(itemValues.city);
editBtn.show();
addBtn.hide();
});
}
function clearFields() {
nameField.val('');
ageField.val('');
cityField.val('');
}