localStorageメモリ値取値およびアクセスJSON、およびhtml 5 localStorageベースのカート

6354 ワード

localStorage.setItem("key","value");//      key,  value   
 
localStorage.key = "value"//      key,  value   
 
localStorage.getItem("key");//       key  www.it165.net
 
localStorage.key;//       key  
 
localStorage.removeItem("key")//      key     
//    localStorage     。      JSON     。
var students = 
{
	liyang:{name:"liyang",age:17},

	lilei:{name:"lilei",age:18}

}//    JSON  


students = JSON.stringify(students);// JSON        

localStorage.setItem("students",students);// localStorage          
//      JSON  ,                  students    
 
var students = localStorage.getItem("students");//  students  
 
students = JSON.parse(students);//       JSON  
 
//         students JSON   
utils = {
    setParam : function (name,value){
        localStorage.setItem(name,value)
    },
    getParam : function(name){
        return localStorage.getItem(name)
    }
}

product={
    id:0,
    name:"",
    num:0,
    price:0.00
};
orderdetail={
    username:"",
    phone:"",
    address:"",
    zipcode:"",
    totalNumber:0,
    totalAmount:0.00    
}
cart = {
    //         
    addproduct: function (product) {
        var ShoppingCart = utils.getParam("ShoppingCart");
        if (ShoppingCart == null || ShoppingCart == "") {
            //       
            var jsonstr = { "productlist": [{ "id": product.id, "name": product.name, "num": product.num, "price": product.price}], "totalNumber": product.num, "totalAmount": (product.price * product.num) };
            utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr));
        } else {
            var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length));
            var productlist = jsonstr.productlist;
            var result = false;
            //            
            for (var i in productlist) {
                if (productlist[i].id == product.id) {
                    productlist[i].num = parseInt(productlist[i].num) + parseInt(product.num);
                    result = true;
                }
            }
            if (!result) {
                //           
                productlist.push({ "id": product.id, "name": product.name, "num": product.num, "price": product.price });
            }
            //      
            jsonstr.totalNumber = parseInt(jsonstr.totalNumber) + parseInt(product.num);
            jsonstr.totalAmount = parseFloat(jsonstr.totalAmount) + (parseInt(product.num) * parseFloat(product.price));
            orderdetail.totalNumber = jsonstr.totalNumber;
            orderdetail.totalAmount = jsonstr.totalAmount;
            //     
            utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr));
        }
    },
    //        
    updateproductnum: function (id, num) {
        var ShoppingCart = utils.getParam("ShoppingCart");
        var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length));
        var productlist = jsonstr.productlist;

        for (var i in productlist) {
            if (productlist[i].id == id) {
                jsonstr.totalNumber = parseInt(jsonstr.totalNumber) + (parseInt(num) - parseInt(productlist[i].num));
                jsonstr.totalAmount = parseFloat(jsonstr.totalAmount) + ((parseInt(num) * parseFloat(productlist[i].price)) - parseInt(productlist[i].num) * parseFloat(productlist[i].price));
                productlist[i].num = parseInt(num);
                orderdetail.totalNumber = jsonstr.totalNumber;
                orderdetail.totalAmount = jsonstr.totalAmount;
                utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr));
                return;
            }
        }
    },
    //           
    getproductlist: function () {
        var ShoppingCart = utils.getParam("ShoppingCart");
        var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length));
        var productlist = jsonstr.productlist;
        orderdetail.totalNumber = jsonstr.totalNumber;
        orderdetail.totalAmount = jsonstr.totalAmount;
        return productlist;
    },
    //            
    existproduct: function (id) {
        var result = false;
        var ShoppingCart = utils.getParam("ShoppingCart");
        if (ShoppingCart != null) {
            var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length));
            var productlist = jsonstr.productlist;
            for (var i in productlist) {
                if (productlist[i].id == id) {
                    result = true;
                }
            }
        }
        return result;
    },
    //        
    deleteproduct: function (id) {
        var ShoppingCart = utils.getParam("ShoppingCart");
        var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length));
        var productlist = jsonstr.productlist;
        var list = [];
        for (var i in productlist) {
            if (productlist[i].id == id) {
                jsonstr.totalNumber = parseInt(jsonstr.totalNumber) - parseInt(productlist[i].num);
                jsonstr.totalAmount = parseFloat(jsonstr.totalAmount) - parseInt(productlist[i].num) * parseFloat(productlist[i].price);
            } else {
                list.push(productlist[i]);
            }
        }
        jsonstr.productlist = list;
        orderdetail.totalNumber = jsonstr.totalNumber;
        orderdetail.totalAmount = jsonstr.totalAmount;
        utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr));
    }
};
//     html5 localStorage    JS      
var product =
{
	'id': id,        //         ,        
	'name': 'hhh',
	'num':jq('#text-4').val(),
	'price':199.9
};
//        
cart.addproduct(product);
var productlist=cart.getproductlist();//       
alert('', '  :'+productlist[0].id+' '+productlist[0].name+' '+productlist[0].num+' '+productlist[0].price, '  ');