Home:ALL Converter>IndexedDB wildcard at start and end of searchterm

IndexedDB wildcard at start and end of searchterm

Ask Time:2013-08-09T16:07:54         Author:Anonymoose

Json Formatter

This topic explains how to use wildcards ad the end of the searchterm using IndexedDB.

I am looking for a way to add a wildcard at the end AND at the start of the searchterm. In SQL it would be: LIKE '%SearchTerm%'.

How can I achieve this with IndexedDB? Here is my code:

function getMaterials() {
    var materialNumber = $("#input").val();
    var transaction = db.transaction(["materials"]);
    var objectStore = transaction.objectStore("materials");

    var request = objectStore.openCursor(IDBKeyRange.bound(materialNumber, materialNumber + '\uffff'), 'prev');
    $("#output").find("tr:gt(0)").remove();

    request.onsuccess = function (event) {

        var cursor = event.target.result;
        if (cursor) {
            var newRow = '<tr><td>'+ cursor.value.materialNumber +'</td>'+
                 '<td>'+ cursor.value.description +'</td>'+
                 '<td>'+ cursor.value.pieces +'</td>'+
                 '<td>'+ cursor.value.price +'</td></tr>';

            $('#output').append(newRow);

            cursor.continue();
        }
    };
};

EDIT:

I could achieve this by letting indexDB return all rows and then narrow down in JavaScript. But there must be a better approach in terms of performance.

if (cursor.value.materialNumber.indexOf(materialNumber) != -1){
     //add result...
}

Author:Anonymoose,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18142504/indexeddb-wildcard-at-start-and-end-of-searchterm
yy