данные из json при написаний 3 символов должны выводится в input в консоли ошибок нету в чем может быть причина пути проверял
Код:
<!DOCTYPE html>
<html>
<head>
<title>Поиск элементов</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#search-input {
padding: 8px;
font-size: 16px;
width: 100%;
box-sizing: border-box;
}
#search-results {
list-style: none;
margin: 0;
padding: 0;
}
#search-results li {
padding: 8px;
font-size: 16px;
border-bottom: 1px solid #ccc;
}
#search-results li.category {
font-weight: bold;
background-color: #f0f0f0;
}
#item-details {
margin-top: 16px;
padding: 8px;
font-size: 16px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<input id="search-input" type="text" placeholder="Введите поисковый запрос...">
<ul id="search-results"></ul>
<div id="item-details"></div>
<script src="script.js"></script>
</body>
</html>
Код:
const searchInput = document.getElementById('search-input');
const searchResults = document.getElementById('search-results');
const itemDetails = document.getElementById('item-details');
const jsonFileUrl = 'data.json';
let data;
fetch(jsonFileUrl)
.then(response => response.json())
.then(jsonData => {
data = jsonData;
})
.catch(error => console.error(error));
function showSearchResults(results) {
searchResults.innerHTML = '';
for (let i = 0; i < results.length; i++) {
const li = document.createElement('li');
const item = results[i];
const isCategory = item.items && item.items.length > 0;
let suggestion = '';
if (item.code && item.name) {
suggestion = item.code + ': ' + item.name;
} else if (item.name) {
suggestion = item.name;
}
if (isCategory) {
li.classList.add('category');
}
li.textContent = suggestion;
searchResults.appendChild(li);
li.addEventListener('click', function() {
searchResults.innerHTML = '';
showItemDetails(item);
});
}
}
function searchItems(query) {
if (!data) {
return [];
}
const words = query.toLowerCase().split(' ');
const filtered = data.filter(function(item) {
const text = (item.code || '') + ' ' + (item.name || '');
return words.every(function(word) {
return text.toLowerCase().indexOf(word) >= 0;
});
});
const sorted = filtered.sort(function(a, b) {
const weightDiff = (b.weight || 0) - (a.weight || 0);
if (weightDiff !== 0) {
return weightDiff;
} else if (a.name && b.name) {
return a.name.localeCompare(b.name);
} else {
return 0;
}
});
const limit = 10;
const sliced = sorted.slice(0, limit);
return sliced;
}