- 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 - 遊標
我們使用 get() 函式來檢索資料,當時我們知道想要檢索哪個鍵,但如果我們想要逐步處理物件儲存的所有值的,我們可以使用遊標。
首先我們使用 open cursor 函式,然後我們可以向其新增引數。我們可以插入 openCursor() 函式的引數有 -
- 使用鍵範圍限制物件範圍
- 我們想要進行迭代的方向
以下是遊標的語法
語法
ObjectStore.openCursor(optionalKeyRange, optionalDirection);
對於物件儲存,我們使用 openCursor()
optionalKeyRange - 我們可以限制我們需要的檢索的物件範圍。
optionalDirection - 我們可以指定我們想要進行迭代的方向。
範例 1
此範例中,我們學習如何使用 JavaScript 開啟遊標函式 -
var objectStore = db.transaction("student").objectStore("student”);
objectStore.openCursor().onsuccess = event => {
var cursor = event.target.result;
if (cursor) {
document.write("Name" + cursor.key + cursor.value.name);
cursor.continue();
} else {
document.write("entries closed");
}
};
範例 2
當我們想要從物件儲存中檢索所有物件並將其放入陣列中時。
var student = [];
objectStore.openCursor().onsuccess = event => {
var cursor = event.target.result;
if (cursor) {
student.push(cursor.value);
cursor.continue();
} else {
document.write(student);
}
};
範例 3
以下列出了在 JavaScript 中執行 openCursor() 函式的另一個範例 -
var singleKeyRange = IDBKeyRange.only("Jason");
var lowerBoundKeyRange = IDBKeyRange.lowerBound("Praneeth");
var lowerBoundOpenKeyRange = IDBKeyRange.lowerBound("jason", true);
var upperBoundOpenKeyRange = IDBKeyRange.upperBound("praneeth", true);
var boundKeyRange = IDBKeyRange.bound("jason", "praneeth", false, true);
index.openCursor(boundKeyRange).onsuccess = event => {
var cursor = event.target.result;
if (cursor) {
cursor.continue();
}
};
或者,如果我們想要指定方向 -
objectStore.openCursor(boundKeyRange, "prev").onsuccess = event => {
var cursor = event.target.result;
if (cursor) {
cursor.continue();
}
};
HTML 範例
執行遊標函式用途的 HTML 指令碼如下 -
<!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 req = store.openCursor();
req.onsuccess = function(){
const cursor = req.result;
if(cursor){
const key = cursor.key;
const value = cursor.value;
document.write(key,value);
cursor.continue();
} else {
document.write("bots completed");
}
}
transaction.oncomplete = function(){
db.close;
}
}
</script>
</body>
</html>
輸出
database opened successfully
1 {id: 1, name: 'jason', branch: 'IT'}
2 {id: 2, name: 'praneeth', branch: 'CSE'}
3 {id: 3, name: 'palli', branch: 'EEE'}
4 {id: 4, name: 'deevana', branch: 'CSE'}
bots completed
廣告