IDBKeyRange
接口在
IndexedDB API
represents a continuous interval over some data type that is used for keys. Records can be retrieved from
IDBObjectStore
and
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.
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 bounded ; if it has no bounds, it is unbounded . 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:
| 范围 | 代码 |
|---|---|
| All keys ≥ x |
IDBKeyRange.lowerBound
(
x
)
|
| All keys > x |
IDBKeyRange.lowerBound
(
x
, true)
|
| All keys ≤ y |
IDBKeyRange.upperBound
(
y
)
|
| All keys < y |
IDBKeyRange.upperBound
(
y
, true)
|
| All keys ≥ x && ≤ y |
IDBKeyRange.bound
(
x
,
y
)
|
| All keys > x &&< y |
IDBKeyRange.bound
(
x
,
y
, true, true)
|
| All keys > x && ≤ y |
IDBKeyRange.bound
(
x
,
y
, true, false)
|
| All keys ≥ x &&< y |
IDBKeyRange.bound
(
x
,
y
, false, true)
|
| The key = z |
IDBKeyRange.only
(
z
)
|
A key is in a key range if the following conditions are true:
undefined
lowerOpen
is
false
.
undefined
upperOpen
is
false
.
IDBKeyRange.lower
只读
Lower bound of the key range.
IDBKeyRange.upper
只读
Upper bound of the key range.
IDBKeyRange.lowerOpen
只读
Returns false if the lower-bound value is included in the key range.
IDBKeyRange.upperOpen
只读
Returns false if the upper-bound value is included in the key range.
IDBKeyRange.bound()
Creates a new key range with upper and lower bounds.
IDBKeyRange.only()
Creates a new key range containing a single value.
IDBKeyRange.lowerBound()
Creates a new key range with only a lower bound.
IDBKeyRange.upperBound()
Creates a new upper-bound key range.
IDBKeyRange.includes()
Returns a boolean indicating whether a specified key is inside the key range.
The following example illustrates how you'd use a key range. Here we declare a
keyRangeValue
as a range between values of "A" and "F". We open a transaction (using
IDBTransaction
) and an object store, and open a Cursor with
IDBObjectStore.openCursor
, declaring
keyRangeValue
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
IDBKeyRange.bound("A", "F", true, true);
, then the range would not include "A" and "F", only the values between them.
注意 : For a more complete example allowing you to experiment with key range, have a look at our IDBKeyRange-example repo ( view the example live too )。
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 = '<strong>' + cursor.value.fThing + '</strong>, ' + cursor.value.fRating;
list.appendChild(listItem);
cursor.continue();
} else {
console.log('Entries all displayed.');
}
};
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
索引数据库 API 2.0
The definition of 'IDBKeyRange' in that specification. |
推荐 | 初始定义。 |
|
索引数据库 API 草案
The definition of 'IDBKeyRange' in that specification. |
推荐 |
添加
includes()
.
|
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
IDBKeyRange
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
bound
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
包括
|
Chrome 52 | Edge ≤18 | Firefox 47 | IE ? | Opera 39 | Safari 10.1 | WebView Android 52 | Chrome Android 52 | Firefox Android Yes | Opera Android 41 | Safari iOS 10.3 | Samsung Internet Android 6.0 |
lower
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
lowerBound
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
lowerOpen
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
only
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
upper
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
upperBound
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
upperOpen
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
| Available in workers | Chrome Yes | Edge ≤18 | Firefox 37 | IE ? | Opera Yes | Safari ? | WebView Android Yes | Chrome Android Yes | Firefox Android 37 | Opera Android Yes | Safari iOS ? | Samsung Internet Android Yes |
完整支持
部分支持
兼容性未知
要求使用供应商前缀或不同名称。
IDBDatabase
IDBTransaction
IDBKeyRange
IDBObjectStore
IDBCursor
IDBKeyRange
IDBCursor
IDBCursorSync
IDBCursorWithValue
IDBDatabase
IDBDatabaseException
IDBDatabaseSync
IDBEnvironment
IDBEnvironmentSync
IDBFactory
IDBFactorySync
IDBIndex
IDBIndexSync
IDBObjectStore
IDBObjectStoreSync
IDBOpenDBRequest
IDBRequest
IDBTransaction
IDBTransactionSync
IDBVersionChangeEvent
IDBVersionChangeRequest