aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/idbkeyrange
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/api/idbkeyrange')
-rw-r--r--files/zh-cn/web/api/idbkeyrange/index.html193
-rw-r--r--files/zh-cn/web/api/idbkeyrange/lowerbound/index.html126
2 files changed, 319 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/idbkeyrange/index.html b/files/zh-cn/web/api/idbkeyrange/index.html
new file mode 100644
index 0000000000..32481c3a02
--- /dev/null
+++ b/files/zh-cn/web/api/idbkeyrange/index.html
@@ -0,0 +1,193 @@
+---
+title: IDBKeyRange
+slug: Web/API/IDBKeyRange
+tags:
+ - API
+ - Database
+ - IDBKeyRange
+ - IndexedDB
+ - Interface
+ - NeedsTranslation
+ - Reference
+ - Storage
+ - TopicStub
+translation_of: Web/API/IDBKeyRange
+---
+<p>{{APIRef("IndexedDB")}}</p>
+
+<div>
+<p>The <strong><code>IDBKeyRange</code></strong> interface of the <a href="/en/IndexedDB" title="en/IndexedDB">IndexedDB API</a> represents a continuous interval over some data type that is used for keys. Records can be retrieved from {{domxref("IDBObjectStore")}} and {{domxref("IDBIndex")}} objects using keys or a range of keys. You can limit the range using lower and upper bounds. For example, you can iterate over all values of a key in the value range A–Z.</p>
+</div>
+
+<p>A key range can be a single value or a range with upper and lower bounds or endpoints. If the key range has both upper and lower bounds, then it is <em>bounded</em>; if it has no bounds, it is <em>unbounded</em>. A bounded key range can either be open (the endpoints are excluded) or closed (the endpoints are included). To retrieve all keys within a certain range, you can use the following code constructs:</p>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Range</th>
+ <th scope="col">Code</th>
+ </tr>
+ <tr>
+ <td>All keys ≤ <strong>x</strong></td>
+ <td>{{domxref("IDBKeyRange.upperBound")}}<code>(<strong>x</strong>)</code></td>
+ </tr>
+ <tr>
+ <td>All keys &lt; <strong>x</strong></td>
+ <td>{{domxref("IDBKeyRange.upperBound")}}<code>(<strong>x</strong>, true) </code></td>
+ </tr>
+ <tr>
+ <td>All keys ≥<strong> y</strong></td>
+ <td>{{domxref("IDBKeyRange.lowerBound")}}<code>(<strong>y</strong>)</code></td>
+ </tr>
+ <tr>
+ <td>All keys &gt;<strong> y</strong></td>
+ <td>{{domxref("IDBKeyRange.lowerBound")}}<code>(<strong>y</strong>, true)</code></td>
+ </tr>
+ <tr>
+ <td>All keys ≥ <strong>x</strong> &amp;&amp; ≤ <strong>y</strong></td>
+ <td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>)</code></td>
+ </tr>
+ <tr>
+ <td>All keys &gt; <strong>x</strong> &amp;&amp;&lt; <strong>y</strong></td>
+ <td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>, true, true)</code></td>
+ </tr>
+ <tr>
+ <td>All keys &gt; <strong>x</strong> &amp;&amp; ≤ <strong>y</strong></td>
+ <td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>, true, false)</code></td>
+ </tr>
+ <tr>
+ <td>All keys ≥ <strong>x</strong> &amp;&amp;&lt; <strong>y</strong></td>
+ <td>{{domxref("IDBKeyRange.bound")}}<code>(<strong>x</strong>, <strong>y</strong>, false, true)</code></td>
+ </tr>
+ <tr>
+ <td>The key = <strong>z</strong></td>
+ <td>{{domxref("IDBKeyRange.only")}}<code>(<strong>z</strong>)</code></td>
+ </tr>
+ </thead>
+</table>
+
+<p>A key is in a key range if the following conditions are true:</p>
+
+<ul>
+ <li>The lower value of the key range is one of the following:
+ <ul>
+ <li><code>undefined</code></li>
+ <li>Less than key value</li>
+ <li>Equal to key value if <code>lowerOpen</code> is <code>false</code>.</li>
+ </ul>
+ </li>
+ <li>The upper value of the key range is one of the following:
+ <ul>
+ <li><code>undefined</code></li>
+ <li>Greater than key value</li>
+ <li>Equal to key value if <code>upperOpen</code> is <code>false</code>.</li>
+ </ul>
+ </li>
+</ul>
+
+<p>{{AvailableInWorkers}}</p>
+
+<h2 id="Properties">Properties</h2>
+
+<dl>
+ <dt>{{domxref("IDBKeyRange.lower")}} {{readonlyInline}}</dt>
+ <dd>Lower bound of the key range.</dd>
+ <dt>{{domxref("IDBKeyRange.upper")}} {{readonlyInline}}</dt>
+ <dd>Upper bound of the key range.</dd>
+ <dt>{{domxref("IDBKeyRange.lowerOpen")}} {{readonlyInline}}</dt>
+ <dd>Returns false if the lower-bound value is included in the key range.</dd>
+ <dt>{{domxref("IDBKeyRange.upperOpen")}} {{readonlyInline}}</dt>
+ <dd>Returns false if the upper-bound value is included in the key range.</dd>
+</dl>
+
+<h2 id="Methods">Methods</h2>
+
+<h3 id="Static_methods">Static methods</h3>
+
+<dl>
+ <dt>{{domxref("IDBKeyRange.bound()")}}</dt>
+ <dd>Creates a new key range with upper and lower bounds.</dd>
+ <dt>{{domxref("IDBKeyRange.only()")}}</dt>
+ <dd>Creates a new key range containing a single value.</dd>
+ <dt>{{domxref("IDBKeyRange.lowerBound()")}}</dt>
+ <dd>Creates a new key range with only a lower bound.</dd>
+ <dt>{{domxref("IDBKeyRange.upperBound()")}}</dt>
+ <dd>Creates a new upper-bound key range.</dd>
+</dl>
+
+<h3 id="Instance_methods">Instance methods</h3>
+
+<dl>
+ <dt>{{domxref("IDBKeyRange.includes()")}}</dt>
+ <dd>Returns a boolean indicating whether a specified key is inside the key range.</dd>
+</dl>
+
+<h2 id="Examples">Examples</h2>
+
+<p>The following example illustrates how you'd use a key range. Here we declare a <code>keyRangeValue</code> as a range between values of "A" and "F". We open a transaction (using {{domxref("IDBTransaction")}}) and an object store, and open a Cursor with {{domxref("IDBObjectStore.openCursor")}}, declaring <code>keyRangeValue</code> as its optional key range value. This means that the cursor will only retrieve records with keys inside that range. This range includes the values "A" and "F", as we haven't declared that they should be open  bounds. If we used <span style="background-color: #fafbfc; font-family: consolas,monaco,andale mono,monospace; font-size: 1rem; line-height: 19px; white-space: pre;">IDBKeyRange.bound("A", "F", true, true);</span>, then the range would not include "A" and "F", only the values between them.</p>
+
+<div class="note">
+<p><strong>Note</strong>: For a more complete example allowing you to experiment with key range, have a look at our <a href="https://github.com/mdn/indexeddb-examples/tree/master/idbkeyrange">IDBKeyRange-example</a> repo (<a href="https://mdn.github.io/indexeddb-examples/idbkeyrange/">view the example live too</a>.)</p>
+</div>
+
+<pre class="brush: js">function displayData() {
+ var keyRangeValue = IDBKeyRange.bound("A", "F");
+
+ var transaction = db.transaction(['fThings'], 'readonly');
+ var objectStore = transaction.objectStore('fThings');
+
+ objectStore.openCursor(keyRangeValue).onsuccess = function(event) {
+ var cursor = event.target.result;
+ if(cursor) {
+ var listItem = document.createElement('li');
+ listItem.innerHTML = '&lt;strong&gt;' + cursor.value.fThing + '&lt;/strong&gt;, ' + cursor.value.fRating;
+ list.appendChild(listItem);
+
+ cursor.continue();
+ } else {
+ console.log('Entries all displayed.');
+ }
+ };
+}</pre>
+
+<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-IDBKeyRange', 'IDBKeyRange')}}</td>
+ <td>{{Spec2('IndexedDB')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('IndexedDB 2', '#keyrange', 'IDBKeyRange')}}</td>
+ <td>{{Spec2('IndexedDB 2')}}</td>
+ <td>Adds <code>includes()</code>.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
+
+<div>
+
+
+<p>{{Compat("api.IDBKeyRange")}}</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/idbkeyrange/lowerbound/index.html b/files/zh-cn/web/api/idbkeyrange/lowerbound/index.html
new file mode 100644
index 0000000000..8a9457d202
--- /dev/null
+++ b/files/zh-cn/web/api/idbkeyrange/lowerbound/index.html
@@ -0,0 +1,126 @@
+---
+title: IDBKeyRange.lowerBound()
+slug: Web/API/IDBKeyRange/lowerBound
+translation_of: Web/API/IDBKeyRange/lowerBound
+---
+<p>{{ APIRef("IndexedDB") }}</p>
+
+<div>
+<p>{{domxref("IDBKeyRange")}} 接口的<strong><code>lowerBound()</code></strong>方法创建一个只有下限的新的键范围。默认情况下,它包含较低的端点值。</p>
+</div>
+
+<p>{{AvailableInWorkers}}</p>
+
+<h2 id="Syntax">Syntax</h2>
+
+<pre class="syntaxbox" style="font-size: 14px;">var <em>myIDBKeyRange</em> = <em>IDBKeyRange</em>.lowerBound(<em>lower</em>);
+var <em>myIDBKeyRange</em> = <em>IDBKeyRange</em>.lowerBound(<em>lower</em>, <em>open</em>);
+</pre>
+
+<h3 id="Parameters">Parameters</h3>
+
+<dl>
+ <dt>lower </dt>
+ <dd>指定新键范围的下限。</dd>
+ <dt>open<em> </em>{{optional_inline}}</dt>
+ <dd>指示下限是否排除端点值。默认值为false。</dd>
+</dl>
+
+<h3 id="Return_value">Return value</h3>
+
+<p>{{domxref("IDBKeyRange")}}: 新创建的键范围.</p>
+
+<h3 id="Exceptions">Exceptions</h3>
+
+<p>此方法可能引发以下类型的 {{domxref("DOMException")}} :</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;">DataError</span></font></td>
+ <td>
+ <p>The value parameter passed was not a valid key.</p>
+
+ <div></div>
+ </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Example">Example</h2>
+
+<p>下面的示例演示如何使用下限键范围。在这里,我们声明<code>keyRangeValue = IDBKeyRange.lowerBound("F", false);</code>— 一个包含值“F”及其后所有内容的范围。我们打开一个事务(使用 {{domxref("IDBTransaction")}})和一个对象存储,并使用 {{domxref("IDBObjectStore.openCursor")}}打开一个游标,声明<code>keyRangeValue</code> 为其可选的键范围值。这意味着光标将只检索键值为“F”及其后面的所有记录。如果使用<code>IDBKeyRange.lowerBound("F", true);</code>,则该范围将不包括端点“F”,仅包括其后面的值。</p>
+
+<div class="note">
+<p><strong>Note</strong>: 要获得一个更完整的示例,使您能够使用键范围进行实验,请查看我们的 <a href="https://github.com/mdn/indexeddb-examples/tree/master/idbkeyrange">IDBKeyRange-example</a>(<a href="https://mdn.github.io/indexeddb-examples/idbkeyrange/">实时查看该示例</a>)。</p>
+</div>
+
+<pre class="brush: js" style="font-size: 14px;">function displayData() {
+ var keyRangeValue = IDBKeyRange.lowerBound("F");
+
+ var transaction = db.transaction(['fThings'], 'readonly');
+ var objectStore = transaction.objectStore('fThings');
+
+ objectStore.openCursor(keyRangeValue).onsuccess = function(event) {
+ var cursor = event.target.result;
+ if(cursor) {
+ var listItem = document.createElement('li');
+ listItem.innerHTML = '&lt;strong&gt;' + cursor.value.fThing + '&lt;/strong&gt;, ' + cursor.value.fRating;
+ list.appendChild(listItem);
+
+ cursor.continue();
+ } else {
+ console.log('Entries all displayed.');
+ }
+ };
+ };</pre>
+
+<div></div>
+
+<h2 id="Specification">Specification</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-IDBKeyRange-lowerBound-IDBKeyRange-any-lower-boolean-open', 'lowerBound()')}}</td>
+ <td>{{Spec2('IndexedDB')}}</td>
+ <td></td>
+ </tr>
+ <tr>
+ <td>{{SpecName("IndexedDB 2", "#dom-idbkeyrange-lowerbound-lower-open-lower", "lowerBound()")}}</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.IDBKeyRange.lowerBound")}}</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>