web2py 中 jQuery dataTable 的 JSON 数据
我正在尝试通过 AJAX 将 json 数据从 web2py 控制器加载到 jQuery dataTable。
但仅呈现空白数据表(具有所需的格式、搜索框等)
有人可以请吗?指出我的代码哪里有错误。
数据未显示(由“get_data”方法返回)。
我已确保数据库表已填充。
控制器
def show_data():
return dict()
def get_data():
custdata = db.executesql(qry, as_dict=True)
return custdata
出于测试目的,我在单独的方法中返回了response.json(custdata)在“jsonlint.com”上验证了相同的内容。 这是有效的 json。
查看 (show_data.html)
{{extend 'layout.html'}}
$(document).ready(function() {
var oTable = $('.smarttable').dataTable( {
"sScrollY": "200px",
"sAjaxSource": "{{=URL('MIS','get_data.json')}}",
"sDom": "frtiS",
"bDeferRender": true
} );
} );
最后,为 class="smarttable" 的表格定义 html 表格标签
I am trying to load json data from web2py controller to jQuery dataTable via AJAX.
But only blank dataTable is rendered (with desired formatting, search box, etc.)
Can somebody pl. point out into my code as to where I have a mistake.
Data is not displayed (as returned by "get_data" method).
I have made sure that the database tables have been populated.
Controller
def show_data():
return dict()
def get_data():
custdata = db.executesql(qry, as_dict=True)
return custdata
For testing purpose, I returned response.json(custdata) in a separate method & validated the same on "jsonlint.com".
It is valid json.
View (show_data.html)
{{extend 'layout.html'}}
$(document).ready(function() {
var oTable = $('.smarttable').dataTable( {
"sScrollY": "200px",
"sAjaxSource": "{{=URL('MIS','get_data.json')}}",
"sDom": "frtiS",
"bDeferRender": true
} );
} );
Lastly, html table tags are defined for a table with class="smarttable"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 get_data 函数应该返回一个字典,如下所示:
在 web2py 中,仅当控制器操作返回字典时才会调用视图 - 否则,控制器输出只是按原样返回到客户端(并且按原样, custdata 还没有已转换为 JSON)。
当您调用 URL /MIS/get_data.json 时,.json 扩展名会告诉 web2py 查找 /views/MIS/get_data.json 视图文件以用于呈现 JSON。如果找不到该视图文件,它将尝试使用 /views/generic.json,尽管它只会将 generic.json 用于本地请求,除非您通过指定response.generic_patterns=['json' 来覆盖它]。
作为使用视图呈现 JSON 的替代方法,您还可以执行以下操作:
编辑:jQuery DataTables 插件要求 JSON 包含一些特殊参数,因此您必须在返回 JSON 之前添加这些参数。为了让事情变得更简单,您可以考虑使用 PowerTable (DataTables 的 web2py 插件),或 jqGrid 小部件包含在 web2py 的 plugin_wiki (该小部件可以在任何网页上使用,而不仅仅是 wiki 页面) 。
Your get_data function should return a dictionary, like this:
In web2py, a view is only called if the controller action returns a dictionary -- otherwise, the controller output is simply returned to the client as is (and as is, custdata has not yet been converted to JSON).
When you call the URL /MIS/get_data.json, the .json extension tells web2py to look for a /views/MIS/get_data.json view file to use for rendering the JSON. If it doesn't find that view file, it will trying using /views/generic.json, though it will only use generic.json for local requests, unless you override that by specifying
response.generic_patterns=['json']
.As an alternative to using a view to render the JSON, you could also do:
EDIT: The jQuery DataTables plugin requires the JSON to include some special parameters, so you'll have to add those before returning the JSON. To make things easier, you might consider using PowerTable (a web2py plugin for DataTables), or the jqGrid widget included with web2py's plugin_wiki (the widget can be used on any web page, not just wiki pages).
您必须在为 return 提供的“字典”中拥有“键”值。
iTotalRecords、iTotalDisplayRecords、sEcho 和 aaData。您可以在 http://datatables.net/usage/server-side 中找到说明
you have to have the "key" values in the "dictionary" that you give for return .
iTotalRecords,iTotalDisplayRecords,sEcho and aaData. you can find the explanations in http://datatables.net/usage/server-side