blob: 833c8e02abfa50c59be1b86abf03c418ed9c5d91 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
---
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>
</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 = '<strong>' + cursor.value.fThing + '</strong>, ' + cursor.value.fRating;
list.appendChild(listItem);
cursor.continue();
} else {
console.log('Entries all displayed.');
}
};
};</pre>
<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>
|