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 badges
Add a ment  | 

2 Answers 2

Reset to default 8

I 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