IDBIndex: objectStore-Eigenschaft

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2021.

Hinweis: Diese Funktion ist in Web Workers verfügbar.

Die objectStore-Eigenschaft des IDBIndex-Interfaces gibt den Objektspeicher zurück, der durch den aktuellen Index referenziert wird.

Wert

Beispiele

Im folgenden Beispiel öffnen wir eine Transaktion und einen Objektspeicher und holen uns dann den Index lName aus einer einfachen Kontaktdatenbank. Anschließend öffnen wir einen grundlegenden Cursor auf dem Index mit IDBIndex.openCursor. Dies funktioniert genauso wie das direkte Öffnen eines Cursors auf einem ObjectStore mittels IDBObjectStore.openCursor, außer dass die zurückgegebenen Datensätze basierend auf dem Index und nicht auf dem Primärschlüssel sortiert sind.

Der aktuelle Objektspeicher wird in die Konsole protokolliert und sollte etwas zurückgeben wie:

json
IDBObjectStore { name: "contactsList", keyPath: "id", indexNames: DOMStringList[7], transaction: IDBTransaction, autoIncrement: false }

Abschließend iterieren wir durch jeden Datensatz und fügen die Daten in eine HTML-Tabelle ein. Für ein vollständiges funktionierendes Beispiel, siehe unser IndexedDB-Beispiel-Demo-Repo (Live-Demo anzeigen).

js
function displayDataByIndex() {
  tableEntry.textContent = "";
  const transaction = db.transaction(["contactsList"], "readonly");
  const objectStore = transaction.objectStore("contactsList");

  const myIndex = objectStore.index("lName");
  console.log(myIndex.objectStore);

  myIndex.openCursor().onsuccess = (event) => {
    const cursor = event.target.result;
    if (cursor) {
      const tableRow = document.createElement("tr");
      for (const cell of [
        cursor.value.id,
        cursor.value.lName,
        cursor.value.fName,
        cursor.value.jTitle,
        cursor.value.company,
        cursor.value.eMail,
        cursor.value.phone,
        cursor.value.age,
      ]) {
        const tableCell = document.createElement("td");
        tableCell.textContent = cell;
        tableRow.appendChild(tableCell);
      }
      tableEntry.appendChild(tableRow);

      cursor.continue();
    } else {
      console.log("Entries all displayed.");
    }
  };
}

Spezifikationen

Specification
Indexed Database API 3.0
# ref-for-dom-idbindex-objectstore①

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
objectStore

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch