← 返回首页
IDBIndex: objectStore プロパティ - Web API | MDN

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

IDBIndex: objectStore プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.

IDBIndex インターフェイスの objectStore プロパティは、現在のインデックスが参照するオブジェクトストアを返します。

メモ: この機能はウェブワーカー内で利用可能です。

In this article

IDBObjectStore です。

以下の例では、トランザクションとオブジェクトストアを開き、シンプルな連絡先データベースからインデックス lName を取得します。そして、このインデックスで IDBIndex.openCursor により基本的なカーソルを開きます。これは、返されるレコードが主キーではなくこのインデックスに基づいてソートされる以外、ObjectStore で直接 IDBObjectStore.openCursor を用いてカーソルを開くのと同じように動きます。

現在のオブジェクトストアをコンソールに記録します。これは以下のようなものになるはずです。

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

最後に、各レコードを走査し、データを HTML テーブルに挿入します。動く例全体は、IndexedDB-examples デモレポジトリーを参照してください。(動く例を見る)

js
function displayDataByIndex() { tableEntry.innerHTML = ""; 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"); tableRow.innerHTML = `<td>${cursor.value.id}</td>` + `<td>${cursor.value.lName}</td>` + `<td>${cursor.value.fName}</td>` + `<td>${cursor.value.jTitle}</td>` + `<td>${cursor.value.company}</td>` + `<td>${cursor.value.eMail}</td>` + `<td>${cursor.value.phone}</td>` + `<td>${cursor.value.age}</td>`; tableEntry.appendChild(tableRow); cursor.continue(); } else { console.log("全エントリーを表示しました。"); } }; }

仕様書

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

ブラウザーの互換性

Enable JavaScript to view this browser compatibility table.

関連情報