blob: 72f4f7c5d16493b6b94a62ced1d187f20ebe264c (
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
|
---
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>
{{Compat("api.IDBObjectStore.openCursor")}}
<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>
|