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
|
---
title: IDBObjectStore.openKeyCursor()
slug: Web/API/IDBObjectStore/openKeyCursor
tags:
- API
- IDBObjectStore
- IndexedDB
- Méthode
- Reference
translation_of: Web/API/IDBObjectStore/openKeyCursor
---
<div>{{APIRef("IndexedDB")}}</div>
<p>La méthode <strong><code>openKeyCursor()</code></strong>, rattachée à l'interface {{domxref("IDBObjectStore")}}, renvoie un objet {{domxref("IDBRequest")}} dont le résultat sera un curseur ({{domxref("IDBCursor")}}) qui pourra être utilisé afin de parcourir les enregistrements correspondants. Cette méthode est utilisée afin de parcourir les clés d'un magasin d'objets grâce à un curseur.</p>
<p>Afin de déterminer si le curseur a bien été ajouté, on pourra écouter l'évènement <code>success</code> sur le résultat de la méthode.</p>
<h2 id="Syntaxe">Syntaxe</h2>
<pre class="syntaxbox">var <em>requete</em> = <em>objectStore</em>.openKeyCursor(<em>optionalKeyRange</em>, <em>optionalDirection</em>);</pre>
<h3 id="Paramètres">Paramètres</h3>
<dl>
<dt><code>optionalKeyRange</code></dt>
<dd>L'intervalle de clés qu'on souhaite parcourir. Si une seule clé est fournie, l'intervalle constitué ne contiendra que cette clé. Si aucune valeur n'est passée en argument, le curseur produit parcourera tous les enregistrements du magasin d'objets.</dd>
<dt><code>optionalDirection</code></dt>
<dd>Un objet {{domxref("IDBCursorDirection")}} qui indique la direction dans laquelle le curseur doit parcourir les évènements. La valeur par défaut est <code>"next"</code>.</dd>
</dl>
<h3 id="Valeur_de_retour">Valeur de retour</h3>
<p>Un objet {{domxref("IDBRequest")}} sur lequel seront déclenchés les différents évènements relatifs à cette opération.</p>
<h3 id="Exceptions">Exceptions</h3>
<p>Cette méthode peut déclencher une exception {{domxref("DOMException")}} ayant l'un des types suivants :</p>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Exception</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>InvalidStateError</code></td>
<td>Le magasin d'objets ({{domxref("IDBObjectStore")}} ou l'index ({{domxref("IDBIndex")}}) a été supprimé.</td>
</tr>
<tr>
<td><code>TransactionInactiveError</code></td>
<td>La transaction associée au magasin d'objet ({{domxref("IDBObjectStore")}}) est inactive.</td>
</tr>
<tr>
<td><code>DataError</code></td>
<td>La clé ou l'intervalle de clés indiqué est invalide.</td>
</tr>
</tbody>
</table>
<h2 id="Exemples">Exemples</h2>
<p>Dans le fragment de code suivant, on crée une transaction, on l'utilise sur un magasin d'objets donné puis on utilise le curseur pour parcourir tous les enregistrements du magasin d'objets :</p>
<pre class="brush: js">var transaction = db.transaction("name", "readonly");
var objectStore = transaction.objectStore("name");
var request = objectStore.openKeyCursor();
request.onsuccess = function(event) {
var cursor = event.target.result;
if(cursor) {
// cursor.key contient la clé de l'enregistrement courant
// à la différence de openCursor, il n'y a pas de cursor.value
cursor.continue();
} else {
// Il n'y a plus d'autres résultats
}
};</pre>
<h2 id="Spécifications">Spécifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spécification</th>
<th scope="col">État</th>
<th scope="col">Commentaires</th>
</tr>
<tr>
<td>{{SpecName('IndexedDB2', '#dom-idbobjectstore-openkeycursor', 'openKeyCursor')}}</td>
<td>{{Spec2('IndexedDB')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Compatibilité_des_navigateurs">Compatibilité des navigateurs</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Fonctionnalité</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari (WebKit)</th>
</tr>
<tr>
<td>Support simple</td>
<td>{{CompatChrome(48.0)}}</td>
<td>{{CompatGeckoDesktop("44.0")}} [1]</td>
<td>10, partial</td>
<td>15</td>
<td>7.1</td>
</tr>
<tr>
<td>Disponible dans les <em>web workers</em></td>
<td>{{CompatChrome(48.0)}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatOpera(35)}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Fonctionnalité</th>
<th>Android</th>
<th>Webview Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Phone</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
<th>Chrome pour Android</th>
</tr>
<tr>
<td>Support simple</td>
<td>4.4</td>
<td>{{CompatChrome(48.0)}}</td>
<td>{{CompatGeckoMobile("44.0")}} [1]</td>
<td>10</td>
<td>22</td>
<td>{{CompatNo}}</td>
<td>{{CompatChrome(48.0)}}</td>
</tr>
<tr>
<td>Disponible dans les <em>web workers</em></td>
<td>{{CompatNo}}</td>
<td>{{CompatChrome(48.0)}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatOperaMobile(35)}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatChrome(48.0)}}</td>
</tr>
</tbody>
</table>
</div>
<p>[1] Cette fonctionnalité était disponible depuis Firefox 27 via une préférence : <code>dom.indexedDB.experimental</code> qui était désactivée par défaut.</p>
<h2 id="Voir_aussi">Voir aussi</h2>
<ul>
<li><a href="/fr/docs/Web/API/API_IndexedDB/Using_IndexedDB">Utiliser IndexedDB</a></li>
<li>Initier une connexion : {{domxref("IDBDatabase")}}</li>
<li>Utiliser les transactions : {{domxref("IDBTransaction")}}</li>
<li>Définir un intervalle de clés : {{domxref("IDBKeyRange")}}</li>
<li>Récupérer et modifier les données : {{domxref("IDBObjectStore")}}</li>
<li>Utiliser les curseurs {{domxref("IDBCursor")}}</li>
<li>Exemple de référence : <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="https://mdn.github.io/to-do-notifications/">exemple <em>live</em></a>).</li>
</ul>
|