Indexeddb OpenCursor在特定键值上返回null

时间:2015-03-04 10:22:31

标签: javascript indexeddb

我正在使用Indexeddb,我有一个名为Items_S1和KeyPath的对象库:“ItemID” 我在objectstore中有一些订单,包括ORDERID,ItemID,ItemName,Qty,Rate和NetAmount。再次订购相同的项目,它增加了它的数量..现在的问题是,当我点击任何记录旁边的按钮,以减少其数量没有任何事情发生...相同的代码正在另一个项目,但在这里,我有问题..我使用OpenCursor()但它返回null。有人请帮助我,这是代码:

$("#cart_menu").on("click", "#deselect", function(){

    var thisId = $(this).parent().parent().data("key");
    var transaction = db.transaction(["Item_S1"], "readwrite");  
    var objectstore = transaction.objectStore("Item_S1");

    objectstore.openCursor(thisId).onsuccess = function(e){
        var cursor = e.target.result;
        if(cursor){
            if(Number(cursor.value.ItemQty) > 1) {
                objectstore.put({
                        OrderID: cursor.value.OrderID,
                        ItemID: cursor.value.ItemID,
                        ItemName: cursor.value.ItemName,
                        ItemQty: Number(cursor.value.ItemQty) - 1,
                        ItemRate: cursor.value.ItemRate ,
                        ItemAmnt: (Number(cursor.value.ItemQty) - 1) * Number(cursor.value.ItemRate),
                        ItemID: cursor.value.ItemID
                });
            } else {
                objectstore.delete(cursor.value.ItemID);
            }
        }
    };

    transaction.oncomplete = function(){
         displayMenuItem();
    }; 

});

1 个答案:

答案 0 :(得分:1)

我得到了一个问题的解决方案...我为objectstore创建了索引然后使用了IDBKeyRange.only()方法,它运行得很完美..这是代码,

$(“#cart_menu”)。on(“click”,“#rightselect”,function(){

var thisId = $(this).parent().parent().data("key");
var transaction = db.transaction(["Item_S1"], "readwrite");  
var objectstore = transaction.objectStore("Item_S1");
var indexvalue = objectstore.index("By_ItemID");

requestindex = indexvalue.openCursor(IDBKeyRange.only(thisId.toString()));
requestindex.onsuccess = function(){
var cursor = requestindex.result;
if(cursor){

if(Number(cursor.value.ItemQty) > 1)
    {
    objectstore.put({OrderID: cursor.value.OrderID, ItemID: cursor.value.ItemID, ItemName: cursor.value.ItemName, ItemQty: Number(cursor.value.ItemQty) - 1, ItemRate: cursor.value.ItemRate , ItemAmnt: (Number(cursor.value.ItemQty) - 1) * Number(cursor.value.ItemRate), ItemID: cursor.value.ItemID});
    }
    else{
    objectstore.delete(cursor.value.ItemID);
    }
    Total = Number($("#Text1").val()) - Number(cursor.value.ItemRate);
        $("#Text1").val(Total);
}


};

transaction.oncomplete = function(){
     displayMenuItem();
     }; 

});