Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 995
Ruben Teixeira Avatar asked Oct 02 '12 11:10

Ruben Teixeira


People also ask

How do I get all data from IndexedDB?

Using the IndexedDB API we have these 2 methods: getAll() and getAllKeys() with an usage example below: let transaction = this. db. transaction(["table"]); let object_store = transaction.

What is keyPath in IndexedDB?

In short, the indexName is how we want to the "field" or "column" (index) will be named in our "table" (Object Store) and the keyPath is the property of the stored object that contains the value of the field.

Is IndexedDB asynchronous?

Operations performed using IndexedDB are done asynchronously, so as not to block applications.

Can I store object in IndexedDB?

It lets you store just about anything in the user's browser. In addition to the usual search, get, and put actions, IndexedDB also supports transactions. Here is the definition of IndexedDB on MDN: IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs.


1 Answers

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
        };
    }
}
like image 100
Deni Spasovski Avatar answered Nov 02 '22 23:11

Deni Spasovski