jqgrid:如何使用 indexeddb(客户端数据库)进行分页

Bri*_*ser 1 jqgrid indexeddb

我正在研究 indexeddb(本地客户端数据库)。我已经编写了 jqgrid 来呈现数据。我无法进行分页。

我的要求是:在 jqgrid 中,dataType 是本地的,因为它不是从服务器获取数据。而且我不想用 jqgrid 缓存所有记录。假设在索引数据库中有 100 条记录,并且在第一次加载时,我只想加载 10 条记录。当用户按下下一步按钮时,我应该从 indexedDB(这是客户端数据库)中获取接下来的 10 条记录并显示。

我可以从 indexeddb 获取数据,只有 jqgrid 有问题。

你能帮我么。

感谢和问候, Brijesh Baser

Jos*_*osh 6

indexedDB 不提供等效的 SQL 限制。停止迭代的唯一方法是维护一个计数器变量并检查它是否达到。像这样的东西:

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();
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

要进入下一页,您要使用 IDBCursor.prototype.advance,并传入要跳过的对象数量,例如 10。类似这样:

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);
    // ...
  }
}
Run Code Online (Sandbox Code Playgroud)