IDBIndex: keyPath-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 keyPath-Eigenschaft des IDBIndex-Interfaces gibt den key path des aktuellen Indexes zurück. Wenn dieser null ist, wird dieser Index nicht automatisch ausgefüllt.

Wert

Jeder Datentyp, der als key path verwendet werden kann.

Beispiele

Im folgenden Beispiel öffnen wir eine Transaktion und einen Objektspeicher und holen den Index lName aus einer einfachen Kontaktdatenbank. Wir öffnen dann einen einfachen Cursor auf dem Index mit IDBIndex.openCursor — dies funktioniert genauso wie das Öffnen eines Cursors direkt auf einem ObjectStore mithilfe von IDBObjectStore.openCursor, außer dass die zurückgegebenen Datensätze basierend auf dem Index und nicht dem Primärschlüssel sortiert werden.

Der key path des aktuellen Indexes wird in der Konsole ausgegeben: Er sollte als lName zurückgegeben werden.

Schließlich iterieren wir durch jeden Datensatz und fügen die Daten in eine HTML-Tabelle ein. Ein vollständiges Arbeitsbeispiel finden Sie in unserem IndexedDB-examples Demo-Repo (Beispiel live ansehen).

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

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

  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
# dom-idbindex-keypath

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
keyPath

Legend

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

Full support
Full support

Siehe auch