2

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 ?

leora
  • 188,729
  • 360
  • 878
  • 1,366
  • I dont have a working example to give atm, but I can tell you, echoeing your data as `json_encode` is going to be the first key to unlocking your solution as this will autoformat you data array into the expected form that jqGrid and even Flexigrid use to interpret the data object. – SpYk3HH Feb 08 '12 at 17:17

1 Answers1

1

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

Evan
  • 5,975
  • 8
  • 34
  • 63