7

我有一个选择框,我想用来自 objectStore 的值自动填充,为此我需要像这样迭代它:“从 TABLE 中选择 COL1,其中 COL2 = 'myvalue'”,这是我的代码:

var db;
var req = indexedDB.open("DB");
req.onsuccess = function (e) {
    db = req.result;
    var tran = db.transaction("store");         
    var singleKeyRange = IDBKeyRange.only("myvalue"); //the value I want to reach from COL2         
    tran.objectStore("store").openCursor(singleKeyRange).onsuccess = function(e) { 
        var cursor = e.target.result;
        if (cursor) { 
            var opt = document.getElementById("selectbox");
            var option = document.createElement("option");
            var optionText=document.createTextNode(cursor.value.COL1); //the matching values in COL1
            option.appendChild(optionText);
            opt.appendChild(option);
            cursor.continue();
        }
    }
};

我在 objectStore 中正确索引了所有值,只是现在不知道如何通过其他值来获取值。

4

2 回答 2

6

这是一个在非索引列上搜索项目的示例,您需要遍历所有项目并比较值并将它们添加到列表中,然后您可以返回结果集。

function SearchItems( keyPath, value, requestNo, callback){
    var initOpenReq = indexedDB.open(baseName);
    initOpenReq.onsuccess = function() {
    var db = initOpenReq.result;
        var transaction = db.transaction(objectStoreName, 'readonly');
        var objectStore = transaction.objectStore(objectStoreName);
        var cursorRequest = objectStore.openCursor();
        var agregate = [];
        cursorRequest.onsuccess = function (event){
            if (event.target.result){
                if(event.target.result.value[keyPath] && event.target.result.value[keyPath] == value){ //compare values
                    agregate.push(event.target.result.value);
                }
                event.target.result['continue']();
            }
        };

        transaction.oncomplete = function (event) {
                callback(agregate); // return items
        };
    }
}
于 2012-10-02T13:36:34.723 回答
4

这是一个带有索引的示例。

var db; 
var req = indexedDB.open("DB", 2);
// Creating the index
req.onupgradeneeded = function (e){
   var trans = e.target.transaction;
   var obj = trans.objectStore("store");
   obj.createIndex("indexname", "keypath") // keypath is the propertyname you want the index on. for Ex. {Name: "x", Age:5 }; If you want to filter on Name, keypath = Name
} 
req.onsuccess = function (e) {
    db = req.result;
   var tran = db.transaction("store");
   var singleKeyRange = IDBKeyRange.only("myvalue"); //the value I want to reach from COL2              
   var objectStore = tran.objectStore("store");
   var opt = document.getElementById("selectbox");

   objectStore.index("indexname").openCursor(singleKeyRange).onsuccess = 
   function(e){          
         var cursor = e.target.result;
         if (cursor) {
             var option = document.createElement("option");
             var optionText=document.createTextNode(cursor.value.COL1); //the matching values in COL1             
             option.appendChild(optionText);
             opt.appendChild(option);
             cursor.continue(); 
         }
     } 
}; 
于 2012-10-03T05:15:21.800 回答