Skip to Content
0

In shopping cart project cart items are removed when i refresh the page

Feb 21, 2017 at 01:22 PM

61

avatar image
Former Member

Hi,

I am doing shopping cart project when i add the products to cart the products are removed when i refresh the page.But, cart items should never remove when i refresh the page, if anybody knows please comment your answers.

My code is like this

createContent: function()

{

var oView = sap.ui.view({

id: "app",

viewName: "shoppingcart2.App",

type: "JS",

viewData: {component: this}

});

var oModel = new sap.ui.model.odata.ODataModel("/sap/opu/odata/SAP/ZPROJECT_SHOPPING_CART_SRV",true,"username", "password"); sap.ui.getCore().setModel(oModel,'products'); oView.setModel(oModel, 'products');

var data = {

items: []

}

var oCartModel = new sap.ui.model.json.JSONModel(data); oView.setModel(oCartModel,'cart');

addToCart: function(e,obj)

{ var model = obj.getModel("model2");

var data = model.oData;

var cart = obj.getModel('cart').getData();

console.log(cart);

var exist = false;

$.each(cart.items, function(i, obj)

{

if(obj.Pid == data.Pid){

exist = true, cart.items[i].Pquan += 1; }

});

if(!exist)

{

cart.items.push({ Pid: data.Pid, Pname: data.Pname, Pquan: 1,

Pfilename: data.Pfilename, Pprice:data.Pprice, Pdesc:data.Pdesc

});

}

obj.getModel('cart').setData(cart);

sap.m.MessageToast.show('Item added in cart');

}

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Srikanth KV Feb 21, 2017 at 06:01 PM
0

There is no persistent data storage in your code. Are you storing the newly added item to database?

After refresh the code tries to retrieve data from JSON model which is created new after every refresh.

Save the data to a DB or local browser storage

Show 3 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Srikath sir,

I am new to ui5, i dont know to how to add cart items to odata databse, if you have any idea please share your code.

0

There is lot of documentation in SCN blogs, I would suggest to go through them.

0
Former Member
Srikanth KV

Useful blogs...Thank you Srikanth sir.

0