javascript 对 indexedDB 查询的结果进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11492062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting the results of an indexedDB query
提问by vivek.m
I want to sort results obtained from indexedDB.
Each record has structure {id, text, date} where 'id' is the keyPath.
我想对从 indexedDB 获得的结果进行排序。
每条记录都有结构 {id, text, date},其中 'id' 是 keyPath。
I want to sort the results by date.
我想按日期对结果进行排序。
My current code is as below:
我目前的代码如下:
var trans = db.transaction(['msgs'], IDBTransaction.READ);
var store = trans.objectStore('msgs');
// Get everything in the store;
var keyRange = IDBKeyRange.lowerBound("");
var cursorRequest = store.openCursor(keyRange);
cursorRequest.onsuccess = function(e) {
var result = e.target.result;
if(!!result == false){
return;
}
console.log(result.value);
result.continue();
};
采纳答案by vivek.m
Thanks to zomg, hughfdHymanson of javascript irc, I sorted the final array. Modified code as below:
感谢 zomg,javascript irc 的 hughfdHymanson,我对最终的数组进行了排序。修改后的代码如下:
var trans = db.transaction(['msgs'], IDBTransaction.READ);
var store = trans.objectStore('msgs');
// Get everything in the store;
var keyRange = IDBKeyRange.lowerBound("");
var cursorRequest = store.openCursor(keyRange);
var res = new Array();
cursorRequest.onsuccess = function(e) {
var result = e.target.result;
if(!!result == false){
**res.sort(function(a,b){return Number(a.date) - Number(b.date);});**
//print res etc....
return;
}
res.push(result.value);
result.continue();
};
回答by Kyaw Tun
Actually you have to index the date
field in the msgs
objectStore and open an index cursor on the objectStore.
实际上,您必须对objectStore 中的date
字段进行索引,并在msgs
objectStore 上打开索引游标。
var cursorRequest = store.index('date').openCursor(null, 'next'); // or prev
This will get the sorted result. That is how indexes are supposed to be used.
这将得到排序结果。这就是应该如何使用索引。
回答by Andrei Cojocaru
Here's the more efficient way suggested by Josh.
这是 Josh 建议的更有效的方法。
Supposing you created an index on "date":
假设您在“日期”上创建了一个索引:
// Use the literal "readonly" instead of IDBTransaction.READ, which is deprecated:
var trans = db.transaction(['msgs'], "readonly");
var store = trans.objectStore('msgs');
var index = store.index('date');
// Get everything in the store:
var cursorRequest = index.openCursor();
// It's the same as:
// var cursorRequest = index.openCursor(null, "next");
// Or, if you want a "descendent ordering":
// var cursorRequest = index.openCursor(null, "prev");
// Note that there's no need to define a key range if you want all the objects
var res = new Array();
cursorRequest.onsuccess = function(e) {
var cursor = e.target.result;
if (cursor) {
res.push(cursor.value);
cursor.continue();
}
else {
//print res etc....
}
};
More on cursor direction here: http://www.w3.org/TR/IndexedDB/#cursor-concept
更多关于光标方向的信息:http: //www.w3.org/TR/IndexedDB/#cursor-concept
IDBIndex API is here: http://www.w3.org/TR/IndexedDB/#idl-def-IDBIndex
IDBIndex API 在这里:http: //www.w3.org/TR/IndexedDB/#idl-def-IDBIndex