简体   繁体   English

如何为每一行创建 HTML 表格按钮并将第一列的值输入到 Flask 函数进行查询

[英]How to create HTML table button for each row and input first column's value to Flask function for query

I have an HTML table that I want to have a button named "more details" for each row at the last column.我有一个 HTML 表格,我希望在最后一列的每一行都有一个名为“更多详细信息”的按钮。

I want to have the ability that when I click on "more details" button for each row, the value of month (which is the first column of the table, for example: '2021-10') for that specific row will send to a flask function to query info only for that specific month, and then the browser will jump to another HTML file (monthly_sales_report_sub.html) that display the new info as a table that contains one row.我希望能够在单击每一行的“更多详细信息”按钮时,该特定行的月份值(即表格的第一列,例如:'2021-10')将发送到一个仅查询特定月份信息的烧瓶函数,然后浏览器将跳转到另一个 HTML 文件 (monthly_sales_report_sub.html),该文件将新信息显示为包含一行的表格。

What is the best way to make the HTML code and flask function work?使 HTML 代码和 Flask 函数工作的最佳方法是什么? My current code will show all the months records on monthly_sales_report_sub.html我当前的代码将显示monthly_sales_report_sub.html 上的所有月份记录

Thanks in advance!提前致谢!

The HTML code below (monthly_sales_report.html):下面的 HTML 代码 (monthly_sales_report.html):

           <table width=80%>
                <!-- Show Report Table -->
                <thead>
                    <tr>
                        {% for header in headings %}
                        <th>{{ header }}</th>
                        {% endfor %}
                    </tr>
                </thead>
                <tbody>
                    {% for row in data %}
                    <tr>
                        {% for cell in row %}
                        <td>{{ cell }}</td>
                        {% endfor %}
                        <td class="td-actions text-right">
                            <a href=monthly_sales_report_sub><input type="submit" value="more details"><</input></a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>

My current flask function looks like this:我当前的烧瓶函数如下所示:

@api.route('/monthly_sales_report', methods=['GET'])
def monthly_sales_table():
    return render_template("monthly_sales_report.html", headings=SQL_Operations.get_monthly_sales_report(connection)['headings'], data=SQL_Operations.get_monthly_sales_report(connection)['rows'])

@api.route('/monthly_sales_report_sub', methods=['GET'])
def monthly_sales_table_sub():
    return render_template("monthly_sales_report_sub.html", headings=SQL_Operations.get_monthly_sales_report_sub(connection)['headings'], data=SQL_Operations.get_monthly_sales_report_sub(connection)['rows'])

SQL_Operations.py SQL_Operations.py

def get_monthly_sales_report_sub(connection):
    cursor = connection.cursor()
    query = ("""SELECT month, sales_person, total_num_sold, total_sales FROM Sale;""")
    cursor.execute(query)
    res = []
    for (month, sales_person, total_num_sold, total_sales) in cursor:
        res.append([month, sales_person, total_num_sold, total_sales])
    res_new = dict()
    res_new['headings'] = ['month', 'sales_person', 'total_num_sold', 'total_sales']
    res_new['rows'] = res
    return res_new

In flask you can create routes with arguments在烧瓶中,您可以使用参数创建路由

@app.route('/detail/<month>',methods = ['GET'])

In your table you can use a button or a tag button with:在您的表格中,您可以使用按钮或标签按钮:

<a href={{ url_for('detail',month = your_row_month_value) }} ... 

You have to add url_for to your Flask imports您必须将 url_for 添加到 Flask 导入中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取html表中每一行的特定列的值? - how to get the value of a specific column of each row in a html table? 获取HTML表行数据到Flask function - Get HTML table row data to Flask function Flask - 如何在 Python ZC1C425268E18385D1AB44A 中使用 HTML 按钮的值? - Flask - How do I use the value of an HTML button in a Python function? 将每个第一个匹配行中的列值设置为 0 - Set column value in each first matched row to 0 如何为熊猫中的列中的每个逗号分隔值创建一个新行 - How to create a new row for each comma separated value in a column in pandas 如何创建包含每行倒数第二个值的列? - How to create a column that contains the penultimate value of each row? 如何使用第一行的值拆分一列? - How to split sort a column using the first row's value? 如何根据用户输入的行和列标题创建乘法表? - How to create a multiplication table based on user input with row and column headers? 返回新列中每一行中第一个匹配值的列名 - Return column name of the first matching value in each row in a new column Python Pandas 旋转:如何在第一列中分组并为第二列中的每个唯一值创建一个新列 - Python Pandas pivoting: how to group in the first column and create a new column for each unique value from the second column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM