IndexedDB paging

I have an IndexedDB datastore with several hundred objects. I would like to take items 40-59 from him based on an order in one of my indices in this store. Is there a way to do this without simply calling cursor.continue () 39 times before starting to use the data? It looks pretty wasteful in terms of processing time.

+6
javascript html5
source share
2 answers

I had the same problem, and cursor.advance(40) is what you want to use.

One thing that took me a while to figure this out can be useful for others, if you want to move the cursor and openCursor().onsuccess over the results, you will need to call them in separate openCursor().onsuccess or implement some kind of tracking to both of them were thrown in one request or an InvalidStateError exception which should be thrown. This can be done like this:

Individual 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(); } } 

Specification: http://www.w3.org/TR/IndexedDB/#widl-IDBCursor-advance-void-unsigned-long-count MDN reference with an example: https://developer.mozilla.org/en-US/docs /Web/API/IDBCursor.advance

+6
source share

I think you can call cursor.advance(40)

Specification: http://www.w3.org/TR/IndexedDB/#widl-IDBCursor-advance

+2
source share

All Articles