无法将JSON数据加载到jQuery sqlalchemy-datatable

3

我遇到了将JSON数据加载到数据表中的问题。 这是我用Python执行查询并使用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请求:

{% 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 %}

所以,当我刷新该模板/页面时,会弹出一个警告对话框,告诉我:
"DataTables warning: table id=dt_110x - 'list' object has no attribute 'add_columns'"

因此,数据正在无限处理中,没有任何返回(0条记录)。

如有帮助,将不胜感激。

此致敬礼。

1个回答

4

尝试使用select_from()而不带属性(即您的映射类)来传递query()。但最重要的是,您必须避免在末尾添加all()。据我所理解,Datatables接受SQLAlchemy对象并为您完成工作。对于您的情况,这应该可以起作用:

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

除了这一行之外,你的代码应该没有问题。

我有同样的问题,但它给了我一个错误:“参数必须是字符串或数字,而不是'NoneType'”。 - Al Kasih

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接