Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用IndexedDB进行过滤和排序_Javascript_Indexeddb - Fatal编程技术网

Javascript 使用IndexedDB进行过滤和排序

Javascript 使用IndexedDB进行过滤和排序,javascript,indexeddb,Javascript,Indexeddb,我正在寻找一些关于如何过滤的建议,同时使用不同的键从IndexedDB中获得排序结果 条件是: IndexedDB中有两个键,一个用于过滤,一个用于排序 在查询时,过滤键值是已知的,但是分隔排序键值的值可能是未知的 排序键值可能包含随机数字和字母 我的解决方案包括两件事: 具有两个键的复合索引:筛选键和排序键。在我的示例中,我按名称筛选并按id排序: objectStore.createIndex('nameId',['name','id'],{unique:true}) 在查询中,我使用该索

我正在寻找一些关于如何过滤的建议,同时使用不同的键从IndexedDB中获得排序结果

条件是:

  • IndexedDB中有两个键,一个用于过滤,一个用于排序
  • 在查询时,过滤键值是已知的,但是分隔排序键值的值可能是未知的
  • 排序键值可能包含随机数字和字母
  • 我的解决方案包括两件事:

    • 具有两个键的复合索引:筛选键和排序键。在我的示例中,我按名称筛选并按id排序:

      objectStore.createIndex('nameId',['name','id'],{unique:true})

    • 在查询中,我使用该索引的上下限。由于“0”追加,因此不是一个优雅的解决方案:

      objectStore.index('nameId').openCursor(window.IDBKeyRange.bound(['John'],['John'+'0'],true,false))

    正如预期的那样,在我的示例中返回:

     1. {"name":"John","id":13}
     2. {"name":"John","id":75}
     3. {"name":"John","id":77}
     4. {"name":"John","id":78}
     5. {"name":"John","id":88}
     6. {"name":"John","id":98}
     7. {"name":"John","id":99}
    
    有没有更优雅的解决方案可以达到同样的效果

    完整示例:

    window.indexedDB=window.indexedDB | | window.mozIndexedDB | | window.webkitIndexedDB | | window.msIndexedDB;
    window.IDBTransaction=window.IDBTransaction | | window.webkitibdransaction | | window.msIDBTransaction;
    window.IDBKeyRange=window.IDBKeyRange | | window.webkitIDBKeyRange | | window.msIDBKeyRange;
    如果(!window.indexedDB){
    alert('您的浏览器不支持IndexedDB的稳定版本');
    }
    功能日志(文本){
    var div=document.createElement('div');
    div.textContent=文本;
    文件.正文.附件(div);
    }
    函数addData(objectStore){
    objectStore.put({name:'Paul',id:45});
    objectStore.put({name:'Paul',id:56});
    objectStore.put({name:'Paul',id:43});
    put({name:'Paul',id:36});
    objectStore.put({name:'Paul',id:10});
    put({name:'Paul',id:93});
    objectStore.put({name:'John',id:78});
    put({name:'John',id:77});
    objectStore.put({name:'John',id:75});
    objectStore.put({name:'John',id:98});
    objectStore.put({name:'John',id:88});
    objectStore.put({name:'John',id:13});
    objectStore.put({name:'John',id:99});
    put({name:'Patrick',id:34});
    put({name:'Patrick',id:23});
    put({name:'Patrick',id:12});
    put({name:'Patrick',id:87});
    put({name:'Patrick',id:02});
    }
    var请求=window.indexedDB.open('Test',1);
    request.onsuccess=函数(事件){
    var db=event.target.result,
    objectStore=db.transaction('customers')。objectStore('customers'),
    cursor=objectStore.index('nameId').openCursor(window.IDBKeyRange.bound(['John'],['John'+'0'],true,false));
    cursor.onsuccess=函数(事件){
    var cursor=event.target.result;
    如果(光标){
    log(JSON.stringify(cursor.value));
    cursor.continue();
    }
    };
    };
    request.onupgradeneeded=函数(事件){
    var db=event.target.result,
    objectStore=db.createObjectStore('customers',{keyPath:'id'});
    createIndex('nameId',['name','id'],{unique:true});
    objectStore.transaction.oncomplete=函数(事件){
    var transaction=db.transaction('customers','readwrite'),
    objectStore=transaction.objectStore(“客户”);
    addData(objectStore);
    };
    
    };如果不在代码中进行排序,这可能是唯一的解决方案。是的,我忘了提到它,但另一个条件是在获取后不使用JavaScript进行排序或过滤。感谢@kristofedegaveindexeddb只允许一个索引用于筛选和排序。剩下的都是你自己做的。也许有一点提示,如果您需要排序或过滤更多内容,您可以使用webworkers,这样您就不会阻塞UI threadsure,有许多事情可以在代码中完成,但我希望这与IndexedDB的API严格相关。谢谢这就是indexeddb过滤和排序的方式。在IDB中,除了可以并行迭代另一个游标之外,没有其他东西了。您可以使用
    \uFFFF
    而不是
    0
    ['John','\uFFFF']