
- 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
廣告