這是非常簡單的工作。不知道你需要一個庫,除了一旦你找到你的結果進行DOM操作。
function searchArrayFields(search, objectArray)
{
var results = [];
for (var i=0; i < objectArray.length; i++)
{
for (var key in objectArray[i])
{
if (objectArray[i][key].indexOf(search) > -1)
{
results.push(objectArray[i]);
break;
}
}
}
return results;
}
下面是一些例子DOM的東西太多。
function buildAddress(row)
{
var retVal = '<div class="container"><div class="name">'+row.name + '</div>';
retVal += '<div class="address-line-1">'+ row.address+'</div>';
retVal += '<div class="address-line-2"><span class="city">'+ row.city+'</span>';
retVal += ' <span class="state">'+ row.state+'</span>';
retVal += ', <span class="zip">'+row.zip+'</span></div></div>';
return retVal;
}
function displayAddresses(search)
{
var rows = searchArrayFields(search, global_db);
var retVal = [];
for (var i=0; i < rows.length; i++)
{
retVal.push(buildAddresses(rows[i]));
}
$('#target').html(retVal.join(''));
}
假設你的數據都存儲這樣,這將是一個標準的JSON設置:
[{name:'John Doe', address:'111 Main Street', city:'Toledo', state:'OH',zip:44343}];