【问题标题】:does anyone have any examples of using jqGrid with twisted / python有没有人有任何使用 jqGrid 和 twisted / python 的例子
【发布时间】:2012-02-22 00:20:05
【问题描述】:

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

【问题讨论】:

  • 我没有给 atm 的工作示例,但我可以告诉您,将您的数据回显为 json_encode 将是解锁您的解决方案的第一个关键,因为这会将您的数据数组自动格式化为jqGrid 甚至 Flexigrid 用来解释数据对象的预期形式。

标签: jquery python json jqgrid twisted.web


【解决方案1】:

首先在某处定义您的网格(例如,grids.py)。只需要一个模型或查询集和一个 url。

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

为这些视图定义 URL

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

【讨论】:

    猜你喜欢
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 2011-04-19
    • 2010-10-03
    • 2022-11-09
    • 2014-08-23
    相关资源
    最近更新 更多