Advertisement
Rich_A

calling jinja {%for loop%} in javascript

May 3rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. python script
  2. @app.route('/treatment', methods=['GET','POST'])
  3. @login_required
  4. def treatment():
  5. Patient=Patients.query.all()
  6. return render_template('treatment.html', title='treatment', li='Diagnosis',Patient=Patient)
  7.  
  8.  
  9. <html>
  10. <body>
  11. <button class="add_field_button">Add More Fields</button>
  12. <div>
  13. <select class="form-control" name="mytext[]">
  14.  
  15. <option >Select a Medicine</option>
  16. {%for all in Patient%}
  17. <option value="{{all.id}}" >{{all.registration_no}}&nbsp;-&nbsp;{{all.first_name}}&nbsp;{{all.Sur_name}}</option>
  18. {%endfor%}
  19. </select>
  20.  
  21. </div>
  22.  
  23. </body>
  24. </html
  25. <script>
  26. var max_fields = 10; //maximum input boxes allowed
  27. var wrapper = $(".input_fields_wrap"); //Fields wrapper
  28. var add_button = $(".add_field_button"); //Add button ID
  29.  
  30. var x = 1; //initlal text box count
  31. $(add_button).click(function(e){ //on add input button click
  32. e.preventDefault();
  33. if(x < max_fields){ //max input box allowed
  34. x++; //text box increment
  35. $(wrapper).append('<div><select class="form-control" name="mytext[]"> <option >Select a Medicine</option>{%for all in Patient%}<option value="{{all.id }}">{{all.registration_no }}&nbsp;-&nbsp;{{all.first_name }}&nbsp;{{all.Sur_name }}</option>{%endfor%} </select><a href="#" class="remove_field">Remove</a></div>'); //add input box
  36. }
  37. });
  38.  
  39. $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
  40. e.preventDefault(); $(this).parent('div').remove(); x--;
  41. })
  42. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement