IDBKeyRange: upper 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

注意:此功能在 Web Workers 中可用。

IDBKeyRange 介面的只讀屬性 upper 返回鍵範圍的上限。

鍵範圍的上限(可以是任何型別)。

示例

以下示例說明了如何使用鍵範圍。在此,我們宣告 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①

瀏覽器相容性

另見