2

I'm making a flask webpage which dynamically generates an HTML table from a database, and a checkbox for each record beside it. I want the user to select multiple records and delete them all at once, but I can't seem to figure out how to know which records are selected. The code for the html table:

            <thead>
            <tr>
                <th scope="col" class="col-md-1">Delete</th>
                <th scope="col" class="col-md-5">Device Name</th>
                <th scope="col" class="col-md-6">Device MAC</th>
            </tr>
            {% for row in data %}
            <tr>
                <td>
                    <input type="checkbox" name="checkbox{{row .index}}"
                           value={{row .index}}{% if row .like== 1 %} checked {% else %} {% endif %}>
                    {{row .like}}
                </td>
                <td>
                    {{row[0]}}
                </td>
                <td>
                    {{row[1]}}
                </td>
            </tr>
            {% endfor %}
            </thead>
        </table>

I was thinking about using the requests.form way of getting the 'checked' value, but it's not on a form, so how should I access this? And how do I do this for every record in the table, so I can delete them from the database?

Edit:

    @app.route('/', methods=['GET', 'POST'])
    def index():
        if request.method == 'POST':
            if request.form['submit_button'] == 'Add':
                devAdd = [request.form['addDN'], request.form['addMAC']]
                error = addDevice(devAdd)
                return render_template('index.html', 
                errorAdd=error,data=loadDB())
            else:
                #Delete
        else:
            return render_template('index.html', data=loadDB())

The if request.form['submit_button'] == 'Add': part is another button that adds a record to the table, so the code for the delete process needs to be in the else: #Delete part. However, the table is not part of the form, so should I create its own form? And will it cause conflict?

1 Answer 1

1

Can you post your python code as well? Depending on your python code, I think a clean way to do this might be to:

  1. Create a form
  2. Append a select field's value (maybe flask wtform) with the row_indices (from checkbox (True/False) -- JS might be useful here.
  3. Retrieve those indices back from the POST request sent with the form submission
  4. Drop the rows from your table based on the indices
  5. Redirect back to this route that you're showing us in the HTML (basically a refresh with the updated table).
Sign up to request clarification or add additional context in comments.

1 Comment

Also I don't know anything about JS so that is not an option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.