How do I iterate over an IndexedDB objectStore using a condition on a non-key property?

StackOverflow https://stackoverflow.com//questions/12689066

  •  12-12-2019
  •  | 
  •  

Question

I have a select box that I want to populate automatically with values from a objectStore, for that I need to iterate it like this: "Select COL1 from TABLE where COL2 = 'myvalue'", this is my code:

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

I have all my values correctly indexed in the objectStore, just don't now how to reach values through others values.

Était-ce utile?

La solution

Here is an example in which items are searched on non-indexed column, you need to go through all items and compare the values and add them to list, after that you can return the result set.

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

Autres conseils

This is an example with an index.

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(); 
         }
     } 
}; 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top