Код: Выделить всё
ID Name Time Date
1 COR 01:40:37 01/24/24
2 BING 01:39:38 01/23/24
Сначала получив номер строки и отсортировав ее по времени:
Код: Выделить всё
ID Name Time Date Rn
2 BING 01:39:38 01/23/24 1
1 COR 01:40:37 01/24/24 2
Код: Выделить всё
ID Name Time Date Rn
2 BING 01:39:38 01/23/24 1
Код: Выделить всё
async searchByName(name) {
try{
const response = await new Promise((resolve, reject) => {
const query = "SELECT *, ROW_NUMBER() OVER (ORDER BY time) rn FROM medley_relay; SELECT * FROM medley_relay WHERE name = ?;";
// try separating the two queries
connection.query(query, [name], (err, results) => {
if (err) reject(new Error(err.message));
resolve(results);
})
});
console.log(response)
return response;
} catch (error) {
console.log(error);
}
}
Код: Выделить всё
app.get('/search/:name', (request, response) => {
const {name} = request.params;
const db = dbService.getDbServiceInstance();
const result = db.searchByName(name);
result
.then(data => response.json({data : data}))
.catch(err => console.log(err));
console.log(result);
})
Код: Выделить всё
const searchBtn = document.querySelector('#search-btn');
searchBtn.onclick = function() {
const searchValue = document.querySelector('#search-input').value;
fetch('http://localhost:5000/search/' + searchValue)
.then(response => response.json())
.then(data => loadHTMLTable(data['data']));
}
Код: Выделить всё
function loadHTMLTable(data) {
const table = document.querySelector('table tbody');
console.log(data);
if (data.length === 0) {
table.innerHTML = "No DataNo Data";
return;
}
let tableHtml = "";
data.forEach(function ({id, name, time, date, rn}) {
tableHtml += "";
tableHtml += `${rn}`;
tableHtml += `${name}`;
tableHtml += `${time}`;
tableHtml += `${new Date(date).toLocaleDateString()}`;
tableHtml += `Delete`;
tableHtml += `Edit`;
tableHtml += "";
});
table.innerHTML = tableHtml;
}
Код: Выделить всё
const query = "SELECT * FROM medley_relay WHERE name = ?;";
Код: Выделить всё
[
[
RowDataPacket {
id: 15,
name: 'Corning',
time: '01:40:15',
date: 2024-04-10T04:00:00.000Z,
rn: 1
},
RowDataPacket {
id: 16,
name: 'Binghamton',
time: '01:52:42',
date: 2024-04-02T04:00:00.000Z,
rn: 2
}
],
[
RowDataPacket {
id: 16,
name: 'Binghamton',
time: '01:52:42',
date: 2024-04-02T04:00:00.000Z
}
]
]
Код: Выделить всё
app.get('/getAll', (request, response) => {
const db = dbService.getDbServiceInstance();
const result = db.getAllData();
result
.then(data => response.json({data : data}))
.catch(err => console.log(err));
})
Код: Выделить всё
async getAllData() {
try{
const response = await new Promise((resolve,reject) => {
const query = "SELECT *, ROW_NUMBER() OVER (ORDER BY time) rn FROM medley_relay ORDER BY rn;"
"SELECT * FROM medley_relay;";
connection.query(query, (err,results) => {
if (err) reject(new Error(err.message));
resolve(results);
})
});
return response;
} catch (error) {
console.log(error);
}
}
Подробнее здесь: https://stackoverflow.com/questions/783 ... -that-also