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/idbobjectstore | |
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/idbobjectstore')
-rw-r--r-- | files/zh-cn/web/api/idbobjectstore/add/index.html | 179 | ||||
-rw-r--r-- | files/zh-cn/web/api/idbobjectstore/autoincrement/index.html | 133 | ||||
-rw-r--r-- | files/zh-cn/web/api/idbobjectstore/get/index.html | 149 | ||||
-rw-r--r-- | files/zh-cn/web/api/idbobjectstore/index.html | 759 | ||||
-rw-r--r-- | files/zh-cn/web/api/idbobjectstore/indexnames/index.html | 110 | ||||
-rw-r--r-- | files/zh-cn/web/api/idbobjectstore/keypath/index.html | 115 | ||||
-rw-r--r-- | files/zh-cn/web/api/idbobjectstore/opencursor/index.html | 171 | ||||
-rw-r--r-- | files/zh-cn/web/api/idbobjectstore/put/index.html | 163 |
8 files changed, 1779 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/idbobjectstore/add/index.html b/files/zh-cn/web/api/idbobjectstore/add/index.html new file mode 100644 index 0000000000..b953262c13 --- /dev/null +++ b/files/zh-cn/web/api/idbobjectstore/add/index.html @@ -0,0 +1,179 @@ +--- +title: IDBObjectStore.add() +slug: Web/API/IDBObjectStore/add +translation_of: Web/API/IDBObjectStore/add +--- +<p>{{ APIRef("IndexedDB") }}</p> + +<div> +<p>{{domxref("IDBObjectStore")}} 接口中的 <strong><code>add()</code></strong> 方法返回一个 {{domxref("IDBRequest")}} 对象,在单独的线程中创建一个结构(<a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interfaces.html#structured-clone">structured clone</a>)化克隆值,并且在对象存储中存储这个克隆值。这个方法用作在一个对象存储中添加一条新的记录。</p> +</div> + +<p> 要确定添加操作是否成功完成,可以监听事务的 <code>complete</code> 事件。除了 <code>IDBObjectStore.add </code>请求 <code>success</code> 事件之外,因为事务在成功被触发后仍然可能失败。换句话说,成功事件只有在事务成功排队后才会触发。</p> + +<p><strong><code>add()</code></strong> 方法是唯一的插入方法。如果以关键字参数作为主键的一条记录已经存在在存储对象中,这时在返回的请求对象中 <code>ConstrainError</code> 错误事件将被触发。对于更新存在的记录,你应该使用 {{domxref("IDBObjectStore.put")}} 方法替代它。</p> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">var <em>request</em> = objectStore.add(<em>value</em>); +var <em>request</em> = objectStore.add(<em>value</em>, <em>key</em>); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>value</dt> + <dd>需要存储的值。</dd> + <dt>key {{optional_inline}}</dt> + <dd>关键字用于识别记录。如果未指明,即为空。</dd> +</dl> + +<h3 id="返回">返回</h3> + +<p><span style="line-height: 1.5;">一个 {{domxref("IDBRequest")}} 对象,在该操作对象中触发与此相关的后续事件。</span></p> + +<h3 id="异常">异常</h3> + +<p><span style="line-height: 1.5;">这个方法可能导致以下类型中的一个</span> <span style="line-height: 1.5;">{{domxref("DOMException")}}</span> <span style="line-height: 1.5;">:</span></p> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>ReadOnlyError</code></td> + <td>与此操作相关联的事务处于只读模式。</td> + </tr> + <tr> + <td><code>TransactionInactiveError</code></td> + <td>当前 {{domxref("IDBObjectStore")}} 事务不可用。</td> + </tr> + <tr> + <td><code><span style="line-height: normal;">DataError</span></code></td> + <td> + <p>适用于以下任何条件:</p> + + <ul> + <li>对象存储使用内联键或者拥有密钥生成器(键生成器),并且提供了键参数。</li> + <li>对象存储使用外联键或者没有密钥生成器(键生成器),并且没有提供键参数。</li> + <li>对象存储使用内联键但是没有密钥生成器(键生成器),并且对象存储的键路径不会产生一个有效的键值。</li> + <li>键参数已经被提供,但是不包含一个有效的键。</li> + </ul> + </td> + </tr> + <tr> + <td><code>InvalidStateError</code></td> + <td> + <p>{{domxref("IDBObjectStore")}} 已经被删除或者移除。</p> + </td> + </tr> + <tr> + <td><code>DataCloneError</code></td> + <td>通过内部结构的克隆算法,被存储的数据无法被克隆<br> + </td> + </tr> + <tr> + <td><code class="idl">ConstraintError</code></td> + <td> + <p>因为主键违法规定导致的插入操作失败(由于已存在的记录使用了相同的主键值)。</p> + </td> + </tr> + </tbody> +</table> + +<h2 id="示例">示例</h2> + +<p><span style="line-height: 1.5;">在以下的代码片段中,在我们数据库中打开一个 </span>read/write(读写)事务和使用 <code>add()</code> 方法添加一些数据到存储对象中。<span style="">还要注意附加到事务事件处理程序的函数,这些函数用于报告事务打开成功或失败时的结果。完整的示例代码,请查看我们的</span><a href="https://github.com/mdn/to-do-notifications/" style="line-height: 1.5;"> To-do Notifications</a><span style="line-height: 1.5;"> 应用</span><span style="">(</span> <a href="http://mdn.github.io/to-do-notifications/" style="line-height: 1.5;">在线查看示例</a> <span style="">)。</span></p> + +<pre class="brush: js notranslate">// Let us open our database +<span style="line-height: 1.5;">var DBOpenRequest = window.indexedDB.open("toDoList", 4); + +</span><span style="line-height: 1.5;">DBOpenRequest.onsuccess = function(event) {</span> + note.innerHTML += '<li>Database initialised.</li>'; + + // store the result of opening the database in the db variable. + // This is used a lot below + db = DBOpenRequest.result; + + // Run the addData() function to add the data to the database + addData(); +}; + +function addData() { + // Create a new object ready to insert into the IDB + var newItem = [ { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" } ]; + + // open a read/write db transaction, ready for adding the data + var transaction = db.transaction(["toDoList"], "readwrite"); + + // report on the success of the transaction completing, when everything is done + transaction.oncomplete = function(event) { + note.innerHTML += '<li>Transaction completed.</li>'; + }; + + transaction.onerror = function(event) { + note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>'; + }; + + // create an object store on the transaction + var objectStore = transaction.objectStore("toDoList"); + + // Make a request to add our newItem object to the object store + var objectStoreRequest = objectStore.add(newItem[0]); + + objectStoreRequest.onsuccess = function(event) { + // report the success of our request + note.innerHTML += '<li>Request successful.</li>'; + }; +};</pre> + +<h2 id="Specification"><span style="font-size: 2.14285714285714rem;">Specification</span></h2> + +<dl> +</dl> + +<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', '#dom-idbobjectstore-add', 'add()')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbobjectstore-add", "add()")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2> + +<div> + + +<p>{{Compat("api.IDBObjectStore.add")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li> + <li>Starting transactions: {{domxref("IDBDatabase")}}</li> + <li>Using transactions: {{domxref("IDBTransaction")}}</li> + <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li> + <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li> + <li>Using cursors: {{domxref("IDBCursor")}}</li> + <li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> +</ul> diff --git a/files/zh-cn/web/api/idbobjectstore/autoincrement/index.html b/files/zh-cn/web/api/idbobjectstore/autoincrement/index.html new file mode 100644 index 0000000000..0a4aca4377 --- /dev/null +++ b/files/zh-cn/web/api/idbobjectstore/autoincrement/index.html @@ -0,0 +1,133 @@ +--- +title: IDBObjectStore.autoIncrement +slug: Web/API/IDBObjectStore/autoIncrement +translation_of: Web/API/IDBObjectStore/autoIncrement +--- +<p>{{ APIRef("IndexedDB") }}</p> + +<div> +<p>{{domxref("IDBObjectStore")}}的只读属性<strong><code>autoIncrement</code></strong>接口返回当前objectStore的自增标记值(true或false)。</p> + +<p>什么是自增呢?熟悉SQL的朋友应该知道,SQL数据里面的字段可以设置自增,当一条记录被插入时,不必传入该字段,新记录的该字段值会在前面一条记录该字段值的基础上加1.而indexedDB里面的autoIncrement的同样的意义。(译者注)</p> + +<p>注意:每个objectStore的auto increment计数器是分开独立的。</p> + +<p>{{AvailableInWorkers}}</p> +</div> + +<h2 id="句法">句法</h2> + +<pre class="syntaxbox">var <em>myAutoIncrement</em> = <em>objectStore</em>.autoIncrement;</pre> + +<h3 id="Value">Value</h3> + +<p><span style="line-height: 1.5;">{{domxref("Boolean")}}:</span></p> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">值</th> + <th scope="col">含义</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>true</code></td> + <td>当前objectStore会自增</td> + </tr> + <tr> + <td><code>false</code></td> + <td>当前objectStore不会自增<br> + </td> + </tr> + </tbody> +</table> + +<h2 id="例子">例子</h2> + +<p>在下面代码片段中,我们在数据库里打开了一个可读写的事务(transaction),并且用<code>add()</code>向一个objectStore中添加了一些数据。在objectStore被创建之后,我们在console中打印了<span style="background-color: #fafbfc; font-family: consolas,monaco,andale mono,monospace; font-size: 1rem; line-height: 19px; white-space: pre;">objectStore.autoIncrement</span>的值。想查看完整的例子,请查看我们的<a href="https://github.com/mdn/to-do-notifications/" rel="noopener">To-do Notifications</a>应用(<a href="http://mdn.github.io/to-do-notifications/" rel="noopener">查看在线例子</a>)。</p> + +<pre class="brush: js" style="font-size: 14px;">// Let us open our database +<span style="line-height: 1.5;">var DBOpenRequest = window.indexedDB.open("toDoList", 4); + +</span><span style="line-height: 1.5;">DBOpenRequest.onsuccess = function(event) {</span> + note.innerHTML += '<li>Database initialised.</li>'; + + // store the result of opening the database in the db variable. + // This is used a lot below + db = DBOpenRequest.result; + + // Run the addData() function to add the data to the database + addData(); +}; + +function addData() { + // Create a new object ready to insert into the IDB + var newItem = [ { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" } ]; + + // open a read/write db transaction, ready for adding the data + var transaction = db.transaction(["toDoList"], "readwrite"); + + // report on the success of the transaction completing, when everything is done + transaction.oncomplete = function(event) { + note.innerHTML += '<li>Transaction completed.</li>'; + }; + + transaction.onerror = function(event) { + note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>'; + }; + + // create an object store on the transaction + var objectStore = transaction.objectStore("toDoList"); + console.log(objectStore.autoIncrement); + + // Make a request to add our newItem object to the object store + var objectStoreRequest = objectStore.add(newItem[0]); + + objectStoreRequest.onsuccess = function(event) { + // report the success of our request + note.innerHTML += '<li>Request successful.</li>'; + }; +};</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + <tr> + <td>{{SpecName('IndexedDB', '#widl-IDBObjectStore-autoIncrement', 'autoIncrement')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbobjectstore-autoincrement", "autoIncrement")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div> +<div class="hidden">本页的兼容性列表是通过结构化数据生成的。如果你想贡献数据,请check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并给我们发pull request.</div> + +<p>{{Compat("api.IDBObjectStore.autoIncrement")}}</p> +</div> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">使用 IndexedDB</a></li> + <li>开始学习事务 transactions: {{domxref("IDBDatabase")}}</li> + <li>使用事务 transactions: {{domxref("IDBTransaction")}}</li> + <li>key值域range的使用: {{domxref("IDBKeyRange")}}</li> + <li>检索、修改: {{domxref("IDBObjectStore")}}</li> + <li>使用游标: {{domxref("IDBCursor")}}</li> + <li>相关例子: <a href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> +</ul> diff --git a/files/zh-cn/web/api/idbobjectstore/get/index.html b/files/zh-cn/web/api/idbobjectstore/get/index.html new file mode 100644 index 0000000000..fb7938d827 --- /dev/null +++ b/files/zh-cn/web/api/idbobjectstore/get/index.html @@ -0,0 +1,149 @@ +--- +title: IDBObjectStore.get() +slug: Web/API/IDBObjectStore/get +translation_of: Web/API/IDBObjectStore/get +--- +<p>{{ APIRef("IndexedDB") }}</p> + +<div> +<p>{{domxref("IDBObjectStore")}} 的接口 <strong><code>get()</code></strong>方法 返回 {{domxref("IDBRequest")}} 对象,并在“单独的线程(separate thread)”中返回由指定键选择的“对象储存(object store)” 。这用于从对象储存检索特定记录。</p> +</div> + +<p>如果成功找到值,则会创建其值的结构化克隆,并设置为“请求对象(request object)”的 <code><a href="/en/IndexedDB/IDBRequest#attr_result" title="en/IndexedDB/IDBRequest#attr result">result</a></code> 。</p> + +<p>{{ Note("This method produces the same result for: a) a record that doesn't exist in the database and b) a record that has an undefined value. To tell these situations apart, call the <code>openCursor()</code> method with the same key. That method provides a cursor if the record exists, and no cursor if it does not.") }}</p> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">var <em>request</em> = <em>objectStore</em>.get(<em>key</em>);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>key</dt> + <dd><span style="">标识要检索的记录的键或键范围</span>。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p><span style="">触发与此操作相关的后续事件的</span> {{domxref("IDBRequest")}} 对象。</p> + +<h3 id="异常"><span style="">异常</span></h3> + +<p><span style="">此方法可能会引发以下类型之一的</span> <span style="line-height: 1.5;"> {{domxref("DOMException")}} :</span></p> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><font face="Courier New, Andale Mono, monospace"><span style="line-height: normal;">TransactionInactiveError</span></font></td> + <td>This {{domxref("IDBObjectStore")}}'s transaction is inactive.</td> + </tr> + <tr> + <td><font face="Courier New, Andale Mono, monospace"><span style="line-height: normal;">DataError</span></font></td> + <td> + <p>The key or key range provided contains an invalid key.</p> + </td> + </tr> + <tr> + <td><code>InvalidStateError</code></td> + <td>The {{domxref("IDBObjectStore")}} has been deleted or removed.<br> + </td> + </tr> + </tbody> +</table> + +<h2 id="例子">例子</h2> + +<p>在以下的代码段中,我们在数据库上打开一个“读/写 事务(read/write transaction)”,并使用 <code>get()</code> 从“对象储存( object store )”中获取一个特定的记录——一个带有“Walk dog”键的示例记录。一旦检索到这个数据对象,你就可以使用普通的JavaScript更新它,然后使用 {{domxref("IDBObjectStore.put")}} 操作将其放回数据库。有关完整的工作示例,查看我们的 <a href="https://github.com/mdn/to-do-notifications/" style="line-height: 1.5;">To-do Notifications</a><span style="line-height: 1.5;"> app (</span><a href="http://mdn.github.io/to-do-notifications/" style="line-height: 1.5;">view example live</a><span style="line-height: 1.5;">.)</span></p> + +<pre class="brush: js notranslate">// Let us open our database +<span style="line-height: 1.5;">var DBOpenRequest = window.indexedDB.open("toDoList", 4); + +</span><span style="line-height: 1.5;">DBOpenRequest.onsuccess = function(event) {</span> + note.innerHTML += '<li>Database initialised.</li>'; + + // store the result of opening the database in the db variable. + // This is used a lot below + db = DBOpenRequest.result; + + // Run the getData() function to get the data from the database + getData(); +}; + +function getData() { + // open a read/write db transaction, ready for retrieving the data + var transaction = db.transaction(["toDoList"], "readwrite"); + + // report on the success of the transaction completing, when everything is done + transaction.oncomplete = function(event) { + note.innerHTML += '<li>Transaction completed.</li>'; + }; + + transaction.onerror = function(event) { + note.innerHTML += '<li>Transaction not opened due to error: ' + transaction.error + '</li>'; + }; + + // create an object store on the transaction + var objectStore = transaction.objectStore("toDoList"); + + // Make a request to get a record by key from the object store + var objectStoreRequest = objectStore.get("Walk dog"); + + objectStoreRequest.onsuccess = function(event) { + // report the success of our request + note.innerHTML += '<li>Request successful.</li>'; + + var myRecord = objectStoreRequest.result; + }; + +};</pre> + +<h2 id="规范"><span style="font-size: 2.14285714285714rem;">规范</span></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', '#dom-idbobjectstore-get', 'get()')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbobjectstore-get", "get()")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.IDBObjectStore.get")}}</p> +</div> + +<h2 id="另请参阅">另请参阅</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li> + <li>Starting transactions: {{domxref("IDBDatabase")}}</li> + <li>Using transactions: {{domxref("IDBTransaction")}}</li> + <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li> + <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li> + <li>Using cursors: {{domxref("IDBCursor")}}</li> + <li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> +</ul> diff --git a/files/zh-cn/web/api/idbobjectstore/index.html b/files/zh-cn/web/api/idbobjectstore/index.html new file mode 100644 index 0000000000..172820799e --- /dev/null +++ b/files/zh-cn/web/api/idbobjectstore/index.html @@ -0,0 +1,759 @@ +--- +title: IDBObjectStore +slug: Web/API/IDBObjectStore +tags: + - API + - IDBObjectStore + - IndexedDB + - Storage + - local storage & cache +translation_of: Web/API/IDBObjectStore +--- +<p>{{APIRef("IndexedDB")}}</p> + +<p> <a href="https://developer.mozilla.org/en/IndexedDB" title="en/IndexedDB">IndexedDB API</a> 的 <strong><code>IDBObjectStore </code></strong>接口表示数据库中的 一个 <a href="https://developer.mozilla.org/en/IndexedDB#gloss_object_store" title="en/IndexedDB#gloss object store">对象库(object store)</a> 。对象库中的记录根据其键值进行排序。这种排序可以实现快速插入,查找和有序检索。</p> + +<p>{{AvailableInWorkers}}</p> + +<p>注:为了方便理解,可以把“对象存储空间”想象成关系数据库的“表”结构,下文也会把对象存储空间称为表。</p> + +<h2 id="方法预览">方法预览</h2> + +<table class="standard-table"> + <tbody> + <tr> + <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#add()">add</a> (in any value, in optional any key) raises (<a href="/En/DOM/DOMException" title="en/DOM/DOMException">DOMException</a>);</code></td> + </tr> + <tr> + <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#clear()">clear</a> () raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td> + </tr> + <tr> + <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#count()">count</a> (in optional any key) raises</code><code>(<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td> + </tr> + <tr> + <td><code><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a> <a href="#createIndex()">createIndex</a> (in <a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a> name, in </code><code><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code> keyPath, in optional boolean unique) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td> + </tr> + <tr> + <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#delete()">delete</a> (in any key) raises </code><code>(<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td> + </tr> + <tr> + <td><code>void <a href="#deleteIndex()">deleteIndex</a> (in any </code><code><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code> indexName) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td> + </tr> + <tr> + <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#get()">get</a> (in any key) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td> + </tr> + <tr> + <td><code><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a> <a href="#index()">index</a> (in </code><code><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code> name) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td> + </tr> + <tr> + <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#openCursor()">openCursor</a> (in optional <a href="/en/IndexedDB/IDBKeyRange" title="en/IndexedDB/IDBKeyRange">IDBKeyRange</a> range, in optional unsigned short direction) raises(<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td> + </tr> + <tr> + <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#put()">put</a> (in any value, in optional any key) raises (<a href="/En/DOM/DOMException" title="en/DOM/DOMException">DOMException</a>);</code></td> + </tr> + </tbody> +</table> + +<h2 id="属性">属性</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Attribute</th> + <th scope="col">Type</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td id="attr_indexNames"><code>indexNames</code></td> + <td><code>readonly DOMStringList</code></td> + <td>表中对象的<a href="https://developer.mozilla.org/en/IndexedDB#gloss_index" title="https://developer.mozilla.org/en/IndexedDB#gloss_index">索引</a>名列表。</td> + </tr> + <tr> + <td id="attr_keyPath"><code>keyPath</code></td> + <td><code>readonly </code><code><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code></td> + <td>表中的<a href="https://developer.mozilla.org/en/IndexedDB#gloss_key_path" title="https://developer.mozilla.org/en/IndexedDB#gloss_key_path">键路径</a>,如果该属性为null,每次操作表时必须提供一个键名。</td> + </tr> + <tr> + <td id="attr_name"><code>name</code></td> + <td><code>readonly </code><code><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code></td> + <td>表名</td> + </tr> + <tr> + <td id="transaction"><code>transaction</code></td> + <td><code>readonly <a href="/en-US/docs/IndexedDB/IDBTransaction" title="/en-US/docs/IndexedDB/IDBTransaction">IDBTransaction</a></code></td> + <td>事务的名称,该表属于此事务。</td> + </tr> + <tr> + <td id="autoincrement"><code>autoIncrement</code></td> + <td><code>readonly boolean</code></td> + <td>表中自增字段的值</td> + </tr> + </tbody> +</table> + +<h2 id="方法">方法</h2> + +<h3 id="add">add()</h3> + +<p>返回一个IDBRequest对象,并且在新线程中克隆一个值,该值存储在表中。</p> + +<p>想知道是否成功添加数据,可以在事务的complete事件中进行监听,而不是success,因为事务在success事件之后还有可能失败。</p> + +<p>add方法只能插入数据。如果以key参数作为某记录的关键字,并且该条记录已存在,则其所返回的请求对象会产生ConstrainError错误。</p> + +<pre><a href="/en/IndexedDB/IDBRequest" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBRequest">IDBRequest</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> </span><a href="#add" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="#add">add</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> (in any value, in optional any key) raises (</span><a href="/En/DOM/DOMException" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/DOM/DOMException">DOMException</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;">);</span></pre> + +<h5 id="参数">参数</h5> + +<dl> + <dt>value</dt> + <dd>被存储的值。</dd> + <dt>key</dt> + <dd>标识某条记录的键,如果不指定,它会被设为null。</dd> +</dl> + +<h5 id="返回">返回</h5> + +<dl> + <dt><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></dt> + <dd>一个请求对象,可以在其中绑定事件。</dd> +</dl> + +<h5 id="异常">异常</h5> + +<p>该方法会抛出DOMError类型的DOMException异常。</p> + +<table class="standard-table" style="font-size: 14px;"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td>ReadOnlyError</td> + <td>The transaction associated with this operation is in read-only <a href="/en-US/docs/IndexedDB/IDBTransaction#mode_constants" title="/en-US/docs/IndexedDB/IDBTransaction#mode_constants">mode</a>.</td> + </tr> + <tr> + <td>TransactionInactiveError</td> + <td>This IDBObjectStore's transaction is inactive.</td> + </tr> + <tr> + <td><span style="line-height: normal;">DataError</span></td> + <td> + <p>Any of the following conditions apply:</p> + + <ul> + <li>The object store uses in-line keys or has a key generator, and a key parameter was provided.</li> + <li>The object store uses out-of-line keys and has no key generator, and no key parameter was provided.</li> + <li>The object store uses in-line keys but no key generator, and the object store's key path does not yield a valid key.</li> + <li>The key parameter was provided but does not contain a valid key.</li> + </ul> + </td> + </tr> + <tr> + <td><code>InvalidStateError</code></td> + <td>The IDBObjectStore has been deleted or removed.</td> + </tr> + <tr> + <td><code>DataCloneError</code></td> + <td>The data being stored could not be cloned by the internal structured cloning algorithm.</td> + </tr> + </tbody> +</table> + +<h3 id="clear">clear()</h3> + +<p>创建并立即返回一个 <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> 对象, 并且在一个单独的线程中清除这个对象存储. 清除对象存储包括从对象存储中删除所有的记录,并删除对象存储引用的索引中的所有记录。</p> + +<pre><a href="/en/IndexedDB/IDBRequest" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBRequest">IDBRequest</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> </span><a href="#clear" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="#clear">clear</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> () raises (</span><a href="/en-US/docs/DOM/DOMException" style="font-family: 'Courier New', 'Andale Mono', monospace;" title="/en-US/docs/DOM/DOMException">DOMException</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;">);</span></pre> + +<h5 id="Returns">Returns</h5> + +<dl> + <dt><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></dt> + <dd>返回一个request对象,在其上触发与操作相关的事件。</dd> +</dl> + +<h5 id="Exceptions">Exceptions</h5> + +<p>此方法可能会引发domException,其中domError的类型如下:</p> + +<table class="standard-table" style="font-size: 14px;"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td>ReadOnlyError</td> + <td>The transaction associated with this operation is in read-only <a href="/en-US/docs/IndexedDB/IDBTransaction#mode_constants" title="/en-US/docs/IndexedDB/IDBTransaction#mode_constants">mode</a>.</td> + </tr> + <tr> + <td>TransactionInactiveError</td> + <td>This IDBObjectStore's transaction is inactive.</td> + </tr> + </tbody> +</table> + +<h3 id="count">count()</h3> + +<p>立即返回一个 <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> 对象,并在新线程中计算符合条件的对象的数量,该方法的参数可以是键,或键范围(key range)。在 <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> 对象中,source属性就是IDBObjectStore对象,result属性持有计算后的数量值。如果参数非法将会抛出异常。</p> + +<pre><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="/en/IndexedDB/IDBObjectStore#count" title="en/IndexedDB/IDBObjectStore#count">count</a> (in optional any key) raises</code><code style="font-size: 14px; color: rgb(51, 51, 51);">(<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></pre> + +<h5 id="参数_2">参数</h5> + +<dl> + <dt>key</dt> + <dd>计算被该键或键范围(key range)所标识的记录数。</dd> +</dl> + +<h5 id="Returns_2">Returns</h5> + +<dl> + <dt><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></dt> + <dd>一个请求对象,可绑定事件。</dd> +</dl> + +<h5 id="异常_2">异常</h5> + +<p>该方法会引发如下异常:</p> + +<table class="standard-table" style="font-size: 14px;"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td>TransactionInactiveError</td> + <td>事务已闲置</td> + </tr> + <tr> + <td>DataError</td> + <td> + <p>key参数非法</p> + </td> + </tr> + <tr> + <td><code style="font-size: 14px;">InvalidStateError</code></td> + <td>IDBObjectStore对象已被删除</td> + </tr> + </tbody> +</table> + +<h3 id="createIndex">createIndex()</h3> + +<dl> +</dl> + +<p>创建并返回新的IDBIndex对象,该方法只能从versionchange事务模式的回调方法中被调用。</p> + +<pre><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a> <a href="#createIndex" title="#createIndex">createIndex</a> (in <a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a> name, in </code><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code style="font-size: 14px; color: rgb(51, 51, 51);"> keyPath, in optional boolean unique) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></pre> + +<h5 id="Parameters">Parameters</h5> + +<dl> + <dt>name</dt> + <dd>The name of the index to create.</dd> + <dt>keyPath</dt> + <dd>The key path for the index to use.</dd> + <dt>optionalParameters</dt> + <dd> + <p class="warning">The IDBIndexParameters object whose attributes are optional parameters to the method. It includes the following properties:</p> + + <table class="standard-table"> + <thead> + <tr> + <th scope="col">Attribute</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>unique</code></td> + <td>If true, the index will not allow duplicate values for a single key.</td> + </tr> + <tr> + <td><code>multiEntry</code></td> + <td>If true, the index will add an entry in the index for each array element when the <em>keypath</em> resolves to an Array. If false, it will add one single entry containing the Array.</td> + </tr> + </tbody> + </table> + </dd> +</dl> + +<h5 id="Returns_3">Returns</h5> + +<dl> + <dt><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a></dt> + <dd>The newly created index.</dd> +</dl> + +<h5 id="Exceptions_2">Exceptions</h5> + +<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p> + +<table class="standard-table" style="font-size: 14px;"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code style="font-size: 14px;">InvalidStateError</code></td> + <td>The IDBObjectStore has been deleted or removed or the method was not called from a <code><a href="/en-US/docs/IndexedDB/IDBTransaction#VERSION_CHANGE" title="/en-US/docs/IndexedDB/IDBTransaction#VERSION_CHANGE">versionchange</a></code> transaction mode callback.</td> + </tr> + <tr> + <td><code style="font-size: 14px;">ConstraintError</code></td> + <td>An index with the same name (case-sensitive) already exists in the database.<br> + </td> + </tr> + </tbody> +</table> + +<h3 id="delete">delete()</h3> + +<dl> +</dl> + +<p>Immediately returns an <code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></code> object, and removes the records specified by the given key or key range from this object store, and any indexes that reference it, in a separate thread.</p> + +<pre><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#delete" title="#delete">delete</a> (in any key) raises </code><code style="font-size: 14px; color: rgb(51, 51, 51);">(<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></pre> + +<h5 id="Parameters_2">Parameters</h5> + +<dl> + <dt>key</dt> + <dd>The key or key range that identifies the records.</dd> +</dl> + +<h5 id="Returns_4">Returns</h5> + +<dl> + <dt><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></dt> + <dd>A request object on which subsequent events related to this operation are fired. As <a href="http://www.w3.org/TR/IndexedDB/#object-store-deletion-operation" title="http://www.w3.org/TR/IndexedDB/#object-store-deletion-operation">per spec</a> the result of the Object Store Deletion Operation algorithm is <code>undefined</code>, so it's not possible to know if some records were actually deleted by looking at the request result.</dd> +</dl> + +<h5 id="Exceptions_3">Exceptions</h5> + +<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p> + +<table class="standard-table" style="font-size: 14px;"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td>TransactionInactiveError</td> + <td>This IDBObjectStore's transaction is inactive.</td> + </tr> + <tr> + <td>ReadOnlyError</td> + <td> + <p>The transaction associated with this operation is in read-only <a href="/en-US/docs/IndexedDB/IDBTransaction#mode_constants" title="/en-US/docs/IndexedDB/IDBTransaction#mode_constants">mode</a>.</p> + </td> + </tr> + <tr> + <td><code>DataError</code></td> + <td>The key or key range provided contains an invalid key.</td> + </tr> + </tbody> +</table> + +<dl> +</dl> + +<div class="note"> +<p>If the key that identifies the record is a Number, the key passed to the delete method must be a Number too, and not a String. So for example you might need to do the following:</p> + +<pre class="brush: js">var key_val = '42'; +var key = Number(key_val); +objectstore.delete(key);</pre> +</div> + +<h3 id="deleteIndex">deleteIndex()</h3> + +<p>Destroys the index with the specified name in the connected database. Note that this method must be called only from a <code><a href="/en/IndexedDB/IDBTransaction#VERSION_CHANGE" title="en/IndexedDB/IDBTransaction#VERSION CHANGE">VersionChange</a></code> transaction mode callback. Note that this method synchronously modifies the <a href="#attr_indexNames" title="#attr_indexNames">indexNames</a> property.</p> + +<pre><code style="font-size: 14px; color: rgb(51, 51, 51);">void <a href="#deleteIndex" title="#deleteIndex">deleteIndex</a> (in any </code><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code style="font-size: 14px; color: rgb(51, 51, 51);"> indexName) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></pre> + +<h5 id="Parameters_3">Parameters</h5> + +<dl> + <dt>indexName</dt> + <dd>The name of the existing index to remove.</dd> + <dt>Returns</dt> + <dd>Void</dd> +</dl> + +<h5 id="Exceptions_4">Exceptions</h5> + +<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p> + +<table class="standard-table" style="font-size: 14px;"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code style="font-size: 14px;">InvalidStateError</code></td> + <td>The method was not called from a <code style="font-size: 14px;"><a href="/en-US/docs/IndexedDB/IDBTransaction#VERSION_CHANGE" title="/en-US/docs/IndexedDB/IDBTransaction#VERSION_CHANGE">versionchange</a></code> transaction mode callback.</td> + </tr> + <tr> + <td><code style="font-size: 14px;">NotFoundError</code></td> + <td>There is no index with the given name (case-sensitive) in the database.</td> + </tr> + </tbody> +</table> + +<h3 id="get">get()</h3> + +<p>Immediately returns an <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> object, and retrieves the requested record from the object store in a separate thread. If the operation is successful, then a success event is fired on the returned object, with its <code><a href="/en/IndexedDB/IDBSuccessEvent#attr_result" title="en/IndexedDB/IDBSuccessEvent#attr result">result</a></code> set to the retrieved value, and <code><a href="/en/IndexedDB/IDBTransactionEvent#attr_transaction" title="en/IndexedDB/IDBTransactionEvent#attr transaction">transaction</a></code> set to the transaction in which this object store is opened. </p> + +<pre><a href="/en/IndexedDB/IDBRequest" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBRequest">IDBRequest</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> </span><a href="#get" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="#get">get</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> (in any key) raises (</span><a href="/en-US/docs/DOM/DOMException" style="font-family: 'Courier New', 'Andale Mono', monospace;" title="/en-US/docs/DOM/DOMException">DOMException</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;">);</span></pre> + +<p>{{ Note("This function produces the same result if no record with the given key exists in the database as when a record exists, but with an <code>undefined</code> value. To tell these situations apart, call the openCursor() method with the same key. That method provides a cursor if the record exists, and no cursor if it does not.") }}</p> + +<h5 id="Parameters_4">Parameters</h5> + +<dl> + <dt>key</dt> + <dd>The key or key range identifying the record to retrieve. In the case of a key range, the record returned is the first record associated with the first key in the range.</dd> +</dl> + +<h5 id="Returns_5">Returns</h5> + +<dl> + <dt><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></code></dt> + <dd>A request object on which subsequent events related to this operation are fired.</dd> +</dl> + +<h5 id="Exceptions_5">Exceptions</h5> + +<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p> + +<table class="standard-table" style="font-size: 14px;"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td>TransactionInactiveError</td> + <td>This IDBObjectStore's transaction is inactive.</td> + </tr> + <tr> + <td>DataError</td> + <td> + <p>The key or key range provided contains and invalid key.</p> + </td> + </tr> + <tr> + <td><code style="font-size: 14px;">InvalidStateError</code></td> + <td>The IDBObjectStore has been deleted or removed.<br> + </td> + </tr> + </tbody> +</table> + +<h3 id="index">index()</h3> + +<p>Opens the named index in this object store.</p> + +<pre><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a> <a href="#index" title="#index">index</a> (in </code><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code style="font-size: 14px; color: rgb(51, 51, 51);"> name) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></pre> + +<h5 id="Parameters_5">Parameters</h5> + +<dl> + <dt>name</dt> + <dd>The name of the index to open.</dd> +</dl> + +<h5 id="Returns_6">Returns</h5> + +<dl> + <dt><code><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a></code></dt> + <dd>An object for accessing the index.</dd> +</dl> + +<h5 id="Exceptions_6">Exceptions</h5> + +<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p> + +<table class="standard-table" style="font-size: 14px;"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code style="font-size: 14px;">InvalidStateError</code></td> + <td>The source object store has been deleted, or the transaction for the object store has finished.</td> + </tr> + <tr> + <td><code style="font-size: 14px;">NotFoundError</code></td> + <td>There is no index with the given name (case-sensitive) in the database.<br> + </td> + </tr> + </tbody> +</table> + +<h3 id="openCursor">openCursor()</h3> + +<p>Immediately returns an <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> object, and creates a <a href="/en-US/docs/IndexedDB/IDBCursor" title="en/IndexedDB#gloss cursor">cursor</a> over the records in this object store, in a separate thread. If there is even a single record that matches the <a href="/en/IndexedDB#gloss_key_range" title="en/IndexedDB#gloss key range">key range</a>, then a success event is fired on the returned object, with its <code><a href="/en/IndexedDB/IDBSuccessEvent#attr_result" title="en/IndexedDB/IDBSuccessEvent#attr result">result</a></code> set to the <a href="/en/IndexedDB/IDBCursor" title="en/IndexedDB/IDBCursor">IDBCursor</a> object for the new cursor. If no records match the key range, then a success event is fired on the returned object, with its <code><a href="/en/IndexedDB/IDBSuccessEvent#attr_result" title="en/IndexedDB/IDBSuccessEvent#attr result">result</a></code> set to null.</p> + +<pre><a href="/en/IndexedDB/IDBRequest" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBRequest">IDBRequest</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> </span><a href="#openCursor" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="#openCursor">openCursor</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> (in optional </span><a href="/en/IndexedDB/IDBKeyRange" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBKeyRange">IDBKeyRange</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> range, in optional unsigned short direction) raises(</span><a href="/en-US/docs/DOM/DOMException" style="font-family: 'Courier New', 'Andale Mono', monospace;" title="/en-US/docs/DOM/DOMException">DOMException</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;">);</span></pre> + +<h5 id="Parameters_6">Parameters</h5> + +<dl> + <dt>range</dt> + <dd>The key range to use as the cursor's range. If this parameter is unspecified or null, then the range includes all the records in the object store.</dd> + <dt>direction</dt> + <dd>The cursor's <a href="/en-US/docs/IndexedDB/IDBCursor" title="/en-US/docs/IndexedDB/IDBCursor">direction</a>.</dd> +</dl> + +<h5 id="Returns_7">Returns</h5> + +<dl> + <dt><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></code></dt> + <dd>A request object on which subsequent events related to this operation are fired.</dd> +</dl> + +<h5 id="Exceptions_7">Exceptions</h5> + +<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p> + +<table class="standard-table" style="font-size: 14px;"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td>TransactionInactiveError</td> + <td>This IDBObjectStore's transaction is inactive.</td> + </tr> + <tr> + <td>DataError</td> + <td> + <p>The key or key range provided contains and invalid key.</p> + </td> + </tr> + <tr> + <td><code style="font-size: 14px;">InvalidStateError</code></td> + <td>The IDBObjectStore has been deleted or removed.</td> + </tr> + <tr> + <td><code>TypeError</code></td> + <td>The value of the <code>direction</code> parameter is invalid.</td> + </tr> + </tbody> +</table> + +<h3 id="put">put()</h3> + +<p>Returns an <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> object, and, in a separate thread, creates a <a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interfaces.html#structured-clone">structured clone</a> of the <code>value</code>, and stores the cloned value in the object store. If the record is successfully stored, then a success event is fired on the returned request object with the <code><a href="/en-US/docs/IndexedDB/IDBRequest" title="en/IndexedDB/IDBSuccessEvent#attr result">result</a></code> set to the key for the stored record, and <code><a href="/en-US/docs/IndexedDB/IDBRequest" title="en/IndexedDB/IDBTransactionEvent#attr transaction">transaction</a></code> set to the transaction in which this object store is opened.</p> + +<p>The put method is an<span class="database"> <em>update or insert</em> </span>method. See also the <a href="#add()">add()</a> method.</p> + +<pre><a href="/en/IndexedDB/IDBRequest" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBRequest">IDBRequest</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> </span><a href="#put" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="#put">put</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> (in any value, in optional any key) raises (</span><a href="/En/DOM/DOMException" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/DOM/DOMException">DOMException</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;">);</span></pre> + +<h5 id="Parameters_7">Parameters</h5> + +<dl> + <dt>value</dt> + <dd>The value to be stored.</dd> + <dt>key</dt> + <dd>The key to use to identify the record. If unspecified, it results to null.</dd> +</dl> + +<h5 id="Returns_8">Returns</h5> + +<dl> + <dt>IDBRequest</dt> + <dd>A request object on which subsequent events related to this operation are fired.</dd> +</dl> + +<h5 id="Exceptions_8">Exceptions</h5> + +<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p> + +<table class="standard-table" style="font-size: 14px;"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td>ReadOnlyError</td> + <td>The transaction associated with this operation is in read-only <a href="/en-US/docs/IndexedDB/IDBTransaction#mode_constants" title="/en-US/docs/IndexedDB/IDBTransaction#mode_constants">mode</a>.</td> + </tr> + <tr> + <td>TransactionInactiveError</td> + <td>This IDBObjectStore's transaction is inactive.</td> + </tr> + <tr> + <td>DataError</td> + <td> + <p>Any of the following conditions apply:</p> + + <ul> + <li>The object store uses in-line keys or has a key generator, and a key parameter was provided.</li> + <li>The object store uses out-of-line keys and has no key generator, and no key parameter was provided.</li> + <li>The object store uses in-line keys but no key generator, and the object store's key path does not yield a valid key.</li> + <li>The key parameter was provided but does not contain a valid key.</li> + </ul> + </td> + </tr> + <tr> + <td><code style="font-size: 14px;">InvalidStateError</code></td> + <td>The IDBObjectStore has been deleted or removed.</td> + </tr> + <tr> + <td><code style="font-size: 14px;">DataCloneError</code></td> + <td>The data being stored could not be cloned by the internal structured cloning algorithm.</td> + </tr> + </tbody> +</table> + +<h2 id="Example">Example</h2> + +<dl> +</dl> + +<dl> +</dl> + +<p>This example shows a variety of different uses of object stores, from updating the data structure with {{domxref("IDBObjectStore.createIndex")}} inside an <code>onupgradeneeded</code>function, to adding a new item to our object store with {{domxref("IDBObjectStore.add")}}. For a full working example, see our <a href="https://github.com/mdn/to-do-notifications/">To-do Notifications</a> app (<a href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</p> + +<pre><code>// Let us open our database +var DBOpenRequest = window.indexedDB.open("toDoList", 4); + +DBOpenRequest.onsuccess = function(event) { + note.innerHTML += '<li>Database initialised.</li>'; + + // store the result of opening the database in db. + db = DBOpenRequest.result; +}; + +// This event handles the event whereby a new version of +// the database needs to be created Either one has not +// been created before, or a new version number has been +// submitted via the window.indexedDB.open line above +DBOpenRequest.onupgradeneeded = function(event) { + var db = event.target.result; + + db.onerror = function(event) { + note.innerHTML += '<li>Error loading database.</li>'; + }; + + // Create an objectStore for this database + + var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" }); + + // define what data items the objectStore will contain + + objectStore.createIndex("hours", "hours", { unique: false }); + objectStore.createIndex("minutes", "minutes", { unique: false }); + objectStore.createIndex("day", "day", { unique: false }); + objectStore.createIndex("month", "month", { unique: false }); + objectStore.createIndex("year", "year", { unique: false }); + + objectStore.createIndex("notified", "notified", { unique: false }); + + note.innerHTML += '<li>Object store created.</li>'; +}; + +// Create a new item to add in to the object store +var newItem = [ + { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: 'December', year: 2013, notified: "no" } +]; + +// open a read/write db transaction, ready for adding the data +var transaction = db.transaction(["toDoList"], "readwrite"); + +// report on the success of the transaction completing, when everything is done +transaction.oncomplete = function(event) { + note.innerHTML += '<li>Transaction completed.</li>'; +}; + +transaction.onerror = function(event) { + note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>'; +}; + +// create an object store on the transaction +var objectStore = transaction.objectStore("toDoList"); +// make a request to add our newItem object to the object store +var objectStoreRequest = objectStore.add(newItem[0]); + +objectStoreRequest.onsuccess = function(event) { + note.innerHTML += '<li>Request successful .</li>'; +}</code></pre> + +<h2 id="Specifications">Specifications</h2> + +<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-IDBObjectStore', 'IDBObjectStore')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#object-store-interface", "IDBObjectStore")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2> + +<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>and send us a pull request.</p> + +<p>{{Compat("api.IDBObjectStore")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li> + <li>Starting transactions: {{domxref("IDBDatabase")}}</li> + <li>Using transactions: {{domxref("IDBTransaction")}}</li> + <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li> + <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li> + <li>Using cursors: {{domxref("IDBCursor")}}</li> + <li>Reference example: <a href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> +</ul> + +<div id="compat-mobile"></div> diff --git a/files/zh-cn/web/api/idbobjectstore/indexnames/index.html b/files/zh-cn/web/api/idbobjectstore/indexnames/index.html new file mode 100644 index 0000000000..c124f70a9b --- /dev/null +++ b/files/zh-cn/web/api/idbobjectstore/indexnames/index.html @@ -0,0 +1,110 @@ +--- +title: IDBObjectStore.indexNames +slug: Web/API/IDBObjectStore/indexNames +translation_of: Web/API/IDBObjectStore/indexNames +--- +<p>{{ APIRef("IndexedDB") }}</p> + +<div> +<p>{{domxref("IDBObjectStore")}} 的只读属性 <strong><code>indexNames</code></strong> 返回此对象存储中对象的 <a href="https://developer.mozilla.org/en/IndexedDB#gloss_index" title="en/IndexedDB#gloss index">indexes</a> 名称(name)列表。</p> + +<p>{{AvailableInWorkers}}</p> +</div> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox">var <em>myindexNames</em> = <em>objectStore</em>.indexNames;</pre> + +<h3 id="Value">Value</h3> + +<p>一个 {{domxref("DOMStringList")}}.</p> + +<h2 id="Example">Example</h2> + +<p>在下面的代码片段中,我们在数据库上打开一个读/写事务并使用 <code>add()</code> 向对象存储添加一些数据。创建对象存储后,我们将打印 <span style="background-color: #fafbfc; font-family: consolas,monaco,andale mono,monospace; font-size: 1rem; line-height: 19px; white-space: pre;"><code>objectStore.indexNames</code></span> 到控制台。有关完整的工作示例,请参阅我们的 <a href="https://github.com/mdn/to-do-notifications/">待办事项通知</a>应用程序<span style="line-height: 1.5;"> ( </span><a href="http://mdn.github.io/to-do-notifications/">实时查看示例</a><span style="line-height: 1.5;"> )</span></p> + +<pre class="brush: js" style="font-size: 14px;">// 让我们来打开我们的数据库 +<span style="line-height: 1.5;">var DBOpenRequest = window.indexedDB.open("toDoList", 4); + +</span><span style="line-height: 1.5;">DBOpenRequest.onsuccess = function(event) {</span> + note.innerHTML += '<li>Database initialised.</li>'; + + // 将打开数据库的结果存储在db变量中 + // 下面经常用到这个 + db = this.result; + + // 运行 addData() 函数将数据添加到数据库 + addData(); +}; + +function addData() { + // 创建一个新对象以准备插入到IDB中 + var newItem = [ { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" } ]; + + // 打开读/写数据库事务,准备添加数据 + var transaction = db.transaction(["toDoList"], "readwrite"); + + // 当所有事情都完成时,报告事务完成的成功情况 + transaction.oncomplete = function(event) { + note.innerHTML += '<li>Transaction completed.</li>'; + }; + + + transaction.onerror = function(event) { + note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>'; + }; + + // 在事务上创建对象存储 + var objectStore = transaction.objectStore("toDoList"); + console.log(objectStore.indexNames); + + // 请求将 newItem 对象 添加到对象存储区 + var objectStoreRequest = objectStore.add(newItem[0]); + + objectStoreRequest.onsuccess = function(event) { + // 报告我们请求的成功 + note.innerHTML += '<li>Request successful.</li>'; + }; +};</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">解释</th> + </tr> + <tr> + <td>{{SpecName('IndexedDB', '#dom-idbobjectstore-indexnames', 'indexNames')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbobjectstore-indexnames", "indexNames")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.IDBObjectStore.indexNames")}}</p> +</div> + +<h2 id="查看其它内容">查看其它内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">使用 IndexedDB</a></li> + <li><span id="w_40">启动</span><span id="w_41">事务</span> : {{domxref("IDBDatabase")}}</li> + <li>使用事务 : {{domxref("IDBTransaction")}}</li> + <li>设置键的范围 : {{domxref("IDBKeyRange")}}</li> + <li>检索和更改数据 : {{domxref("IDBObjectStore")}}</li> + <li>使用游标 : {{domxref("IDBCursor")}}</li> + <li>参考示例 : <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> +</ul> diff --git a/files/zh-cn/web/api/idbobjectstore/keypath/index.html b/files/zh-cn/web/api/idbobjectstore/keypath/index.html new file mode 100644 index 0000000000..e99f1ae519 --- /dev/null +++ b/files/zh-cn/web/api/idbobjectstore/keypath/index.html @@ -0,0 +1,115 @@ +--- +title: IDBObjectStore.keyPath +slug: Web/API/IDBObjectStore/keyPath +translation_of: Web/API/IDBObjectStore/keyPath +--- +<p>{{ APIRef("IndexedDB") }}</p> + +<div> +<p>{{domxref("IDBObjectStore")}}的只读属性keyPath接口返回当前objectStore的<a href="/en-US/docs/Web/API/IndexedDB_API/Basic_Concepts_Behind_IndexedDB#gloss_keypath">key path</a>。</p> + +<p>什么是keyPath呢?在indexedDB中,一条记录就是一个object,object里面有一个属性作为这条记录的主要依据用来进行查询,而这个属性的属性名就是keyPath,属性值就是key。在用indexedDB的get方法时,提供key,而不需要指定keyPath,因为get方法把参数作为这个最主要的属性的值,在数据库中进行查询。(译者注)</p> + +<p>如果该属性值为null,应用中必须在每一次进行修改性质的操作时提供一个key。</p> + +<p>add、put方法都可以传第二个参数,当你当前的objectStore的autoIncrement为true时,你一般不会设置keyPath,如果这个时候你在put的时候不提供第二个参数,indexedDB就不知道要更新哪一条记录了。(译者注)</p> +</div> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="句法">句法</h2> + +<pre class="syntaxbox">var <em>mykeyPath</em> = <em>objectStore</em>.keyPath;</pre> + +<h3 id="Value">Value</h3> + +<p><span style="line-height: 1.5;">任何类型。</span></p> + +<h2 id="例子">例子</h2> + +<p>在下面代码片段中,我们在数据库里打开了一个可读写的事务(transaction),并且用<code>add()</code>向一个objectStore中添加了一些数据。在objectStore被创建之后,我们在console中打印了objectStore.keyPath的值。想查看完整的例子,请查看我们的<a href="https://github.com/mdn/to-do-notifications/" style="line-height: 1.5;">To-do Notifications</a>应用(<a href="http://mdn.github.io/to-do-notifications/" style="line-height: 1.5;">查看在线例子</a>)。</p> + +<pre class="brush: js">// Let us open our database +<span style="line-height: 1.5;">var DBOpenRequest = window.indexedDB.open("toDoList", 4); + +</span><span style="line-height: 1.5;">DBOpenRequest.onsuccess = function(event) {</span> + note.innerHTML += '<li>Database initialised.</li>'; + + // store the result of opening the database in the db variable. + // This is used a lot below + db = DBOpenRequest.result; + + // Run the addData() function to add the data to the database + addData(); +}; + +function addData() { + // Create a new object ready to insert into the IDB + var newItem = [ { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" } ]; + + // open a read/write db transaction, ready for adding the data + var transaction = db.transaction(["toDoList"], "readwrite"); + + // report on the success of the transaction completing, when everything is done + transaction.oncomplete = function(event) { + note.innerHTML += '<li>Transaction completed.</li>'; + }; + + transaction.onerror = function(event) { + note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>'; + }; + + // create an object store on the transaction + var objectStore = transaction.objectStore("toDoList"); + console.log(objectStore.keyPath); + + // Make a request to add our newItem object to the object store + var objectStoreRequest = objectStore.add(newItem[0]); + + objectStoreRequest.onsuccess = function(event) { + // report the success of our request + note.innerHTML += '<li>Request successful.</li>'; + }; +};</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + <tr> + <td>{{SpecName('IndexedDB', '#widl-IDBObjectStore-keyPath', 'keyPath')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbobjectstore-keypath", "keyPath")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div> +<div class="hidden">本页的兼容性列表是通过结构化数据生成的。如果你想贡献数据,请check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并给我们发pull request.</div> + +<p>{{Compat("api.IDBObjectStore.keyPath")}}</p> +</div> + +<h2 id="相关内容">相关内容</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">使用 IndexedDB</a></li> + <li>开始学习事务 transactions: {{domxref("IDBDatabase")}}</li> + <li>使用事务 transactions: {{domxref("IDBTransaction")}}</li> + <li>值域range的使用: {{domxref("IDBKeyRange")}}</li> + <li>检索、修改: {{domxref("IDBObjectStore")}}</li> + <li>使用游标: {{domxref("IDBCursor")}}</li> + <li>相关例子: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> +</ul> diff --git a/files/zh-cn/web/api/idbobjectstore/opencursor/index.html b/files/zh-cn/web/api/idbobjectstore/opencursor/index.html new file mode 100644 index 0000000000..764582ad9a --- /dev/null +++ b/files/zh-cn/web/api/idbobjectstore/opencursor/index.html @@ -0,0 +1,171 @@ +--- +title: IDBObjectStore.openCursor +slug: Web/API/IDBObjectStore/openCursor +translation_of: Web/API/IDBObjectStore/openCursor +--- +<p>{{ APIRef("IDBObjectStore") }}</p> + +<div> +<p>{{domxref("IDBObjectStore")}} 的 <code>openCursor()</code> 方法 返回一个{{domxref("IDBRequest")}} 对象,并在一个单独的线程中,返回一个新的 {{domxref("IDBCursorWithValue")}} 对象。此方法目的是用一个游标来遍历一个对象存储空间。</p> +</div> + +<p>若要确认此操作是否成功完成,请监听结果的 <code>success</code> 事件。</p> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">var request = ObjectStore.openCursor(query, direction);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>query {{optional_inline}}</dt> + <dd>要查询的键或者 {{domxref("IDBKeyRange")}} 。如果传一个有效的键,则会默认为只包含此键的范围。如果此参数不传值,则默认为一个选择了该对象存储空间全部记录的键范围。</dd> + <dt>direction {{optional_inline}}</dt> + <dd>一个 {{domxref("IDBCursorDirection")}} 来告诉游标要移动的方向。有效的值有 <code>"next"</code> 、<code>"nextunique"</code> 、<code>"prev"</code> 和 <code>"prevunique"</code>。默认值是 <code>"next"</code>。</dd> +</dl> + +<h3 id="返回">返回</h3> + +<p>一个 {{domxref("IDBRequest")}} 对象,在此对象上触发与此操作相关的后续事件。</p> + +<h3 id="异常">异常</h3> + +<p>此方法可能引起以下类型之一的 {{domxref("DOMException")}} :</p> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">异常</th> + <th scope="col">描述</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>InvalidStateError</code></td> + <td>此 {{domxref("IDBObjectStore")}} 或{{domxref("IDBIndex")}} 已被删除。</td> + </tr> + <tr> + <td><code>TransactionInactiveError</code></td> + <td>此 {{domxref("IDBObjectStore")}} 的事务处于非活动状态。</td> + </tr> + <tr> + <td><code><span style="line-height: normal;">DataError</span></code></td> + <td>指定的键或键范围无效。</td> + </tr> + </tbody> +</table> + +<h2 id="例子">例子</h2> + +<p>在下面这个简单的片段中,我们创建一个事务,检索一个对象存储空间,然后用一个游标去遍历该对象存储空间的所有记录:</p> + +<pre class="brush: js">var transaction = db.transaction("name", "readonly"); +var objectStore = transaction.objectStore("name"); +var request = objectStore.openCursor(); +request.onsuccess = function(event) { + var cursor = event.target.result; + if(cursor) { + // cursor.value 包含正在被遍历的当前记录 + // 这里你可以对 result 做些什么 + cursor.continue(); + } else { + // 没有更多 results + } +}; + +</pre> + +<h2 id="规范">规范 </h2> + +<dl> +</dl> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('IndexedDB', '#widl-IDBIndex-openCursor-IDBRequest-any-range-IDBCursorDirection-direction', 'openCursor()')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbobjectstore-opencursor", "openCursor()")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<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>23{{property_prefix("webkit")}}<br> + 24</td> + <td>10 {{property_prefix("moz")}}<br> + {{CompatGeckoDesktop("16.0")}}</td> + <td>10, partial</td> + <td>15</td> + <td>7.1</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("22.0")}}</td> + <td>1.0.1</td> + <td>10</td> + <td>22</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另请参阅">另请参阅</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li> + <li>Starting transactions: {{domxref("IDBDatabase")}}</li> + <li>Using transactions: {{domxref("IDBTransaction")}}</li> + <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li> + <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li> + <li>Using cursors: {{domxref("IDBCursor")}}</li> + <li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> +</ul> diff --git a/files/zh-cn/web/api/idbobjectstore/put/index.html b/files/zh-cn/web/api/idbobjectstore/put/index.html new file mode 100644 index 0000000000..368ea1d9a5 --- /dev/null +++ b/files/zh-cn/web/api/idbobjectstore/put/index.html @@ -0,0 +1,163 @@ +--- +title: IDBObjectStore.put() +slug: Web/API/IDBObjectStore/put +translation_of: Web/API/IDBObjectStore/put +--- +<p>{{ APIRef("IndexedDB") }}</p> + +<div> +<p> {{domxref("IDBObjectStore")}} 接口的 <strong><code>put()</code></strong> 方法更新一条给定的数据库记录,如果给出的值不存在,则插入一个新的记录</p> + +<p>它返回一个 {{domxref("IDBRequest")}} 对象,并且在一个单独的线程 ,创建一个值的 <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interfaces.html#structured-clone">structured clone</a> ,并且把它的值储存在对象仓库(object store)中. 当事务的模式是<code>readwrite时,</code>这个方法用来添加新的记录,或者更新一条对象仓库(object store)中已存在的记录 . 如果记录被成功储存, then a success event is fired on the returned request object with the <code>result</code> set to the key for the stored record, and the <code>transaction</code> set to the transaction in which this object store is opened.</p> +</div> + +<p>put方法是一个插入或更新对象仓库的方法. 参考仅用于插入的方法 {{domxref("IDBObjectStore.add")}} 方法.</p> + +<p>谨记,如果你有一条 {{domxref("cursor","IDBCursor")}} 记录想要更新, 使用{{domxref("IDBCursor.update()")}} 方法更新,比 {{domxref("IDBObjectStore.put()")}} 方法更合适. 这样做可以清楚地表明将更新现有记录,而不是插入新记录.</p> + +<p>{{AvailableInWorkers}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>request</em> = <em>objectStore</em>.put(<em>item</em>); +var <em>request</em> = <em>objectStore</em>.put(<em>item</em>, <em>key</em>);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>item</dt> + <dd>你想要更新 (或插入)的记录.</dd> + <dt>key {{optional_inline}}</dt> + <dd>你想要更新记录的主键 (e.g. from {{domxref("IDBCursor.primaryKey")}}). This is only needed for object stores that have an <code>autoIncrement</code> primary key, therefore the key is not in a field on the record object. In such cases, calling <code>put(item)</code> will always insert a new record, because it doesn't know what existing record you might want to modify.</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个 {{domxref("IDBRequest")}} 对象 ,在该对象上触发与此操作相关的后续事件。</p> + +<h3 id="异常">异常</h3> + +<p>This method may raise a {{domxref("DOMException")}} of one of the following types:</p> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Exception</th> + <th scope="col">Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>ReadOnlyError</code></td> + <td>The transaction associated with this operation is in read-only <a href="/en-US/docs/IndexedDB/IDBTransaction#mode_constants" title="/en-US/docs/IndexedDB/IDBTransaction#mode_constants">mode</a>.</td> + </tr> + <tr> + <td><code>TransactionInactiveError</code></td> + <td>This {{domxref("IDBObjectStore")}}'s transaction is inactive.</td> + </tr> + <tr> + <td><code>DataError</code></td> + <td> + <p>Any of the following conditions apply:</p> + + <ul> + <li>The object store uses in-line keys or has a key generator, and a key parameter was provided.</li> + <li>The object store uses out-of-line keys and has no key generator, and no key parameter was provided.</li> + <li>The object store uses in-line keys but no key generator, and the object store's key path does not yield a valid key.</li> + <li>The key parameter was provided but does not contain a valid key.</li> + </ul> + </td> + </tr> + <tr> + <td><code>InvalidStateError</code></td> + <td>The {{domxref("IDBObjectStore")}} has been deleted or removed.</td> + </tr> + <tr> + <td><code>DataCloneError</code></td> + <td>The data being stored could not be cloned by the internal structured cloning algorithm.<br> + </td> + </tr> + </tbody> +</table> + +<h2 id="参数_2">参数</h2> + +<dl> + <dt>value</dt> + <dd>被储存的值.</dd> + <dt>key</dt> + <dd>识别记录的键. 如果没有声明,那么记录键值将为空. 如果对象仓库有一个键生成器(e.g. autoincrement) ,必须传入key来更新对象.</dd> +</dl> + +<h2 id="Example">Example</h2> + +<p>The following example requests a given record title; when that request is successful the <code>onsuccess</code> function gets the associated record from the {{domxref("IDBObjectStore")}} (made available as <code>objectStoreTitleRequest.result</code><span style="line-height: 1.5;">), updates one property of the record, and then puts the updated record back into the object store in another request with <code>put()</code>. For a full working example, see our </span><a href="https://github.com/mdn/to-do-notifications/" style="line-height: 1.5;">To-do Notifications</a><span style="line-height: 1.5;"> app (</span><a href="http://mdn.github.io/to-do-notifications/" style="line-height: 1.5;">view example live</a><span style="line-height: 1.5;">.)</span></p> + +<pre class="brush: js" style="font-size: 14px;">var title = "Walk dog"; + +// Open up a transaction as usual +var objectStore = db.transaction(['toDoList'], "readwrite").objectStore('toDoList'); + +// Get the to-do list object that has this title as it's title +var objectStoreTitleRequest = objectStore.get(title); + +objectStoreTitleRequest.onsuccess = function() { + // Grab the data object returned as the result + var data = objectStoreTitleRequest.result; + + // Update the notified value in the object to "yes" + data.notified = "yes"; + + // Create another request that inserts the item back into the database + var updateTitleRequest = objectStore.put(data); + + <span style="font-size: 1rem;">// Log the transaction that originated this request</span> + console.log("The transaction that originated this request is " + updateTitleRequest.transaction); + + // When this new request succeeds, run the displayData() function again to update the display + updateTitleRequest.onsuccess = function() { + displayData(); + }; +};</pre> + +<h2 id="Specification"><span style="font-size: 2.14285714285714rem;">Specification</span></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', '#widl-IDBObjectStore-put-IDBRequest-any-value-any-key', 'put()')}}</td> + <td>{{Spec2('IndexedDB')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName("IndexedDB 2", "#dom-idbobjectstore-put", "put()")}}</td> + <td>{{Spec2("IndexedDB 2")}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2> + +<div> + + +<p>{{Compat("api.IDBObjectStore.put")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li> + <li>Starting transactions: {{domxref("IDBDatabase")}}</li> + <li>Using transactions: {{domxref("IDBTransaction")}}</li> + <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li> + <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li> + <li>Using cursors: {{domxref("IDBCursor")}}</li> + <li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li> +</ul> |