如何在数据表中显示Python数组?

2024-09-26 17:39:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是Flask,我有一个数组,我从数据库中读取,这个数组看起来像[[“customer”,“address”,“PPL”],[“customer2”,“address2”,“PPL2”]]

使用DataTables插件,我希望“customer”和“customer2”显示在customer列中,“address”和“address2”显示在address列中,而“PPL”和“PPL2”显示在PPL列中。我可以用HTML中的for循环来实现这一点,但这会扰乱数据表格式,并从表中删除搜索框和prev/next按钮。如何获得这些值来填充数据表中的单元格?在

Python代码:

def hello():

mariadb_connection = mariadb.connect(user='root', password='pwd', 
database='customers')
cursor = mariadb_connection.cursor()

cursor.execute("SELECT * from customer_info")
data = cursor.fetchall()
cursor.close()


namesdb = json.dumps(data)


return render_template('select_customer.html', namesdb=namesdb)

html格式:

^{pr2}$

我被链接到这个帖子,How to structure data to easily build HTML tables in Flask

但如前所述,使用这个for循环可以填充DataTables表中的列,但是for循环似乎删除了搜索框和prev/next按钮。在

在脚本.js在

  $(document).ready(function() {
 var table = $('#example').DataTable();
 $('#example tbody').on( 'click', 'tr', function () {
     $(this).toggleClass('selected');
  } );

 $('#button').click( function () {

alert( table.rows('.selected').data().length +' 
 row(s) selected' );

 } );

 } );

Tags: flaskfordataaddressfunctioncustomer数组ppl
1条回答
网友
1楼 · 发布于 2024-09-26 17:39:40

首先,不要json.dumps文件将数据发送到模板之前。在

def hello():

    mariadb_connection = mariadb.connect(user='root', password='pwd', 
    database='customers')
    cursor = mariadb_connection.cursor()

    cursor.execute("SELECT * from customer_info")
    namesdb = cursor.fetchall()
    cursor.close()

    return render_template('select_customer.html', namesdb=namesdb)

只需使用Jinja中的python元组列表。在

^{pr2}$

相关问题 更多 >

    热门问题