如何使用非键属性上的条件迭代IndexedDB objectStore?

时间:2012-10-02 11:01:36

标签: javascript html5 indexeddb

我有一个选择框,我想用objectStore中的值自动填充,因为我需要像这样迭代:"从TABLE中选择COL1,其中COL2 =' myvalue'& #34 ;, 这是我的代码:

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中正确索引了所有值,但现在不知道如何通过其他值来获取值。

2 个答案:

答案 0 :(得分:5)

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

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
        };
    }
}

答案 1 :(得分:3)

这是一个带索引的例子。

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(); 
         }
     } 
};