I am customizing the OnlinePOS system to meet my task but i don't
understand the Ajax Callback function, I can see it is very beneficial in
updating values without a page refresh.
In the highlighted part in the view below, when i click the *plus *& *minus
*links the quantity value gets updated without page refresh,
What i want to achieve is that
*1. When the quantity gets updated i want the total figure to also update
with quantity automatically *because presently i ma using a jQuery page
reload function which is not ideal*,*
*2. *There is also a <form> with an input field where i enter the paying
amount, *what i want is that when i click submit button there shouldnt be a
page refresh but just have the paying amount displayed and the calculations
done without the page reloading*, currently I get a page refresh!
How can i achieve these??
Below is my code:
*VIEW:*
{{extend 'layout.html'}}
<script>
$(document).ready(function(){
$('#reload2').click(function()
{
location.reload();
});
});
</script>
<div id="clear">
{{=A('CLEAR BASKET', _href=URL('start'), _id="basket")}}
</div>
<h1>Your Shopping Cart</h1>
<hr />
<div id="receipt">
<table width="100%">
<tr>
<th>PRODUCT</th>
<th>PRICE</th>
<th>QUANTITY</th>
<th>SUB-TOTAL</th>
<th>ADD/SUBTRACT</th>
</tr>
{{for id, qty in cart.items():}}{{p=db.product(id)}}
<tr>
<td>{{=p.name}}</td>
<td>P {{=p.price}}</td>
<td><span id="{{='item%s'%p.id}}">{{=qty}}</span></td>
<td>
P {{=qty*p.price}}
</td>
<td>
##
*
{{=A('+',callback=URL('cart_callback',vars=dict(id=p.id,action='add')),target='item%s'%p.id,_class='button
pill', _style='increase')}}*
*
{{=A('-',callback=URL('cart_callback',vars=dict(id=p.id,action='sub')),target='item%s'%p.id,_class='button
pill', _style='increase')}}*
</td>
</tr>
{{pass}}
<tr><td><hr /></td><td><hr /></td><td><hr /></td><td><hr /></td><td><hr
/></td></tr>
<tr>
<td></td><td></td><td class="payment">Tender</td><td class="amt">P
{{=text}}</td>
</tr>
<tr>
<td></td><td></td><td class="payment">Total</td><td class="amt"> P
{{=total}}</td> <td><div id="reload"><a href="#" id="reload2">PRICE RELOAD<
/a></div></td> *#jQuery PAGE RELOAD FUNCTION*
</tr>
<tr>
<td></td><td></td><td class="payment">Change:</td> <td class="amt">P
{{=change}}</td>
</tr>
<tr></tr>
<tr></tr>
<tr>
<td style="font-weight: bold; font-size: small;">Authorised By:
{{=auth.user.first_name}} {{=auth.user.last_name}}</td>
</tr>
<tr>
<td style="font-weight: bold; font-size: small;">Date & Time:
{{=request.now}}</td>
</tr>
</table>
</div>
<hr />
<div id="formContainer">
#INPUT FIELD FOR ENTERING THE PAYING AMOUNT
<form method="POST">
<input name="name" placeholder="Enter Amount" id="amount"/>
<input type="submit" value="PURCHASE" id="buy"/>
</form>
</div>
<hr />
<div id="checkout">
<a href="{{=URL('buy')}}">MOVE TO CHECKOUT</a>
</div>
*CONTROLLER1*
@auth.requires_login()
def cart():
if not session.cart:
session.flash = 'Add something to shopping cart'
redirect(URL('index'))
total = sum(db.product(id).price*qty for id,qty in session.cart.items())
text=request.vars.name
if text:
change=int(text)-total
else:
change=int(0)
return dict(cart=session.cart, total=total, text=text, change=change)
*CONTROLLER2*
@auth.requires_login()
def buy():
if not session.cart:
session.flash = 'Add something to shopping cart'
redirect(URL('index'))
import uuid
from gluon.contrib.AuthorizeNet import process
invoice = str(uuid.uuid4())
total = sum(db.product(id).price*qty for id,qty in session.cart.items())
for key, value in session.cart.items():
db.sale.insert(invoice=invoice,buyer=auth.user.id,product =
key,quantity
= value,price = db.product(key).price)
session.cart.clear()
session.flash = 'Purchase Complete'
redirect(URL('invoice',args=invoice))
return dict(cart=session.cart,form=form,total=total)
Mostwanted
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.