-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_addedit.js
161 lines (145 loc) · 5.04 KB
/
script_addedit.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
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
// Fetch the data from the server and populate the table
fetch('get_data.php')
.then(response => response.json())
.then(data => {
const tableBody = document.querySelector('#data-table tbody');
data.forEach((row, index) => {
const newRow = document.createElement('tr');
newRow.setAttribute('data-id', index + 1); // Add the data-id attribute
newRow.innerHTML = `
<td>${row.application}</td>
<td>${row.version}</td>
<td>${row.run}</td>
<td>
<button class="edit-button" data-id="${index + 1}">Edit</button>
</td>
<td> <!-- Add the delete button column -->
<button class="delete-button" data-id="${index + 1}">Delete</button>
</td>
`;
tableBody.appendChild(newRow);
});
// Add event listeners to the edit buttons
const editButtons = document.querySelectorAll('.edit-button');
editButtons.forEach(button => {
button.addEventListener('click', function() {
const rowId = this.dataset.id;
editRow(rowId);
});
});
// Add event listeners to the delete buttons
const deleteButtons = document.querySelectorAll('.delete-button');
deleteButtons.forEach(button => {
button.addEventListener('click', function() {
const rowId = this.dataset.id;
deleteRow(rowId);
});
});
});
// Function to add a new row to the table
function addRow() {
const applicationInput = document.querySelector('#application-input');
const versionInput = document.querySelector('#version-input');
const runInput = document.querySelector('#run-input');
const newRow = document.createElement('tr');
newRow.innerHTML = `
<td>${applicationInput.value}</td>
<td>${versionInput.value}</td>
<td>${runInput.value}</td>
<td>
<button onclick="editRow(null)">Edit</button>
</td>
`;
// Send the new data to the server for insertion
fetch('add_data.php', {
method: 'POST',
body: JSON.stringify({
application: applicationInput.value,
version: versionInput.value,
run: runInput.value
})
})
.then(response => response.json())
.then(data => {
newRow.querySelector('button').setAttribute('onclick', `editRow(${data.id})`);
});
// Clear the input fields
applicationInput.value = '';
versionInput.value = '';
runInput.value = '';
// Add the new row to the table
const tableBody = document.querySelector('#data-table tbody');
tableBody.appendChild(newRow);
}
// Function to edit a row
function editRow(rowId) {
console.log('Row ID:', rowId);
// Get the row element to be edited
const row = document.querySelector(`#data-table tbody tr[data-id="${rowId}"]`);
// Check if the row exists
if (row) {
// Get the cells within the row
const applicationCell = row.querySelector('td:nth-child(1)');
const versionCell = row.querySelector('td:nth-child(2)');
const runCell = row.querySelector('td:nth-child(3)');
// Get the current values in the cells
const currentApplication = applicationCell.textContent;
const currentVersion = versionCell.textContent;
const currentRun = runCell.textContent;
// Prompt the user to enter the updated values
const updatedApplication = prompt('Enter updated application:', currentApplication);
const updatedVersion = prompt('Enter updated version:', currentVersion);
const updatedRun = prompt('Enter updated run:', currentRun);
// Update the cell values with the updated values
applicationCell.textContent = updatedApplication;
versionCell.textContent = updatedVersion;
runCell.textContent = updatedRun;
// Send the updated data to the server for saving
fetch('update_data.php', {
method: 'POST',
body: JSON.stringify({
id: rowId,
application: updatedApplication,
version: updatedVersion,
run: updatedRun
})
})
.then(response => response.json())
.then(data => {
// You can perform any additional actions after the data is updated
console.log('Row updated successfully:', data);
});
} else {
console.error('Row not found');
}
}
// Function to delete a row
function deleteRow(rowId) {
const requestPayload = { id: rowId };
fetch('delete_row.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestPayload),
})
.then(response => {
if (response.ok) {
return response.json();
}
throw new Error('Error deleting row: ' + response.statusText);
})
.then(data => {
if (data.success) {
// Row was deleted successfully
const row = document.querySelector(`tr[data-id="${rowId}"]`);
row.remove();
} else {
// Row was not found or there was an error
console.error('Error deleting row:', data.error);
}
})
.catch(error => {
console.error('Error deleting row:', error);
});
}