diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/idbindex/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/idbindex/index.html')
-rw-r--r-- | files/zh-cn/web/api/idbindex/index.html | 214 |
1 files changed, 214 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/idbindex/index.html b/files/zh-cn/web/api/idbindex/index.html new file mode 100644 index 0000000000..cb32afc0c0 --- /dev/null +++ b/files/zh-cn/web/api/idbindex/index.html @@ -0,0 +1,214 @@ +--- +title: IDBIndex +slug: Web/API/IDBIndex +translation_of: Web/API/IDBIndex +--- +<p>{{APIRef()}}</p> + +<div class="summary"> +<p><a href="/en/IndexedDB" title="en/IndexedDB">IndexedDB API</a> 中的IDBIndex接口提供了异步获取数据库中一个<a href="/en/IndexedDB/Basic_Concepts_Behind_IndexedDB#gloss_index" title="en/IndexedDB/Basic_Concepts_Behind_IndexedDB#gloss_index">index</a>的功能。index是一种用于在另一个object store中查找记录的object store,其被称为被引用的object store。你可以通过使用该接口来取回数据。</p> +</div> + +<p>你可以通过记录的键或使用一个index取回一个object store中的这些记录 (cursors 提供了第三种方式:请见 {{ domxref("IDBCursor") }})。一个index可以让你在object store的records中,通过使用records的properties(属性)来寻找records。</p> + +<p>index是一个持久的键-值存储,其中其记录的值部分是被引用object store中的record的关键部分。在object store中新增、更新或是删除records时,索引中的records将自动填充。索引中的每条记录只能指向其引用的object store中的唯一一条记录,但是多个索引可以引用同一个object store。当object store变更时,所有引用object store的索引都会自动更新。</p> + +<p>索引中的records总是按照records的key进行排序。然而,不像object stores,一个给定的index可以包含具有相同key的多条记录。这些records将根据被引用object store中的主键进一步排序。</p> + +<p>你可以设置在一个范围内的key,点击这里查看更多: {{domxref("IDBKeyRange")}}.</p> + +<h2 id="Methods">Methods</h2> + +<p>Inherits from: <a href="/en/DOM/EventTarget" title="en/DOM/EventTarget">EventTarget</a></p> + +<dl> + <dt>{{domxref("IDBIndex.count")}}</dt> + <dd>Returns an {{domxref("IDBRequest")}} object, and in a separate thread, returns the number of records within a key range.</dd> + <dt>{{domxref("IDBIndex.get")}}</dt> + <dd>Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, finds either the value in the referenced object store that corresponds to the given key or the first corresponding value, if <code>key</code> is a key range.</dd> + <dt>{{domxref("IDBIndex.getAll")}} {{ Non-Standard_inline() }}</dt> + <dd>Instantly retrieves all objects inside an {{domxref("IDBObjectStore")}}, setting them as the <code>result</code> of the request object.</dd> + <dt>{{domxref("IDBIndex.getKey")}}</dt> + <dd>Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, finds either the given key or the primary key, if <code>key</code> is a key range.</dd> + <dt>{{domxref("IDBIndex.getAllKeys")}} {{ Non-Standard_inline() }}</dt> + <dd>Instantly retrieves the keys of all objects inside an {{domxref("IDBObjectStore")}}, setting them as the <code><a href="/en/IndexedDB/IDBRequest#attr_result" title="en/IndexedDB/IDBRequest#attr result">result</a></code> of the request object.</dd> + <dt>{{domxref("IDBIndex.openCursor")}}</dt> + <dd>Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, creates a <a href="/en/IndexedDB#gloss_cursor" title="en/IndexedDB#gloss cursor">cursor</a> over the specified key range.</dd> + <dt>{{domxref("IDBIndex.openKeyCursor")}} {{ Non-Standard_inline() }}</dt> + <dd>Returns an {{domxref("IDBRequest")}} object, and, in a separate thread, creates a cursor over the specified key range, as arranged by this index.</dd> +</dl> + +<h2 id="Properties">Properties</h2> + +<dl> + <dt>{{domxref("IDBIndex.name")}} {{readonlyInline}}</dt> + <dd>index的名称。</dd> + <dt>{{domxref("IDBIndex.objectStore")}} {{readonlyInline}}</dt> + <dd>index所指向的object store的名称。</dd> + <dt>{{domxref("IDBIndex.keyPath")}} {{readonlyInline}}</dt> + <dd>此index的关键路径。它如果为null,则index不是自动填充的。</dd> + <dt>{{domxref("IDBIndex.multiEntry")}} {{readonlyInline}}</dt> + <dd>影响index的key路径的计算结果生成数组时索引的行为方式。如果为true,那么对于key数组中的每一项,索引中都有一条记录。如果为false,那么对于数组中的每个key都有一条记录。</dd> + <dt>{{domxref("IDBIndex.unique")}} {{readonlyInline}}</dt> + <dd>如果为true,这个index不会允许key有重复值。 </dd> +</dl> + +<h2 id="Examples">Examples</h2> + +<p>Opening a transaction then using <code>get()</code> to retrieve an object of known key:</p> + +<pre class="brush: js;highlight:[18,21] notranslate">// Let us open our database +var request = window.indexedDB.open("toDoList", 4); + +// these two event handlers act on the database being opened successfully, or not +request.onerror = function(event) { + note.innerHTML += '<li>Error loading database.</li>'; +}; + +request.onsuccess = function(event) { +note.innerHTML += '<li>Database initialised.</li>'; + +// store the result of opening the database in the db variable. +db = request.result; + +// Open a transaction on the current database and get a reference to the object store +//that we want to pull information out of +var transaction = db.transaction(["toDoList"]); +var objectStore = transaction.objectStore("toDoList"); + +// Use get() to get a specific object from the object store, the key of which is "Walk dog" +var request = objectStore.get("Walk dog"); +request.onerror = function(event) { + console.log("There is no record stored for " + request.result.taskTitle); +}; +request.onsuccess = function(event) { + // Do something with the request.result! + console.log("The deadline time for " + request.result.taskTitle + " is " + + request.result.hours + ":" + request.result.minutes + "."; +};</pre> + +<div class="note"> +<p><strong>Note</strong>: need to work out a way to retrieve a series/range of objects using an index, or just all of them. Is this possible with get, or is this a job for cursor?</p> +</div> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('IndexedDB', '#idl-def-IDBIndex', 'IDBIndex')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>12{{ property_prefix("-webkit") }}<br> + 23</td> + <td>{{ CompatGeckoDesktop("2.0") }}</td> + <td>10</td> + <td>17</td> + <td>{{ CompatNo() }}</td> + </tr> + <tr> + <td><code>count()</code></td> + <td>23</td> + <td>{{ CompatGeckoDesktop("10.0") }}</td> + <td>10</td> + <td>17</td> + <td>{{ CompatNo() }}</td> + </tr> + <tr> + <td><code>getAll()</code> and <code>getAllKeys()</code></td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatGeckoDesktop("24.0") }}<br> + behind <code>dom.indexedDB.experimental</code> pref</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatNo() }}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>4.4</td> + <td>{{ CompatGeckoMobile("2.0") }}</td> + <td>1.0.1</td> + <td>10</td> + <td>17</td> + <td>{{ CompatNo() }}</td> + </tr> + <tr> + <td><code>count()</code></td> + <td>4.4</td> + <td>{{ CompatGeckoMobile("10.0") }}</td> + <td>1.0.1</td> + <td>10</td> + <td>17</td> + <td>{{ CompatNo() }}</td> + </tr> + <tr> + <td><code>getAll()</code> and <code>getAllKeys()</code></td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatGeckoDesktop("24.0") }}<br> + behind <code>dom.indexedDB.experimental</code> pref</td> + <td>1.1 behind<br> + <code>dom.indexedDB.experimental</code> pref</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatNo() }}</td> + </tr> + </tbody> +</table> +</div> + +<div class="warning"> +<p>Be careful in Chrome as it still implements the old specification along the new one. Similarly it still has the prefixed <code>webkitIndexedDB</code> property even if the unprefixed <code>indexedDB</code> is present.</p> +</div> + +<h2 id="See_also">See also</h2> + +<p>To learn more about various topics, see the following</p> + +<ul> + <li>Starting transactions: {{domxref("IDBDatabase")}}</li> + <li>Setting transaction modes: {{domxref("IDBTransaction")}}</li> + <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li> + <li>The reference application for the examples in this reference: <a class="external" href="https://github.com/chrisdavidmills/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://chrisdavidmills.github.io/to-do-notifications/">view example live</a>.) Not every snippet appears in this example, but every example uses the same data structure and syntax, and they will make sense in the context of this example.</li> +</ul> |