Archived
1
0
This repository has been archived on 2021-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
tidx/example/advanced.html

112 lines
2.9 KiB
HTML
Raw Permalink Normal View History

<html>
<head>
<title>Tidx Test</title>
<style type="text/css">
#result_table td { vertical-align: top; }
td { vertical-align: bottom; }
th { text-align: left; }
#output { font-style: italic; }
</style>
</head>
<body>
<table>
<tr>
<td><label for="search">Multiterm search:</label></td>
<td><input type="text" id="search" name="search" size="50" /></td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td><label for="email">Email search:</label></td>
<td><input type="text" id="email" name="email" size="50" /></td>
<td><input type="button" id="btn_search" value="Search"/></td>
<td><div id="output"></div></td>
</tr>
</table>
<br/>
<table id="result_table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>City</th>
<th>State</th>
<th>Country</th>
<th>Rank</th>
<th>Description</th>
</tr>
</thead>
<tbody id="result_tbody"></tbody>
</table>
<script type="text/javascript" src="../tidx.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var idx = new Tidx();
var json = null;
$(function(){
var output = $('#output');
// Load test JSON
output.html('Loading .. ');
$.getJSON('test.json', function(data){
json = data;
var s = new Date().getTime();
for(var i = 0; i < json.length; i++){
for(var f in json[i]){
switch(f){
case 'id': break; // Don't index the ID
case 'email': idx.index(false, i, f, json[i][f]); break;
default: idx.index(true, i, f, json[i][f]); break;
}
}
}
output.html('test.json indexed in ' + (new Date().getTime() - s) + "ms\n");
});
// Setup the search button
$('#btn_search').click(function(){
var s = new Date().getTime();
var raw_results = {};
// Conduct the multiterm search
var term_count = idx.raw_search($('#search').val(), raw_results);
// Add in the field specific scan of 'email'
term_count += idx.field_scan('email', $('#email').val(), raw_results);
// Process the results
var results = idx.order(raw_results, term_count);
var rtbl = $('#result_tbody').empty();
for(var i in results){
var row = json[results[i]];
rtbl.append(
'<tr>' +
'<td>' + row['id'] + '</td>' +
'<td>' + row['name'] + '</td>' +
'<td>' + row['email'] + '</td>' +
'<td>' + row['city'] + '</td>' +
'<td>' + row['state'] + '</td>' +
'<td>' + row['country'] + '</td>' +
'<td>' + row['rank'] + '</td>' +
'<td>' + row['desc'] + '</td>' +
'</tr>'
);
}
});
// Enter should work to search, too
$('#search').keydown(function(e){
if(e.which == 13){ $('#btn_search').click(); }
}).focus();
});
</script>
</body>
</html>