无法将JSON数据加载到jQuery sqlalchemy-datatable
作者:互联网
我在将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条记录).
任何帮助,将不胜感激,
最好的祝福.
解决方法:
尝试传递不带属性的query()(即您的映射类),然后使用select_from().但最重要的是,您必须避免在最后附加all().据我所知,数据表接受sqlalchemy对象并为您完成工作.
在您的情况下,这应该起作用:
query = db.session.query().select_from(Feature)
除此行外,您的代码应该运行没有问题.
标签:flask,flask-sqlalchemy,datatables,python,jquery 来源: https://codeday.me/bug/20191026/1935758.html