Search code examples
jqgridindexeddb

jqgrid : How to do pagination with indexeddb (client side data base)


I am working on indexeddb (local client side data base). I have written the jqgrid to render the data. I am not able to do pagination.

My requirement is : in jqgrid, dataType is local because it is not fetching the data from server. And I don't wants to cache all records with jqgrid. let say in index db 100 records and on first load, I wants to load 10 records only. when user press next button, i should fetch the next 10 records from indexedDB (this is client side data base) and display.

I can able to fetch the data from indexeddb, only problem with jqgrid.

Can you please help me.

Thanks & Regards, Brijesh Baser


Solution

  • indexedDB does not provide an equivalent of SQL limit. The only way to stop iterating is to maintain a counter variable and check if it was reached. Something like this:

    var counter = 0;
    var limit = 10;
    function query() {
      db.transaction('').objectStore().openCursor().onsuccess = function(event) {
        var cursor = event.target.result;
        if(cursor) {
          var value = cursor.value;
          console.log(value);
          counter++;
          if(counter < limit) {
            // only continue if under limit
            cursor.continue();
          }
        }
      }
    }
    

    To go to the next page, you want to use IDBCursor.prototype.advance, and pass in the amount of objects to skip, such as 10. Something like this:

    function query() {
      var advanced = false;
      db.transaction('').objectStore().openCursor().onsuccess = function(event) {
        var cursor = event.target.result;
    
        if(!cursor) {
          return;
        }
    
        if(!advanced) {
          advanced = true;
          cursor.advance(10);
          return;
        }
    
        var value = cursor.value;
        console.log(value);
        // ...
      }
    }