does anyone have any examples of using jqGrid with twisted / python

1.4k views Asked by At

I am looking to have a very simple grid and i want to use jqGrid with twisted web server returning all of the json. I have a number of examples of jqGrid code but wanted to see if there was any examples of the backend in python / twisted ?

1

There are 1 answers

0
Evan On BEST ANSWER

First define your grid somewhere (e.g., grids.py). Only a model or queryset and a url are required.

class ExampleGrid(JqGrid):
    model = SomeFancyModel # could also be a queryset
    fields = ['id', 'name', 'desc'] # optional 
    url = reverse('grid_handler')
    caption = 'My First Grid' # optional
    colmodel_overrides = {
        'id': { 'editable': False, 'width':10 },
    }

Create views to handle requests

def grid_handler(request):
    # handles pagination, sorting and searching
    grid = ExampleGrid()
    return HttpResponse(grid.get_json(request), mimetype="application/json")

def grid_config(request):
    # build a config suitable to pass to jqgrid constructor   
    grid = ExampleGrid()
    return HttpResponse(grid.get_config(), mimetype="application/json")

Define URLs for those views

from myapp.views import grid_handler, grid_config
...
url(r'^examplegrid/$', grid_handler, name='grid_handler'),
url(r'^examplegrid/cfg/$', grid_config, name='grid_config'),

got this example straight from here which is using django. you will need to build the functions which returns your json based on the data.

maybe something like:

def get_rows():
    db.things.category.represent = lambda v: v.name
    fields = ['id','name','category','price','owner']
    rows = []
    page = int(request.vars.page)
    pagesize = int(request.vars.rows)    
    limitby = (page * pagesize - pagesize,page * pagesize)
    orderby = db.things[request.vars.sidx]
    if request.vars.sord == 'desc': orderby = ~orderby
    for r in db(db.things.id>0).select(limitby=limitby,orderby=orderby):
        vals = []
        for f in fields:
            rep = db.things[f].represent
            if rep:
                vals.append(rep(r[f]))
            else:
                vals.append(r[f])
        rows.append(dict(id=r.id,cell=vals))
    total = db(db.things.id>0).count()       
    pages = int(total/pagesize)
    #if total % pagesize == 0: pages -= 1 
    data = dict(total=pages,page=page,rows=rows)
    return data

which is from here