假设我在indexdb中有一个表,它有两列 - 名称和工资,我在两者上都有单独的索引。
现在我已经使用像这样的索引名称上的游标和范围获得了结果 -
var value ="ujjwal";
var trans = db.transaction(['employee'], "readonly");
var store = trans.objectStore('employee');
var index = store.index('name');
var cursorRequest = index.openCursor(IDBKeyRange.only(value)),
res = new Array();
cursorRequest.onsuccess = function(e) {
var cursor = e.target.result;
if (cursor) {
res.push(cursor.value);
cursor.continue();
}
};
所以现在我得到了多个结果。我怎样才能按工资对它们进行排序。
我想使用IndexedD执行此操作。我已经在工资列上编制索引。
答案 0 :(得分:0)
根据您的评论编辑,然后输入:
function myOnUpgradeNeeded(event) {
var db = event.target.result;
var store = db.createObjectStore('employee');
store.createIndex('name-salary-index', ['salary', 'name']);
}
function findByNameOrderBySalary(db, name, callback) {
var tx = db.transaction('employee');
var store = tx.objectStore('employee');
var index = store.index('name-salary-index');
var lowerBound = [Number.NEGATIVE_INFINITY, name];
var upperBound = [Number.POSITIVE_INFINITY, name];
var range = IDBKeyRange.bound(lowerBound, upperBound);
var request = index.getAll(range);
request.onsuccess = function(event) {
var employeeArray = event.target.result;
callback(employeeArray);
};
request.onerror = function(event) {
console.log('Error occurred', event.target.error);
callback([]);
};
}