首页 > 解决方案 > 有没有办法使用 sqlalchemy-datatables 修改表数据?

问题描述

我今天花了一些时间玩sqlalchemy-datatables。我的目标是简单地实现查询数据的 ajax 加载到数据表以及分页。它很容易处理这些。

到目前为止,最大的障碍是缺乏文档,尽管这个模块的使用似乎相当不错。顺便说一句,如果有人知道一些文档,请分享!

尽管如此,经过一些试验和错误,我还是能够让下面的代码工作(为了简洁起见,我故意省略了我的模型)。

从视图路由

@app.route('/accounts/query/<path:select>/', methods=['GET'])
@roles_accepted('admin', 'copy', 'client')
def query_accounts(select):
    if select == 'all':

        client = db.aliased(User)
        csr = db.aliased(User)

        columns = [
            ColumnDT(Account.id),
            ColumnDT(Account.name),
            ColumnDT(client.fullname),
            ColumnDT(csr.fullname),
            ColumnDT(client.current_login_at)
        ]

        query = db.session.query() \
                    .select_from(Account) \
                    .outerjoin(client, Account.owner) \
                    .outerjoin(csr, Account.copywriter)

        params = request.args.to_dict()
        rowTable = DataTables(params, query, columns)

        return jsonify(rowTable.output_result())

    return abort(404)

来自模板的 html

<table class="table" id="datatable">
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Client</th>
      <th>CSR</th>
      <th>Last Access</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

来自模板的 javascript

$(document).ready(function () {
  $('table.table').dataTable({
    "processing": true,
    "serverSide": true,
    "ajax":"{{ url_for('query_accounts', select='all') }}",
    "order": [1, 'asc']
  });
});

我剩下的几个争论点之一是:由于所有数据表信息都由 jquery-datatables 模块绘制,而 sqlalchemy-datatables 正在提供该信息,所以我看不到能够自定义数据的方法就像我通常在我使用过的几乎所有其他数据表中所做的那样(参见下面的示例,在行中创建指向特定用户记录的链接)。

<table class="table" id="datatable">
  <thead>
    <tr>
      <th>Email</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Business Name</th>
      <th>Last Login</th>
      <th>Roles</th>
    </tr>
  </thead>
  <tbody>
    {% for user in users %}
    <tr>
      <td><a href="{{ url_for('users', id=user.id) }}">{{ user.email }}</a></td>
      <td>{{ user.first_name }}</td>
      <td>{{ user.last_name }}</td>
      <td>{{ user.business_name }}</td>
      <td>{{ user.last_login_at|local_datetime }}</td>
      <td>{{ user.roles|rolesformat }}</td>
    </tr>
    {% endfor %}
  </tbody>
</table>

有没有办法在 sqlalchemy-datatables 创建的表中创建这样的链接?

标签: pythonjqueryflaskdatatablessqlalchemy

解决方案


正如经常发生的那样,睡一晚觉和重新审视问题会产生结果。

这个问题实际上与 sqlalchemy-datatables 无关,而是更多关于数据表本身。我在这里找到了答案:https ://datatables.net/manual/data/renderers ,它讨论了如何在呈现表数据时对其进行修改。

这是使用 id 将表中的名称数据转换为链接的工作 javascript。

$(document).ready(function () {
    var table = $('table.table').dataTable( {
        "processing": true,
        "serverSide": true,
        "ajax":"{{ url_for('query_accounts', select='all') }}",
        "order": [1, 'asc'],
        /* render name column strings as links */
        "columnDefs": [ {
            "targets": 1,
            "data": null,
            "render": function (data, type, row, meta) {
                return "<a href='{{ url_for('accounts') }}" + data[0] + "/'>" + data[1] + "</a>";
            }
        } ]
    } );
});

推荐阅读