无法将JSON数据加载到jQuery sqlalchemy-datatable [英] Can't load JSON data to jQuery sqlalchemy-datatable

查看:82
本文介绍了无法将JSON数据加载到jQuery sqlalchemy-datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将JSON数据加载到数据表时遇到麻烦. 这是我执行该操作的Python代码(对数据库进行查询,并使用jsonify返回该数据):

I'm having troubles on loading JSON data to a datatable. Here it is my Python code to perform that (do the query to the database and return that data with jsonify):

@users_blueprint.route('/data')
def data():
"""Return server side data."""
# defining columns
columns = [
    ColumnDT(User.firstname),
    ColumnDT(User.lastname),
    ColumnDT(User.email),
    ColumnDT(User.urole)
]

# defining the initial query
users = db.session.query(User).all()

# GET parameters
params = request.args.to_dict()

# instantiating a DataTable for the query and table needed
rowTable = DataTables(params, users, columns)

print "AHHAX"
print json.dumps(rowTable.output_result())
# returns what is needed by DataTable
return jsonify(rowTable.output_result())

然后,我有一个jinja2模板( usersAdminSection.html ),其中包含表格式和ajax请求:

Then, I have a jinja2 template (usersAdminSection.html) with the table format and the ajax request:

{% block extra_stylesheets %}
<link href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.css" rel="stylesheet">
{% endblock %}

{% block content %}
<div class="row">
<div class="col-lg-12">
  <table id="dt_110x" class="display" cellspacing="0" width="100%">
    <thead>
      <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Role</th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>
</div>
</div>
{% endblock %}

{% block extra_javascripts %}
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
  var table = $('#dt_110x').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "{{ url_for('users.data') }}"
  });
});
</script>
{% endblock %}

因此,当我刷新该模板/页面时,会出现一个警告对话框,告诉我:

So, when I refresh that template/page I get a alert dialog telling me:

"DataTables warning: table id=dt_110x - 'list' object has no attribute 'add_columns'"

因此,数据将得到无限处理而没有任何返回( 0条记录).

And so, the data is being infinitely processed without any return (0 records).

任何帮助,我们都会感激的,

Any help would be appreciated,

最诚挚的问候.

推荐答案

尝试并传递不带属性的 query()(即您的映射类),并使用 select_from() .但最重要的是,您必须避免在结尾处附加all().据我所知,数据表接受sqlalchemy对象并为您完成工作. 在您的情况下,这应该可行:

Try and pass query() without attributes (i.e. your mapped class) and use select_from(). But above all, you must avoid to append all() at the end. Datatables, as far I can understand, accepts sqlalchemy objects and does the job for you. In your case, this should work:

query = db.session.query().select_from(Feature)

除此行外,您的代码应该运行没有问题.

Except for this line, your code should run without problems.

这篇关于无法将JSON数据加载到jQuery sqlalchemy-datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆