【问题标题】:Can't load JSON data to jQuery sqlalchemy-datatable无法将 JSON 数据加载到 jQuery sqlalchemy-datatable
【发布时间】:2017-06-01 21:22:21
【问题描述】:

我在将 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())

然后,我有一个带有表格格式和 ajax 请求的 jinja2 模板 (usersAdminSection.html):

{% 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 条记录)。

任何帮助将不胜感激,

最好的问候。

【问题讨论】:

    标签: jquery python flask datatables flask-sqlalchemy


    【解决方案1】:

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

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

    除了这一行,您的代码应该可以正常运行。

    【讨论】:

    • 我有同样的问题,但它给了我错误argument must be a string or a number, not 'NoneType'
    猜你喜欢
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2013-10-16
    • 2014-01-07
    • 1970-01-01
    相关资源
    最近更新 更多