IndexedDB
key是只读属性,返回在游标中的位置。如果游标在范围之外,这个值会被置为 undefined。游标的 key 可以是任何数据类型。
值
任意类型
示例
在该示例中,我们创建一个事务,检索一个存储对象,然后使用游标遍历所有存储在 object store 中的记录。遍历的过程中,我们把类似(相簿标题,这是我们的键 key),游标的 key 打印出来。
我们可以不根据游标的 key 来选取数据;我们可以抓取所有。还要注意,在循环的每个迭代中,你可以使用 cursor.value.foo 从当前记录下获取数据。完整示例,请看IDBCursor example (view example live.)
function displayData() {
const transaction = db.transaction(["rushAlbumList"], "readonly");
const objectStore = transaction.objectStore("rushAlbumList");
objectStore.openCursor().onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
const listItem = document.createElement("li");
listItem.textContent = `${cursor.value.albumTitle} ${cursor.value.year}`;
list.appendChild(listItem);
console.log(cursor.key);
cursor.continue();
} else {
console.log("已列出所有条目。");
}
};
}
规范
浏览器兼容性
相关链接
- Using IndexedDB
- Starting transactions: IDBDatabase
- Using transactions: IDBTransaction
- Setting a range of keys: IDBKeyRange
- Retrieving and making changes to your data: IDBObjectStore
- Using cursors: IDBCursor
- Reference example: To-do Notifications (view example live.)
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
