1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| <!DOCTYPE HTML>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>IndexedDB</title>
<script src="http://code.jquery.com/jquery-3.1.1.js"></script>
<style>
body {
margin: auto;
max-width: 60em;
font: 1em/1.25 Verdana;
}
select {
min-width: 10em;
}
select,
button {
min-width: 10em;
font: inherit;
}
li {
font-size: .8em;
}
ul {
display: block;
position: relative;
margin: 1em 0;
max-width: 40em;
list-style: none;
}
li:empty {
height: 0;
margin: 1px 0;
padding: 0;
border-top: 1px solid #ABC;
}
li em {
display: inline-block;
min-width: 10em;
}
</style>
</head>
<body>
<p id="myNote"></p>
<div >
<select id="selection"><option>Choix</option></select>
<button id="read-all">Read All</button>
</div>
<ul id="result"></ul>
<script>
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
if (!window.indexedDB) {
$('#myNote').text('Votre navigateur n\'est pas compatible avec l\'API IndexedDB.');
}
else {
$('#myNote').text('Votre navigateur est compatible avec l\'API IndexedDB.');
}// Préparation des données à stocker dans la base de données
const inscrits = [
{
id: '0',
nameChapter: 'Chapter 1 : hdfhdhfd',
nameTitle: 'Title 1',
Chapter: 'bcbcbbccb'
},
{
id: '1',
nameChapter: 'Chapter 1 : hdfhdhfd',
nameTitle: 'Title 2',
Chapter: 'dfdghdfhdfhb'
},
{
id: '2',
nameChapter: 'Chapter 1 : hdfhdhfd',
nameTitle: 'Title 3',
Chapter: 'jhdtjhdhd'
},
{
id: '3',
nameChapter: 'Chapter 1 : hdfhdhfd',
nameTitle: 'Title 4',
Chapter: 'kgfjrsf'
},
{
id: '4',
nameChapter: 'Chapter 1 : hdfhdhfd',
nameTitle: 'Title 5',
Chapter: 'qfgfghhjhd'
},
{
id: '5',
nameChapter: 'Chapter 1 : hdfhdhfd',
nameTitle: 'Title 6',
Chapter: 'ghjfdfdfh'
},
{
id: '6',
nameChapter: 'Chapter 1 : hdfhdhfd',
nameTitle: 'Title 7',
Chapter: 'dfghdfghdg'
},
{
id: '7',
nameChapter: 'Chapter 2 : kfjgaa',
nameTitle: 'Title 1',
Chapter: 'fgsgbsv'
},
{
id: '8',
nameChapter: 'Chapter 2 : oyhxguyrte',
nameTitle: 'Title 2',
Chapter: 'ghshgsghs'
},
{
id: '9',
nameChapter: 'Chapter 2 : oyhxguyrte',
nameTitle: 'Title 3',
Chapter: 'jgdhdhf'
},
{
id: '10',
nameChapter: 'Chapter 2 : oyhxguyrte',
nameTitle: 'Title 4',
Chapter: 'fgdhdghddfdf'
},
];
// init <select>
var oSelect = $('#selection');
inscrits.forEach(function (item, data) {
oSelect.append('<option value="' + item.id + '">' + item.nameTitle);
});
// Ouverture de la base de données
var db;
var request = window.indexedDB.open('test', 1);
request.onerror = function (evt) {
$('#myNote').text('Erreur : ' + evt.target.errorCode);
};
request.onsuccess = function (event) {
db = event.target.result;
$('#myNote').text('Base de données ouverte avec succès.');
};
request.onupgradeneeded = function (event) {
// Création du store
var store = event.target.result;
var objectStore = store.createObjectStore('inscrits', {
keyPath: 'id'
});
for (var i in inscrits) {
objectStore.add(inscrits[i]);
}
$('#myNote').text('Store créé.');
}
function readOne(ind) {
$('#result').empty();
var myId = ind;
var resultat = '';
var transaction = db.transaction(['inscrits']);
var objectStore = transaction.objectStore('inscrits');
var req = objectStore.get(myId);
req.onerror = function (event) {
console.log('Erreur');
};
req.onsuccess = function (event) {
var oSrc = this.result;//event.target.result;
var html = [];
if (oSrc) {
Object.keys(oSrc).forEach(function (prop) {
html.push('<li><em>' + prop + '</em> : ' + oSrc[prop] + '</li>');
});
// affichage résultat
$('#result').html(html.join(''));
}
};
}
function readAll() {
$('#result').empty();
var transaction = db.transaction(['inscrits']);
var objectStore = transaction.objectStore('inscrits');
var req = objectStore.openCursor();
var html = [];
req.onsuccess = function (event) {
var oCursor = this.result;//event.target.result;
if (oCursor) {
html.push('<li></li>');
Object.keys(oCursor.value).forEach(function (prop) {
html.push('<li><em>' + prop + '</em> : ' + oCursor.value[prop] + '</li>');
});
oCursor.continue ();
}
else {
// affichage résultat
html.push('<li></li>');
$('#result').html(html.join(''));
};
};
};
$('#read-all').on('click', readAll);
$('#read-one').on('click', readOne);
$('#selection').on('change', function () {
readOne(this.value);
});
</script>
</body>
</html> |
Partager