admin 管理员组文章数量: 1086019
I have an IndexedDB data store with a few hundred objects in it. I'd like to grab items 40-59 from it based on the ordering in one of my indexes on that store. Is there a way to do that without simply calling cursor.continue() 39 times before starting to consume data? It seems pretty wasteful in terms of processing time.
I have an IndexedDB data store with a few hundred objects in it. I'd like to grab items 40-59 from it based on the ordering in one of my indexes on that store. Is there a way to do that without simply calling cursor.continue() 39 times before starting to consume data? It seems pretty wasteful in terms of processing time.
Share Improve this question asked Mar 2, 2011 at 6:10 Ben DiltsBen Dilts 10.7k18 gold badges60 silver badges86 bronze badges2 Answers
Reset to default 8I had the same problem and cursor.advance(40)
is what you want to use.
One thing that took me a while to figure out so may be useful to others is if you want to advance the cursor and iterate through the results you will either need to call them in separate openCursor().onsuccess
handlers, or implement some kind of tracking to prevent them both being called in the same request or an InvalidStateError
exception with be thrown. This can be done like so:
Separate Handlers
// advance first
store.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
cursor.advance(40);
};
// then iterate
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
cursor.continue();
});
Same Handler
// create flag for advancing
var advancing = true;
store.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
// advancing
if (advancing === true) {
cursor.advance(40);
// set advancing flag to false so we don't advance again
advancing = false;
}
// continuing
else {
cursor.continue();
}
}
Spec reference: http://www.w3/TR/IndexedDB/#widl-IDBCursor-advance-void-unsigned-long-count MDN Reference with example: https://developer.mozilla/en-US/docs/Web/API/IDBCursor.advance
I believe you can call cursor.advance(40)
Spec reference: http://www.w3/TR/IndexedDB/#widl-IDBCursor-advance
本文标签: javascriptPaging with IndexedDB cursorStack Overflow
版权声明:本文标题:javascript - Paging with IndexedDB cursor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744011615a2518257.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论