aboutsummaryrefslogtreecommitdiff
path: root/files/fr/web/api/idbindex/getkey
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/fr/web/api/idbindex/getkey
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/fr/web/api/idbindex/getkey')
-rw-r--r--files/fr/web/api/idbindex/getkey/index.html214
1 files changed, 214 insertions, 0 deletions
diff --git a/files/fr/web/api/idbindex/getkey/index.html b/files/fr/web/api/idbindex/getkey/index.html
new file mode 100644
index 0000000000..610ca5d29c
--- /dev/null
+++ b/files/fr/web/api/idbindex/getkey/index.html
@@ -0,0 +1,214 @@
+---
+title: IDBIndex.getKey()
+slug: Web/API/IDBIndex/getKey
+tags:
+ - API
+ - IDBIndex
+ - IndexedDB
+ - Méthode
+ - Reference
+translation_of: Web/API/IDBIndex/getKey
+---
+<div>{{APIRef("IndexedDB")}}</div>
+
+<p>La méthode <strong><code>getKey()</code></strong>, rattachée à l'interface {{domxref("IDBIndex")}}, renvoie un objet {{domxref("IDBRequest")}} et, dans un <em>thread</em> séparé, récupère la clé associée ou la clé primaire si l'argument passé à la fonction est un intervalle {{domxref("IDBKeyRange")}}.</p>
+
+<p>Si la méthode trouve une clé, ce sera alors la propriété <code>result</code> de la requête renvoyée. Seule la clé primaire de l'enregistrement est renvoyée (pour obtenir l'ensemble de l'enregistrement, on utilisera {{domxref("IDBIndex.get")}}).</p>
+
+<p>{{AvailableInWorkers}}</p>
+
+<h2 id="Syntaxe">Syntaxe</h2>
+
+<pre class="brush: js">var myIndex = objectStore.index('index');
+var request = myIndex.getKey(key);</pre>
+
+<h3 id="Paramètres">Paramètres</h3>
+
+<dl>
+ <dt><code>key</code> {{optional_inline}}</dt>
+ <dd>Une clé ou un intervalle {{domxref("IDBKeyRange")}} qui identifie l'enregistrement dont on souhaite obtenir la clé. Si la valeur vaut {{jsxref("null")}} ou si elle est absente, le navigateur utilisera un intervalle de clé sans limite.</dd>
+</dl>
+
+<h3 id="Valeur_de_retour">Valeur de retour</h3>
+
+<p>Un objet {{domxref("IDBRequest")}} qui recevra les différents évènements relatifs à l'opération qui est déclenchée.</p>
+
+<h3 id="Exceptions">Exceptions</h3>
+
+<p>Cette méthode peut déclencher une exception. Celle-ci peut avoir 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>TransactionInactiveError</td>
+ <td>La transaction rattachée à cet <code>IDBIndex</code> est inactive.</td>
+ </tr>
+ <tr>
+ <td><code>DataError</code></td>
+ <td>
+ <p>La clé ou l'intervalle de clés qui est fourni contient une clé invalide.</p>
+ </td>
+ </tr>
+ <tr>
+ <td><code>InvalidStateError</code></td>
+ <td>L'index a été supprimé ou déplacé.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Exemples">Exemples</h2>
+
+<p>Dans l'exemple qui suit, on ouvre une transaction et un magasin d'objets. Ensuite on récupère l'index <code>lName</code> sur cette base de donnée. On ouvre alors un curseur sur l'index grâce à la méthode {{domxref("IDBIndex.openCursor")}} (cela fonctionne de la même façon que {{domxref("IDBObjectStore.openCursor")}} sauf que les enregistrements sont triés selon l'index et pas selon la clé primaire).</p>
+
+<p><code>myIndex.getKey('Bungle')</code> est ensuite utilisé afin d'obtenir la clé primaire de l'enregistrement pour lequel <code>lName</code> vaut <code>Bungle</code>. Le résultat de cette requête est imprimé dans la console lorsque la fonction de rappel (<em>callback</em>) de succès est déclenché.</p>
+
+<p>Enfin, on parcourt les enregistrements pour remplir un tableau HTML. Le dépôt <a href="https://github.com/mdn/IDBIndex-example">IDBIndex-example </a>contient un exemple complet (<a href="https://mdn.github.io/IDBIndex-example/">ainsi qu'une démonstration</a>).</p>
+
+<pre class="brush: js">function displayDataByIndex() {
+ tableEntry.innerHTML = '';
+ var transaction = db.transaction(['contactsList'], 'readonly');
+ var objectStore = transaction.objectStore('contactsList');
+ var myIndex = objectStore.index('lName');
+ var getKeyRequest = myIndex.getKey('Bungle');
+ getKeyRequest.onsuccess = function() {
+ console.log(getKeyRequest.result);
+ }
+
+ myIndex.openCursor().onsuccess = function(event) {
+ var cursor = event.target.result;
+ if(cursor) {
+ var tableRow = document.createElement('tr');
+ tableRow.innerHTML = '&lt;td&gt;' + cursor.value.id + '&lt;/td&gt;'
+ + '&lt;td&gt;' + cursor.value.lName + '&lt;/td&gt;'
+ + '&lt;td&gt;' + cursor.value.fName + '&lt;/td&gt;'
+ + '&lt;td&gt;' + cursor.value.jTitle + '&lt;/td&gt;'
+ + '&lt;td&gt;' + cursor.value.company + '&lt;/td&gt;'
+ + '&lt;td&gt;' + cursor.value.eMail + '&lt;/td&gt;'
+ + '&lt;td&gt;' + cursor.value.phone + '&lt;/td&gt;'
+ + '&lt;td&gt;' + cursor.value.age + '&lt;/td&gt;';
+ tableEntry.appendChild(tableRow);
+
+ cursor.continue();
+ } else {
+ console.log('Les éléments sont affichés.');
+ }
+ };
+};</pre>
+
+<h2 id="Spécification">Spécification</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('IndexedDB', '#widl-IDBIndex-getKey-IDBRequest-any-key', 'getKey()')}}</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>Edge</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Support simple</td>
+ <td>23{{property_prefix("webkit")}}<br>
+ 24</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>10 {{property_prefix("moz")}}<br>
+ {{CompatGeckoDesktop("16.0")}}</td>
+ <td>10, partial</td>
+ <td>15</td>
+ <td>7.1</td>
+ </tr>
+ <tr>
+ <td>Disponible dans les <em>web workers</em></td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoMobile("37.0")}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</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>Edge</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>Firefox OS</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>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoMobile("22.0")}}</td>
+ <td>1.0.1</td>
+ <td>10</td>
+ <td>22</td>
+ <td>8</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ <tr>
+ <td>Disponible dans les <em>web workers</em></td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatGeckoMobile("37.0")}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ <td>{{CompatUnknown}}</td>
+ <td>{{CompatVersionUnknown}}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<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>L'exemple de référence : <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">notifications de trucs à faire</a> (<a class="external" href="https://mdn.github.io/to-do-notifications/">voir la démonstration</a>)</li>
+</ul>