- IndexedDB 教程
- IndexedDB - 首页
- IndexedDB - 介绍
- IndexedDB - 安装
- IndexedDB - 连接
- IndexedDB - 对象存储
- IndexedDB - 创建数据
- IndexedDB - 读取数据
- IndexedDB - 更新数据
- IndexedDB - 删除数据
- 使用 getAll() 函数
- IndexedDB - 索引
- IndexedDB - 范围
- IndexedDB - 事务
- IndexedDB - 错误处理
- IndexedDB - 检索
- IndexedDB - 游标
- IndexedDB - Promise 包装
- IndexedDB - Ecmascript 绑定
- IndexedDB 有用资源
- IndexedDB - 快速指南
- IndexedDB - 有用资源
- IndexedDB - 讨论
IndexedDB - 使用 getAll() 函数
在前面的部分中,我们一次只从存储中检索对象。现在我们可以检索所有数据或对象存储的子集。getAll 方法使用 getAll() 函数在对象存储中返回所有对象
语法
ObjectStore.getAll(optionalConstraint);
我们可以直接调用 getAll() 以返回存储在对象存储中的所有对象,或者我们可以指定一个可选约束,例如从汽车数据库中指定红色汽车
示例
在以下示例脚本中,我们调用 getAll() 方法以一次性返回存储在对象存储中的所有对象 −
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script>
const request = indexedDB.open("botdatabase",1);
request.onupgradeneeded = function(){
const db = request.result;
const store = db.createObjectStore("bots",{ keyPath: "id"});
store.createIndex("branch_db",["branch"],{unique: false});
}
request.onsuccess = function(){
document.write("database opened successfully");
const db = request.result;
const transaction=db.transaction("bots","readwrite");
const store = transaction.objectStore("bots");
const branchIndex = store.index("branch_db");
store.add({id: 1, name: "jason",branch: "IT"});
store.add({id: 2, name: "praneeth",branch: "CSE"});
store.add({id: 3, name: "palli",branch: "EEE"});
store.add({id: 4, name: "abdul",branch: "IT"});
store.put({id: 4, name: "deevana",branch: "CSE"});
const query = branchIndex.getAll(["IT"]);
query.onsuccess = function(){
document.write("query",query.result);
}
transaction.oncomplete = function(){
db.close;
}
}
</script>
</body>
</html>
输出
database opened successfully
query (1) [{...}]
arg1:(1) [{...}]
0:{id: 1, name: 'jason', branch: 'IT'}
length:1
[[Prototype]]:Array(0)
[[Prototype]]:Object
广告