有没有人有任何使用带有twisted / python的jqGrid的例子

时间:2012-01-30 05:53:14

标签: jquery python json jqgrid twisted.web

我希望有一个非常简单的网格,我想使用jqGrid与扭曲的web服务器返回所有的json。我有一些jqGrid代码的例子,但想看看python / twisted中是否有后端的例子?

1 个答案:

答案 0 :(得分:1)

首先在某处定义网格(例如,grids.py)。只需要模型或查询集以及网址。

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 },
    }

创建视图以处理请求

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")

定义这些视图的网址

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'),

直接从使用django的here获得此示例。你需要根据数据构建返回json的函数。

可能是这样的:

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

来自here