How to add an input field in SQLFORM.grid??

瀏覽次數:1,308 次
跳到第一則未讀訊息

greenpoise

未讀,
2012年4月11日 下午6:07:582012/4/11
收件者:web...@googlegroups.com
Is this possible?? I have a products table that shows all the products. I also created an add to cart button in it problem is if I want to add 100 items I would have to press the button 100 times.  Is there a way to add a quantity text field???


Thanks

Dan

Massimo Di Pierro

未讀,
2012年4月11日 晚上7:27:172012/4/11
收件者:web...@googlegroups.com
You can put the grid in a form and you add 

SQLFORM.grid(..., link=[lambda row: INTPUT(...)])

greenpoise

未讀,
2012年4月11日 晚上9:23:082012/4/11
收件者:web...@googlegroups.com
A bit confused..I thought SQLFORM.grid was itself a form? Also, can I add more than one link per grid? Reading from the book, it creates a column so technically sounds as if I could???

Massimo Di Pierro

未讀,
2012年4月11日 晚上9:34:162012/4/11
收件者:web...@googlegroups.com
yes and not and what I said needs clarification.

grid = SQLFORM.grid(create=False,update=False) is not a form. If create=True or update=True then it contains a form when you are creating or updating a record. you must avoind a form within a form and you can do so in the view:

{{if grid.create_form or grid.update_form:}}
    {{=grid}} it is a form
{{else:}}
    <form>{{=grid}} it is not a form but you can embed it in one <input type='submit'/></form>
{{pass}}

greenpoise

未讀,
2012年4月11日 晚上10:00:052012/4/11
收件者:web...@googlegroups.com
Gotcha!   Will try thanks

greenpoise

未讀,
2012年4月12日 下午5:14:492012/4/12
收件者:web...@googlegroups.com
Massimo,

where do I change the INPUT size (length, width)?



links = [lambda row: INPUT(_name='quantity', _class='integer', _value=1)]

Richard Vézina

未讀,
2012年4月12日 下午5:27:532012/4/12
收件者:web...@googlegroups.com
Hello Danel,

May I see screenshot of what Massimo propose?

I mean I would be really interrested in a bulk entry capability too.

If it is already available I would know I was about to make a custom mechanism to allow user to add many result at the same time that would rely on Excel and validate_and_insert web2py method...

The problem I had was to read directly Excel file, I would need to write a lot of code with xlrd. If I can avoid that I will be happy :)

Thanks.

Richard

greenpoise

未讀,
2012年4月13日 下午3:26:562012/4/13
收件者:web...@googlegroups.com
Richard,

I tried Massimos approach but it created one form button rather than one for each row. For my uses, it wont cut it. I need one text field plus a button on each row. I was able to accomplish this like this

links = [lambda row: INPUT(_name='qty', _value='1'), lambda row: A('+',callback=URL('cart_callback',vars=dict(id=row.id,action='add',qt=request.vars.qty)))]

Maybe I did not make myself clear but I just want to have a product grid with all the products and the qty. Sort of a storefront, so the input text is not db attached. Click on the button and add it to my cart.. Hmmm but it seems you want to have like a bulk functionality with the grid, right???

Richard Vézina

未讀,
2012年4月13日 下午3:36:292012/4/13
收件者:web...@googlegroups.com
Yes I think I miss understood... What I would have is the capability to insert many records in one operation (one form submission). I thought that what Massimo suggest turn the SQLFORM.grid in a kind of multiples rows form or something like that. It was not actually clear what Massimo was doing what Massimo suggested. That why I ask you if you were willing to show a screenshot of the result.

Don't mind if I actually miss understand.

Cheer.

Richard

greenpoise

未讀,
2012年4月13日 晚上7:33:182012/4/13
收件者:web...@googlegroups.com
Richard,
you know what? maybe it does what you want actually because what I get is a bunch of records with a txt and one submit button at the end. So, basically a grid within a form. I will write it and run it again and send you a screenshot. 




D

Richard Vézina

未讀,
2012年4月16日 上午9:57:382012/4/16
收件者:web...@googlegroups.com
Great!

Thanks

Richard

Richard Vézina

未讀,
2012年5月25日 下午3:26:202012/5/25
收件者:web...@googlegroups.com
Hello Massimo,

May I use this SQLFORM.grid capability to create a bulk or batch insert feature?

I am having problem with form validation and update of record actually. I try to update a record for now :

Controller :
def grid_into_form():
    grid = SQLFORM.grid(db.tab1,
        _class="web2py_grid",
        links=[lambda row: INPUT(_name='result', _class='decimal')],
        create=True,
        editable=True,
        paginate=5)
    return dict(grid=grid)

View :
<form>{{=grid}}<input type='submit'/></form>

I get input for every records in the table. But since I use html form tag I wonder how I could use validators... I try to put the grid in a form at the controller level like this (not successfully) : 
def grid_into_form():
    grid = SQLFORM.grid(db.tab1,
        _class="web2py_grid",
        links=[lambda row: INPUT(_name='result', _class='decimal')],
        create=True,
        editable=True,
        paginate=5)
    form = FORM(grid)
    if form.accepts(request,session):
        response.flash = 'form accepted'
    elif form.errors:
        response.flash = 'form has errors'
    else:
        response.flash = 'please fill the form'
    return dict(form=form)

Also, how could I generate a grid of say 20 rows with only blank input for every fields of a given table (so kind of empty rows)?

Thanks

Richard

Paul Ellis

未讀,
2016年11月25日 晚上11:11:412016/11/25
收件者:web2py-users
Hi,
I am trying to do the same thing as the OP. Add a quantity input field to a grid and then use Callback to work with the product_id and quantity. I just don't seem to be able to pass the quantity to the callback function.

Using this method I don't get a value from quantity.
                        links=[
                            lambda row: INPUT(_type='number', _value=0, _name='qty'),
                            lambda row: A(T('Add'),
                                          callback=URL('_add_product',args=[row.id, request.vars.qty]),
                                          _class='button btn btn-default')
                            ]
                        )

If I use args or vars doesn't matter. The point is I always get 'None' for the Quantity field.
I have tried wrapping the grid in a form and the input in a form. I am sure I am doing something simple wrong.

Here's the whole function:
@auth.requires_login()
def product_add():
    productid = request.args(0)
    grid = SQLFORM.grid(db.product.id == productid,
                        args=[productid],
                        create=False,
                        editable=False,
                        deletable=False,
                        fields=[db.product.productname,
                                db.product.purchasecost,
                                db.product.monthlycost,
                                db.product.smallimage],
                        links=[
                            lambda row: FORM(INPUT(_type='number', _value=0, _name='qty')),
                            lambda row: A(T('Add'),
                                          callback=URL('_add_product', vars=dict(productid=productid, qt=request.vars.qty)),
                                          _class='button btn btn-default')
                            ]
                        )
    
    return locals()

Anthony

未讀,
2016年11月26日 上午8:35:332016/11/26
收件者:web2py-users
On Friday, November 25, 2016 at 11:11:41 PM UTC-5, Paul Ellis wrote:
Hi,
I am trying to do the same thing as the OP. Add a quantity input field to a grid and then use Callback to work with the product_id and quantity. I just don't seem to be able to pass the quantity to the callback function.

Using this method I don't get a value from quantity.
                        links=[
                            lambda row: INPUT(_type='number', _value=0, _name='qty'),
                            lambda row: A(T('Add'),
                                          callback=URL('_add_product',args=[row.id, request.vars.qty]),

Obviously request.vars.qty won't work here, as request.vars is not populated until after the request arrives at the server. Instead, you would have to use Javascript to update the callback URL whenever the value of the input changes.

Anthony

Paul Ellis

未讀,
2016年11月28日 下午3:15:302016/11/28
收件者:web2py-users
What about if I don't use 'callback' but 'href' so it loads the whole page. I still can't get the quantity value to be passed as an arg.
回覆所有人
回覆作者
轉寄
0 則新訊息