我從一個json對象加載數據到我的頁面上的一個表。然後我允許用戶通過輸入過濾數據並僅顯示匹配。我這樣做的方法肯定不是很好,但它確實有效。如何最好地顯示來自大型json對象的匹配?
現在我想做一個完全相同的事情與機場列表和他們的代碼。問題是機場列表要長得多,並且當用數據加載表格以及在表格中搜索用戶的輸入時,頁面顯着下降。
以下是可以工作的頁面的信息,因此您可以選擇我在做什麼。
當我有更大的數據集進行搜索時,我可以採取什麼不同的操作來達到相同的效果?
頁顯示的數據:(類型 「美國航空公司」 或 「AA」 作爲例子) https://pnrbuilder.com/_popups/dataDecoder.php
JSON對象包含航空公司信息: https://pnrbuilder.com/_java/airlineDecoder.js
Sript加載數據到頁面和過濾器它基於用戶輸入: https://pnrbuilder.com/_java/decodeData.js
這裏是最標誌意義我的代碼NT部分:
// This function is called by a for loop on dom ready
// It basically prints data stored in a json object to a table on the page
function fillInfo(line) {
var table = document.getElementById('decodeTable');
var row = document.createElement('tr');
table.appendChild(row);
var col1 = document.createElement('td');
row.appendChild(col1);
var curCode = document.createTextNode(arlnInfo.d[line].IATA);
col1.appendChild(curCode);
var col2 = document.createElement('td');
row.appendChild(col2);
var curArln = document.createTextNode(arlnInfo.d[line].Airline);
col2.appendChild(curArln);
var col3 = document.createElement('td');
row.appendChild(col3);
var curPre = document.createTextNode(arlnInfo.d[line].Prefix);
col3.appendChild(curPre);
var col4 = document.createElement('td');
row.appendChild(col4);
var curIcao = document.createTextNode(arlnInfo.d[line].ICAO);
col4.appendChild(curIcao);
var col5 = document.createElement('td');
row.appendChild(col5);
var curCnty = document.createTextNode(arlnInfo.d[line].Country);
col5.appendChild(curCnty);
}
// This function checks user input against data in the table
// If a match is found whitin a row, the row containing the match is shown
// If a match is not found that row is hidden
function filterTable(input) {
var decodeTable = document.getElementById('decodeTable');
var inputLength = input.length;
// THis first part makes sure that all rows of the generated table are hidden when no input is present
if (inputLength == 0) {
for (var r = 1; r < decodeTable.rows.length; r++) {
decodeTable.rows[r].style.display = "none";
}
}
// This part checks just the airline codes "column" of the table when input is only one or two characters
else if (inputLength < 3) {
for (var r = 1; r < decodeTable.rows.length; r++) {
var celVal = $(decodeTable.rows[r].cells[0])
.text()
.slice(0, inputLength)
.toLowerCase();
if (celVal == input) {
decodeTable.rows[r].style.display = "";
} else {
decodeTable.rows[r].style.display = "none";
}
}
}
// This part checks several "columns" of the table when input is more than two characters
else if (inputLength > 2) {
for (var r = 1; r < decodeTable.rows.length; r++) {
var celVal = $(decodeTable.rows[r].cells[2])
.text()
.slice(0, inputLength)
.toLowerCase();
var celVal2 = $(decodeTable.rows[r].cells[1])
.text();
if (celVal == input || celVal2 == input) {
decodeTable.rows[r].style.display = "";
} else if (celVal2.replace(/<[^>]+>/g, "")
.toLowerCase()
.indexOf(input) >= 0) {
decodeTable.rows[r].style.display = "";
} else {
decodeTable.rows[r].style.display = "none";
}
}
}
}
難道您不是檢查針對JSON數據的輸入嗎?併爲每個對象添加一個屬性,如isVisible:true/false。當所有設置,循環通過JSON數組和:if(!isVisible){... hide table row} – eosterberg
我確定我可以。這會更快嗎?輸入的每個鍵入事件都會調用「過濾器函數」。通過JSON對象分配/刪除isVisible循環,然後循環通過JSON數組再次加載可見的插槽到表中,這比我現在做的要少得多嗎?不要拒絕它,只是想明白。 – DelightedD0D