store.createIndex('nametimestamp', ['text', 'timeStamp']);
//text and timestamp are my field names
字符串 然后将其用于搜索功能:
searchIndexedDB = function (value, callback) {
var request = indexedDB.open(dbName);
request.onsuccess = function(e) {
var db = e.target.result;
var trans = db.transaction(objectStoreName, 'readonly');
var store = trans.objectStore(objectStoreName);
var index = store.index('nametimestamp');
var keyRange = IDBKeyRange.bound([value,0], [value, new Date().getTime()]);
//open the index for all dates and for only one value
var openCursorRequest = index.openCursor(keyRange, 'prev');
//open the database sorted by time stamp - descending
openCursorRequest.onsuccess = function(e) {
if(e.target.result)
callback(e.target.result.value);
};
trans.oncomplete = function(e) {
db.close();
};
openCursorRequest.onerror = function(e) {
console.log("Error Getting: ", e);
};
};
request.onerror = myStorage.indexedDB.onerror;
}
1条答案
按热度按时间6qqygrtg1#
答案非常相似,你需要创建一个compound index,然后从索引中以相反的顺序检索数据。在这个例子中,我使用时间戳存储日期。
首先需要在
onupgradeneeded
事件期间创建复合索引:字符串
然后将其用于搜索功能:
型