简体   繁体   English

如何使用条件,例如浏览器在indexdb中的子句

[英]How to use condition like where Clause in indexdb for browsers

I am using indexdb as it supports all browser. 我正在使用indexdb,因为它支持所有浏览器。 I have successfully added data and got data from indexdb but I want to use where clause condition. 我已经成功添加数据并从indexdb获取数据,但是我想使用where子句条件。 Like I have Product Name, Pathogen, Disease,Route so I want to get data where Product Name let's say is Ali and Disease is Skin. 就像我有产品名称,病原体,疾病,路线一样,所以我想获取产品名称为Ali和Disease是Skin的数据。 They way I have got from different sites what it does it show all the record with starting and end the value provided in the two input fields. 他们的方式是我从不同的站点得到的结果是,它以两个输入字段中提供的值开头和结尾显示所有记录。

Here is my code which I am using. 这是我正在使用的代码。

     <!doctype html>
     <html>
     <head>
     </head>
     <body>
     <script>
     var db;

    function indexedDBOk() {
    return "indexedDB" in window;
    }

    document.addEventListener("DOMContentLoaded", function() {

    //No support? Go in the corner and pout.
    if(!indexedDBOk) return;

    var openRequest = indexedDB.open("products",1);
    //var openRequest = indexedDB.open("idarticle_people5",1);


    openRequest.onupgradeneeded = function(e) {
    var thisDB = e.target.result;

    if(!thisDB.objectStoreNames.contains("products")) {
        var os = thisDB.createObjectStore("products", {autoIncrement:true});
        //I want to get by name later
        os.createIndex("name", "name", {unique:false});
        //I want email to be unique
        os.createIndex("pathogen", "pathogen", {unique:false});
        os.createIndex("disease", "disease", {unique:false});
        os.createIndex("route", "route", {unique:false});
    }
    }

    openRequest.onsuccess = function(e) {
    db = e.target.result;

    //Listen for add clicks
    document.querySelector("#addButton").addEventListener("click", addPerson, false);

    //Listen for get clicks
    document.querySelector("#getButton").addEventListener("click", getPerson, false);

    }   

    openRequest.onerror = function(e) {
    //Do something for the error
    }


   },false);


 function addPerson(e) {
 var name = document.querySelector("#name").value;
 var pathogen = document.querySelector("#pathogen").value;
 var disease = document.querySelector("#disease").value;
 var route = document.querySelector("#route").value;


console.log("About to add "+name+"/"+pathogen);

//Get a transaction
//default for OS list is all, default for type is read
var transaction = db.transaction(["products"],"readwrite");
//Ask for the objectStore
var store = transaction.objectStore("products");

//Define a person
var person = {
    name:name,
    pathogen:pathogen,
    disease:disease,
    route:route
}

//Perform the add
var request = store.add(person);

request.onerror = function(e) {
    alert("Sorry, that email address already exists.");
    console.log("Error",e.target.error.name);
    console.dir(e.target);
    //some type of error handler
}

request.onsuccess = function(e) {
    console.log("Woot! Did it");
 }
 }

function getPerson(e) {

var name = document.querySelector("#nameSearch").value; var name = document.querySelector(“#nameSearch”)。value;

var endname = document.querySelector("#diseaseSearch").value;

if(name == "" && endname == "") return;

var transaction = db.transaction(["products"],"readonly");
var store = transaction.objectStore("products");
var index = store.index("name");





//Make the range depending on what type we are doing
var range;
if(name != "" && endname != "") {
    range = IDBKeyRange.bound(name, endname);
    } else if(name == "") {
    range = IDBKeyRange.upperBound(endname);
    }   else {
    range = IDBKeyRange.lowerBound(name);
    }
    var s = "";

  index.openCursor(range).onsuccess = function(e) {
    var cursor = e.target.result;
    if(cursor) {
        s += "<h2>Key "+cursor.key+"</h2><p>";
        for(var field in cursor.value) {
            s+= field+"="+cursor.value[field]+"<br/>";
        }
        s+="</p>";
        cursor.continue();
    }
    document.querySelector("#status").innerHTML = s;
    } 

    }


    </script>

    <input type="text" id="name" placeholder="Name"><br/>
    <input type="text" id="pathogen" placeholder="Pathogen"><br/>
    <input type="text" id="disease" placeholder="Disease"><br/>
    <input type="text" id="route" placeholder="Route"><br/>

    <button id="addButton">Add Data</button>

     <p/>

   <input type="text" id="nameSearch" placeholder="Name"><br/>
   <input type="text" id="diseaseSearch" placeholder="Disease"><br/>
   <button id="getButton">Get By Name</button>

   <p/>

  <div id="status"></div>


 </body>
 </html>

You can try the following procedure with three parameters: 您可以使用以下三个参数尝试以下过程:

  • databaseid 数据库ID
  • tableid - storage name tableid-存储名称
  • callback - function, which returns array of selected data callback-函数,返回选定数据的数组
  • cond - conditional function. cond-条件函数。 returns true for ok records, and false for others. 对于正常记录,返回true;对于其他记录,返回false。

Here is the code: 这是代码:

function selectFromTable (databaseid, tableid, cb, cond) {
    var request = window.indexedDB.open(databaseid);
    request.onsuccess = function(event) {
        var res = [];
        var ixdb = event.target.result;
        var tx = ixdb.transaction([tableid]);
        var store = tx.objectStore(tableid);
        var cur = store.openCursor();
        cur.onblocked = function(event) {
    }
    cur.onerror = function(event) {
    }
    cur.onsuccess = function(event) {
        var cursor = event.target.result;
        if(cursor) {
            if(cond(cursor.value) ) res.push(cursor.value);
            cursor.continue();
        } else {
            ixdb.close();
            cb(res);
        }
    }
}       

Try using a browser database API adapter and read about its usage from its wiki page . 尝试使用浏览器数据库API适配器,从其Wiki页面中了解其用法。 You need to include all 3 JS files to get started. 您需要包括所有3个JS文件才能上手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM