<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=380">
<title>Basic IDBKeyRange example — favourite things</title>
</head>
<body>
<h1>Basic IDBKeyRange example<br>These are a few of my favourite things</h1>
<ul></ul>
<form>
<div>
<div>
<label for="none">Don't filter values at all</label>
<input type="radio" name="value" value="none" id="none" checked>
</div>
</div>
<div>
<div>
<label for="only">Return <strong>only</strong> one value</label>
<input type="radio" name="value" value="only" id="only">
</div>
<div>
<label for="onlytext">Key value for single value selection</label>
<input type="text" name="onlytext" id="onlytext">
</div>
</div>
<div>
<div>
<label for="range">Return a <strong>range</strong> of values</label>
<input type="radio" name="value" value="range" id="range">
</div>
<div>
<label for="rangelowertext">Lower limit for range</label>
<input type="text" name="rangelowertext" id="rangelowertext">
</div>
<div>
<label for="rangeuppertext">Upper limit for range</label>
<input type="text" name="rangeuppertext" id="rangeuppertext">
</div>
</div>
<div>
<div>
<label for="lower">Return values with a <strong>lower</strong> bound</label>
<input type="radio" name="value" value="lower" id="lower">
</div>
<div>
<label for="lowerboundtext">Lower bound limit for results</label>
<input type="text" name="lowerboundtext" id="lowerboundtext">
</div>
</div>
<div>
<div>
<label for="upper">Return values with an <strong>upper</strong> bound</label>
<input type="radio" name="value" value="upper" id="upper">
</div>
<div>
<label for="upperboundtext">Upper bound limit for results</label>
<input type="text" name="upperboundtext" id="upperboundtext">
</div>
</div>
<button class="run">Run query</button>
</form>
</body>
</html>
/* || basic set up + sizing for containers */
html, body {
margin: 0;
font-family: sans-serif;
font-size: 10px;
}
html {
background-color: #55aa55;
}
body {
max-width: 800px;
margin: 0 auto;
padding: 30px 0;
}
h1 {
text-align: center;
font-size: 4rem;
color: white;
text-shadow: -1px -1px 2px black;
}
ul {
text-align: center;
list-style-type: none;
padding: 2rem 1rem;
background: white linear-gradient(to bottom, rgba(0,0,0,0), rgba(0,0,0,0.2));
box-shadow: 3px 3px 10px black;
}
li {
font-size: 2rem;
padding-bottom: 1rem;
}
form {
float: right;
width: 55%;
}
ul {
float: left;
width: 40%;
}
form {
font-size: 1.7rem;
margin-top: 1rem;
}
form > div {
margin-bottom: 3rem;
padding: 1rem 1rem 2rem;
background: white linear-gradient(to bottom, rgba(0,0,0,0), rgba(0,0,0,0.2));
box-shadow: 3px 3px 10px black;
}
form div div {
clear: both;
margin-top: 1rem;
}
form div label {
width: 55%;
clear: both;
}
form div input {
float: right;
}
form div input[type="text"] {
width: 30%;
}
form div input[type="radio"] {
margin-right: 10px;
}
button {
display: block;
width: 30%;
margin: 0 auto;
font-size: 1.7rem;
line-height: 1.5;
box-shadow: 1px 1px 2px black;
}
// create an instance of a db object for us to store the IDB data in
var db;
var things = [
{ fThing: 'Drum kit', fRating: 10 },
{ fThing: 'Family', fRating: 10 },
{ fThing: 'Batman', fRating: 9 },
{ fThing: 'Brass eye', fRating: 9 },
{ fThing: 'The web', fRating: 9 },
{ fThing: 'Mozilla', fRating: 9 },
{ fThing: 'Firefox OS', fRating: 9 },
{ fThing: 'Curry', fRating: 9 },
{ fThing: 'Paneer cheese', fRating: 8 },
{ fThing: 'Mexican food', fRating: 8 },
{ fThing: 'Chocolate', fRating: 7 },
{ fThing: 'Heavy metal', fRating: 10 },
{ fThing: 'Monty Python', fRating: 8 },
{ fThing: 'Aphex Twin', fRating: 8 },
{ fThing: 'Gaming', fRating: 7 },
{ fThing: 'Frank Zappa', fRating: 9 },
{ fThing: 'Open minds', fRating: 10 },
{ fThing: 'Hugs', fRating: 9 },
{ fThing: 'Ale', fRating: 9 },
{ fThing: 'Christmas', fRating: 8 },
];
// all the variables we need for the app
var list = document.querySelector('ul');
var button = document.querySelector('button');
var onlyText = document.querySelector('#onlytext');
var rangeLowerText = document.querySelector('#rangelowertext');
var rangeUpperText = document.querySelector('#rangeuppertext');
var lowerBoundText = document.querySelector('#lowerboundtext');
var upperBoundText = document.querySelector('#upperboundtext');
window.onload = function() {
// In the following line, you should include the prefixes of implementations you want to test.
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
// DON'T use "var indexedDB = ..." if you're not in a function.
// Moreover, you may need references to some window.IDB* objects:
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
// (Mozilla has never prefixed these objects, so we don't need window.mozIDB*)
var DBOpenRequest = window.indexedDB.open('fThings', 1);
DBOpenRequest.onsuccess = function(event) {
db = DBOpenRequest.result;
populateData();
};
DBOpenRequest.onupgradeneeded = function(event) {
var db = event.target.result;
db.onerror = function(event) {
note.innerHTML += '<li>Error loading database.</li>';
};
var objectStore = db.createObjectStore('fThings', { keyPath: 'fThing' });
objectStore.createIndex('fRating', 'fRating', { unique: false });
};
function populateData() {
var transaction = db.transaction(['fThings'], 'readwrite');
var objectStore = transaction.objectStore('fThings');
for(i = 0; i < things.length ; i++) {
var request = objectStore.put(things[i]);
}
transaction.oncomplete = function() {
displayData();
};
}
var keyRangeValue = null;
function displayData() {
checkedValue = document.querySelector('input[name="value"]:checked').value;
if(checkedValue == "none") {
keyRangeValue = null;
} else if(checkedValue == "only") {
keyRangeValue = IDBKeyRange.only(onlyText.value);
} else if(checkedValue == "range") {
keyRangeValue = IDBKeyRange.bound(rangeLowerText.value, rangeUpperText.value, false, false);
} else if(checkedValue == "lower") {
keyRangeValue = IDBKeyRange.lowerBound(lowerBoundText.value);
} else if(checkedValue == "upper") {
keyRangeValue = IDBKeyRange.upperBound(upperBoundText.value);
}
if(keyRangeValue != null) {
console.log(keyRangeValue.lower);
console.log(keyRangeValue.upper);
console.log(keyRangeValue.lowerOpen);
console.log(keyRangeValue.upperOpen);
}
list.innerHTML = '';
var transaction = db.transaction(['fThings'], 'readonly');
var objectStore = transaction.objectStore('fThings');
var countRequest = objectStore.count();
countRequest.onsuccess = function() {
console.log(countRequest.result);
};
objectStore.openCursor(keyRangeValue).onsuccess = function(event) {
var cursor = event.target.result;
if(cursor) {
var listItem = document.createElement('li');
listItem.innerHTML = '<strong>' + cursor.value.fThing + '</strong>, ' + cursor.value.fRating;
list.appendChild(listItem);
cursor.continue();
} else {
console.log('Entries all displayed.');
}
};
}
button.onclick = function(e) {
e.preventDefault();
displayData();
};
};
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |