在indexedDB中按数字/日期排序的意外行为 [英] Unexpected behavior sorting by number/date in indexedDB

查看:529
本文介绍了在indexedDB中按数字/日期排序的意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的数据库,我在 created_at 上创建了一个名为 time 的索引:

For my databse i've created an index called time on created_at:

var os = thisDB.createObjectStore(name, { keyPath :  "id" });
os.createIndex("time", "created_at", {unique: false });

正如您所见,我将时间保存为unix整数:

As you can see i save the time as a unix integer:

entry['created_at'] = Date.new(entry['created_at']).unix();
var request = sto.add(entry);

因此,在控制台的最后,数据如下所示:

So that at end in the console the data looks like this:

我的问题是如何在方向创建的 asc desc 中获取所有数据:

My question is how i can get all data in asc or desc created at direction:

因为当我用光标检查数据时:

Because when i go through the data with a cursor:

function open_cursor(){
  var objectStore = ENTRYDB.transaction('entries').objectStore('entries').index('time');
  var request = objectStore.openCursor();
  request.onsuccess = function(event) {handle_open(event)};
}
function handle_open(evt){
  var cursor = evt.target.result;
  if (cursor) {
    console.log(cursor.value);
    cursor.continue();
  }
}

它没有按时创建排序!这是随机的!谢谢

Its not sorted by the created at time! It is random! THANKS

推荐答案

您可以通过为openCursor函数提供第二个参数来对升序/降序进行排序:index.openCursor(undefined,下一个);或者prev用于降序。

You can sort ascending/descending in the openCursor function by giving it the second parameter: index.openCursor(undefined, "next"); or "prev" for descending.

排序应该对索引起作用,当你没有提供next或prev(next是默认值)时也是如此。当您在索引上调用openCursor时,它会以与chrome-dev-tools在调试器中单击time索引时显示它们相同的方式循环它们。

Sorting should work on the index, also when you do not provide a next or prev (next is default). When you call openCursor on the index, it loops them in the same way chrome-dev-tools shows them when clicking the "time" index in the debugger.

你是任何机会使用垫片(可能是iOS / android)? ( https://github.com/axemclion/IndexedDBShim ),因为这是一个已知的问题排序没有'真的在那里工作。如果您不确定,我猜这段不适用于您..

Are you using the shim by any chance (maybe for iOS/android)? (https://github.com/axemclion/IndexedDBShim), because it is a known issue sorting doesn't really work there. If you aren't sure then I guess this paragraph doesn't apply to you..

编辑您可能没有使用垫片,因为我在chrome dev-tools中看到了indexedDb的截图,你在使用垫片时无法做到,因为它使用不同的存储机制。)

EDIT You are problably not using the shim because I see a screenshot of the indexedDb from chrome dev-tools, wich you could not have done when using the shim as it uses a different storage mechanism).

这篇关于在indexedDB中按数字/日期排序的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆