-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchList.js
52 lines (44 loc) · 1.57 KB
/
matchList.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
const search = document.getElementById('searchTicker');
const matchList = document.getElementById('match-list');
//updated
var allTickers = "https://timelessmarket-api.onrender.com/ticker";
//search heroku ticker site and filter it
const searchTickers = async searchText => {
const res = await fetch(allTickers);
const tickers = await res.json();
console.log(tickers);
//Get matches to current text input
let matches = tickers.filter(ticker => {
const regex = new RegExp(`^${searchText}`,'gi');
return ticker.Symbol.match(regex) || ticker.SecurityName.match(regex);
});
if(searchText.length === 0){
document.getElementById("match-list").style.display = "none";
matches = [];
matchList.innerHTML = '';
}
outputHtml(matches);
console.log(matches);
};
//show results in HTML
const outputHtml = matches => {
if(matches.length > 0){
matches = matches.slice(0,25); //reduce the max number of searches shown
document.getElementById("match-list").style.display = "inline";
var html = matches.map(match => `
<tr value="${match.Symbol}" id="${match.Symbol}" onclick="dropdownSearch(this)">
<td><b>${match.Symbol}</b></td>
<td>${match.SecurityName}</td>
</tr>
`
).join('');
html = '<table>' + html + '</table>';
matchList.innerHTML = html;
}
}
search.addEventListener('input',() => searchTickers(search.value));
function dropdownSearch(elem){
console.log(elem.id);
document.getElementById("searchTicker").value = elem.id;
document.getElementById("searchForm").submit();
}