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 プロパティは、現在のインデックスが参照するオブジェクトストアを返します。
メモ: この機能はウェブワーカー内で利用可能です。
値
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①> | 
ブラウザーの互換性
Loading…
関連情報
- IndexedDB の使用
- トランザクションの開始: IDBDatabase
- トランザクションの使用: IDBTransaction
- キー範囲の設定: IDBKeyRange
- データの取得と変更: IDBObjectStore
- カーソルの使用: IDBCursor
- リファレンス例: To-do Notifications (動く例を見る)