In Django, I have a model called Exercise
.
Then, on an HTML page, I have a table with all of the exercises that belong to that user.
In the following (shortened) code, you can see that I have a table that displays all of the exercises that the user currently has. Also, it has buttons to add new rows to the table.
Goal: when I add a new row to a table, I want to also create a blank instance of the Exercise
model.
br>
Also, if that is not available in just simple Django and JQuery, please let me know which frameworks support it. Thank you.
$('.deleteButton').on('click', function () {
if ($(this.parentNode.parentNode.parentNode).find('tr').length > 3) {
$(this.parentNode.parentNode.parentNode).find("tr:nth-last-child(2)").remove();
}
})
$('.addButton').click(function () {
$(this.parentNode.parentNode.parentNode).find('tr:last').before('<tr><td>name</td><td>sets</td><td>reps</td><td>weight</td></tr>')
})
{% block content %}
<div class="row" style="margin-top: 1%;">
{% for workout in workouts %}
<div class="col-6 col-lg-3" style="margin-right: 50px;">
<table class="mytable">
{% for exercise in workout.exercise_set.all %}
<tr>
<td>{{ exercise.name }}</td>
<td>{{ exercise.sets }}</td>
<td>{{ exercise.repetitions }}</td>
<td>{{ exercise.weight }}</td>
</tr>
{% endfor %}
<tr id="editrow">
<td colspan="2">
<input class="addButton" type="button" value="Delete" />
</td>
<td colspan="2">
<input class="deleteButton" type="button" value="Delete" />
</td>
</tr>
</table>
</div>
{% endfor %}
</div>
{% endblock %}