值
鍵範圍的上限(可以是任何型別)。
示例
以下示例說明了如何使用鍵範圍。在此,我們宣告 keyRangeValue = IDBKeyRange.upperBound("F", "W", true, true); — 一個包含“F”和“W”之間所有內容但**不**包含它們本身的範圍 — 因為上下界都被宣告為開區間(true)。我們開啟一個事務(使用 IDBTransaction)和一個物件儲存,並使用 IDBObjectStore.openCursor 開啟一個遊標,將 keyRangeValue 宣告為其可選的鍵範圍值。
宣告鍵範圍後,我們將它的 upper 屬性值記錄到控制檯,它應該顯示為“W”。
注意:有關一個更完整的示例,允許您嘗試鍵範圍,請檢視我們的 IDBKeyRange-example 倉庫(也可以 線上檢視示例)。
js
function displayData() {
const keyRangeValue = IDBKeyRange.bound("F", "W", true, true);
console.log(keyRangeValue.upper);
const transaction = db.transaction(["fThings"], "readonly");
const objectStore = transaction.objectStore("fThings");
objectStore.openCursor(keyRangeValue).onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
const listItem = document.createElement("li");
listItem.textContent = `${cursor.value.fThing}, ${cursor.value.fRating}`;
list.appendChild(listItem);
cursor.continue();
} else {
console.log("Entries all displayed.");
}
};
}
規範
| 規範 |
|---|
| Indexed Database API 3.0 # ref-for-dom-idbkeyrange-upper① |
瀏覽器相容性
載入中…
另見
- 使用 IndexedDB
- 開始事務:
IDBDatabase - 使用事務:
IDBTransaction - 設定鍵的範圍:
IDBKeyRange - 檢索和修改資料:
IDBObjectStore - 使用遊標:
IDBCursor - 參考示例:待辦事項通知(檢視即時示例)。