gpt4 book ai didi

jquery - 有没有人有将 jqGrid 与 twisted/python 一起使用的示例

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:48 25 4
gpt4 key购买 nike

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

最佳答案

首先在某处定义您的网格(例如,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 },
}

创建 View 来处理请求

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

为这些 View 定义 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'),

这个例子直接来自 here这是使用 django。您将需要构建根据数据返回 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

关于jquery - 有没有人有将 jqGrid 与 twisted/python 一起使用的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9059921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com