qwe*_*wsx 5 javascript search indexeddb
我正在尝试在我的网站上设置搜索功能,我正在尝试查找IndexedDB代码
SELECT "column" FROM "table" WHERE "column" LIKE "%keyword%"
我在IndexedDB模糊搜索中找到了一个解决方案
db.transaction(['table'], 'readonly')
.objectStore('table')
.openCursor(IDBKeyRange.bound(keyword, keyword + '\uffff'), 'prev')
.onsuccess = function (e) {
e || (e = event);
var cursor = e.target.result;
if (cursor) {
console.log(cursor.value.column);
cursor.continue();
}
};
Run Code Online (Sandbox Code Playgroud)
但我怎样才能找到"%keyword%"而不是"%keyword"?
IndexedDB 中没有任何类似于 SQL WHERE 的东西。
我将循环遍历表/对象存储值并查看当前游标列是否包含关键字:
var keyword = "foo";
var transaction = db.transaction("table", "readwrite");
var objectStore = transaction.objectStore("table");
var request = objectStore.openCursor();
request.onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
if (cursor.value.column.indexOf(keyword) !== -1) {
console.log("We found a row with value: " + JSON.stringify(cursor.value));
}
cursor.continue();
}
};
Run Code Online (Sandbox Code Playgroud)