If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

In model

db.define_table('category',
                Field('name', label='Name of Category'), format='%(name)s')

db.define_table('things',
                Field('name'),
                Field('quantity', 'integer'),
                Field('owner'),
                Field('price','double'),
                Field('category', db.category))

In controller

JqGrid = local_import('jqgrid', app='jqgrid', reload=True).JqGrid
JqGrid.initialize_response_files(globals(), theme='ui-lightness', lang='en')

def things():
    return dict(jqgrid=JqGrid(globals(), db.things)())

A view is not necessary, but can be used to customize the look. For example:

{{extend 'layout.html'}}
<h1>Things</h1>
{{=jqgrid}}

Related slices

Comments (13)

  • Login to post



  • 0
    sif-baksh-10957 6 years ago

    Do you have an example how to use it with out a DB?

    New to web2py

    I'm making a restful API call that returns a JSON output

    I would love to use this method.

    Thanks in advance

    replies (1)
    • iiijjjiii 6 years ago

      The code base has a 'Web Services' example which may help. The example uses xml but the solution for json would be similar. If you intend to make calls within javascript code, it may be simpler to interface with jqGrid directly as it was intended in javascript.


  • 0
    luciusagarthy 7 years ago

    Hey,

    to my previous question: I found it out, it was enough to call:

    db.things.category.represent = lambda v: v.name

    before calling return dict()


  • 0
    luciusagarthy 7 years ago

    Hallo,

    nice job. I started with web2py week ago and want to implement jqgrid. 

    I am using my own db and cannot figure out, how to show column from foreignkey table, it is 

    showing just id column, but not name column (like db.thing with db.category.name in second column).

    Another think: 'rownumbers': 'true' does not work properly, it adds column for rownumbers, but no rownumbers are shown, 

    just data are shift one column left :(, do not know if I have to manually add row numbers or jqgrid generates it.

    Can you help. Sorry for my english, I am used just to read manuals :(.


  • 0
    joel-robinson-10375 9 years ago

    I can't seem to figure out how to do and AND query. I tried somthing like this:

    return dict(customer=JqGrid(globals(), db.customer, query=db.customer.lastname==session.lastname & db.customer.phone==session.phone)())

    How can I combine queries?

    Thanks for you help.

    replies (1)
    • iiijjjiii 9 years ago

      Use braces around the queries. Try:

      return dict(customer=JqGrid(globals(), db.customer, query=((db.customer.lastname==session.lastname) & (db.customer.phone==session.phone)))())


  • 0
    iiijjjiii 9 years ago
    @telloroberto: I'm not quite sure exactly what you are after. Examples above show how the contents of a cell can be formatted as a link and on clicking the link a link_handler controller function can be called with the id of the record passed along. With that you have access to all the records fields including the cell field value. JqGrid also permits calling a controller function on clicking a row within the grid. The id of the record represented in the grid row is passed along to the controller function. Here is the code from the example provided in the jqgrid application default controller.
    def callback_demo():
        """Illustrates setting the select_callback_url.
    
        When the select_callback_url is set, when a row in the jqgrid table is
        clicked, the page redirects to indicated url.
    
        JqGrid makes use of jqgrid's onSelectRow event handler.
        http://www.trirand.com/jqgridwiki/doku.php?id=wiki:events
    
        If present in the url, the placeholder '{id}' is replaced with the id of
        the record represented by the row.
        """
        response.generic_patterns = ['html']
        return dict(foo=JqGrid(
            globals(),
            db.things,
            jqgrid_options={'caption': "Click a row to trigger callback."},
            select_callback_url=URL(r=request, f='select_callback/{id}')
            )())
    
    
    def select_callback():
        """Callback for callback_demo().
    
        request.args(0): the id of the db.things record.
        """
        response.generic_patterns = ['html']
        rows = db(db.things.id == request.args(0)).select()
        return {'': BEAUTIFY(rows[0])}
    
show more comments

Hosting graciously provided by:
Python Anywhere