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 9 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 9 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 10 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 10 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 12 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 12 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 12 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])}
    

  • 0
    telloroberto 12 years ago
    Can i click on a cell and rescue the values?? is that posible and how??

  • 0
    johannspies 13 years ago
    Thanks. That helped.

  • 0
    johannspies 13 years ago
    Thanks for the further examples. How would you handle the 'formatter': 'showlink' and 'baseLinkUrl': '' where uuid is used for cross references and a field is being represented by this: db.akb_articles.journal.represent = lambda x: db(db.akb_journal.uuid==x).select(db.akb_journal.title).first()['title'] I would like the 'journal' field in the grid to show the title of the journal and link to the record in akb_journal.

  • 0
    iiijjjiii 13 years ago
    The only variable data associated with a "formatter: showlink" column is the id of the record. So we have to work with that. Create a link_handler function to redirect it properly.
            'colModel': [
                {
                    'name': 'akb_journal',
                    'formatter':'showlink',
                    'formatoptions':{
                        'baseLinkUrl': 'link_handler',
                        },
                },
    
        def link_handler():
            article = db(db.akb_articles.id == request.vars.id).select().first()
            if not article:
                session.flash = 'Invalid article id: %s' % request.vars.id
                redirect(URL('???'))
    
            journal = db(db.akb_journal.uuid==article.journal).select().first()
            redirect(URL('journal_edit', args=[journal.id]))
    
    If you have more than one column formatted as a link, then pass a parameter along indicating the field. All links go to the same link_handler function, which redirects appropriately.
            'colModel': [
                {
                    'name': 'akb_journal',
                    'formatter':'showlink',
                    'formatoptions':{
                        'baseLinkUrl': 'link_handler',
                        'addParam': '&field=akb_journal',
                        },
                },
                {
                    'name': 'akb_table',
                    'formatter':'showlink',
                    'formatoptions':{
                        'baseLinkUrl': 'link_handler',
                        'addParam': '&field=akb_table',
                        },
                },
    
        def link_handler():
            article = db(db.akb_articles.id == request.vars.id).select().first()
            if not article:
                session.flash = 'Invalid article id: %s' % request.vars.id
                redirect(URL('???'))
    
            if request.vars.field == 'akb_journal':
                journal = db(db.akb_journal.uuid==article.journal).select().first()
                redirect(URL('journal_edit', args=[journal.id]))
    
            if request.vars.field == 'akb_table':
                redirect(URL('table_edit', args=[article.table_id]))
    

  • 0
    johannspies 13 years ago
    Thanks for your reply. I could download the module after pressing Enter at the password prompt: % git clone https://iiijjjii@github.com/iiijjjii/jqgrid.git Cloning into jqgrid... Password: remote: Counting objects: 212, done. remote: Compressing objects: 100% (182/182), done. remote: Total 212 (delta 43), reused 195 (delta 26) Receiving objects: 100% (212/212), 389.62 KiB | 150 KiB/s, done. Resolving deltas: 100% (43/43), done.

  • 0
    iiijjjiii 13 years ago
    I updated the jqgrid application default controller to include examples for the following: * Complex Query (under Custom Queries menu) The jqgrid includes a custom query, selected fields from table including a virtual field, a column with data not found in a table. The query accesses data with a left join and formats data prior to display. * Web Service Illustrates displaying a jqgrid table with data accessed from a webservice.

  • 0
    johannspies 13 years ago
    "git clone https://iiijjjii@github.com/iiijjjii/jqgrid.git" requires a password. Is there some other way to download the module?

  • 0
    iiijjjiii 13 years ago
    There should be no password on it. Try just clicking OK when prompted.

Hosting graciously provided by:
Python Anywhere