[英]Is there any way to retrieve random row from indexeddb
I want to retrieve a random row from the table of meals, how is the way to do that? 我想从餐桌上检索一个随机行,这样做的方法是什么?
My code : 我的代码:
var transaction = db.transaction(["meals"], "readonly");
var store = transaction.objectStore("meals");
var index = store.index("time"); // to search in the field time type
range = IDBKeyRange.only(3); // 3 means it is a lunch
index.openCursor(range).onsuccess = function (e) {
var dt = event.target.result;
if (dt) {
var s = dt.value['fno1'];
}
};
Instead of advancing one row at a time until you hit your random result, what about using advance(n) to jump up a random set? 不是一次推进一行直到你达到随机结果,而是使用advance(n)来跳过随机集? Here is a complete example.
这是一个完整的例子。 It assumes two buttons to seed data and to call the random selection.
它假设有两个按钮来播种数据并调用随机选择。 I'm going to be blogging this Monday.
我本周一要写博客。
/* global $,document,indexedDB,console */
/**
* Returns a random integer between min and max
* Using Math.round() will give you a non-uniform distribution!
*/
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
$(document).ready(function() {
var db;
var openRequest = indexedDB.open("randomidb",1);
openRequest.onupgradeneeded = function(e) {
var thisDB = e.target.result;
console.log("running onupgradeneeded");
if(!thisDB.objectStoreNames.contains("notes")) {
thisDB.createObjectStore("notes", {autoIncrement:true});
}
};
openRequest.onsuccess = function(e) {
console.log("running onsuccess");
db = e.target.result;
$("#seedButton").on("click", function() {
var store = db.transaction(["notes"],"readwrite").objectStore("notes");
for(var i=0; i<10; i++) {
var note = {
title:"Just a random note: "+getRandomInt(1,99999),
created:new Date()
};
var request = store.add(note);
request.onerror = function(e) {
console.log("Error",e.target.error.name);
//some type of error handler
};
request.onsuccess = function(e) {
console.log("Woot! Did it");
};
}
});
$("#randomButton").on("click", function() {
//success handler, could be passed in
var done = function(ob) {
console.log("Random result",ob);
};
//ok, first get the count
var store = db.transaction(["notes"],"readonly").objectStore("notes");
store.count().onsuccess = function(event) {
var total = event.target.result;
var needRandom = true;
console.log("ok, total is "+total);
store.openCursor().onsuccess = function(e) {
var cursor = e.target.result;
if(needRandom) {
var advance = getRandomInt(0, total-1);
console.log("going up "+advance);
if(advance > 0) {
needRandom = false;
cursor.advance(advance);
} else {
done(cursor);
}
} else {
done(cursor);
}
};
};
});
};
});
OK, I have developed this solution and it works just perfect to retrieve random row from table: 好的,我已经开发了这个解决方案,它非常适合从表中检索随机行:
var transaction = db.transaction(["meals"], "readonly");
var store = transaction.objectStore("meals"); // name of table
var index = store.index("time"); // time is name of field and it is a number
range = IDBKeyRange.only(2); // query when time = 2
var y = 1;
var z = true;
var x = 0; // it will equal the random number
index.openCursor(range).onsuccess = function (e) {
var dt = event.target.result;
if (z) {
x = RandInt(1, dt.key); // get random number between 1 and count of rows
z = false; // to only make the above line one time only
}
if (dt) {
if (x == y) {
var s = dt.value['fno1'];
}
else
{ y += 1; dt.continue();}
}
};
Function to get the random number between two values : 函数获取两个值之间的随机数:
function RandInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.