javascript.txt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ======================================
  2. JavaScript customizations in the admin
  3. ======================================
  4. .. _admin-javascript-inline-form-events:
  5. Inline form events
  6. ==================
  7. You may want to execute some JavaScript when an inline form is added or removed
  8. in the admin change form. The ``formset:added`` and ``formset:removed`` jQuery
  9. events allow this. The event handler is passed three arguments:
  10. * ``event`` is the ``jQuery`` event.
  11. * ``$row`` is the newly added (or removed) row.
  12. * ``formsetName`` is the formset the row belongs to.
  13. The event is fired using the :ref:`django.jQuery namespace
  14. <contrib-admin-jquery>`.
  15. In your custom ``change_form.html`` template, extend the
  16. ``admin_change_form_document_ready`` block and add the event listener code:
  17. .. code-block:: html+django
  18. {% extends 'admin/change_form.html' %}
  19. {% load static %}
  20. {% block admin_change_form_document_ready %}
  21. {{ block.super }}
  22. <script type="text/javascript" src="{% static 'app/formset_handlers.js' %}></script>
  23. {% endblock %}
  24. .. snippet:: javascript
  25. :filename: app/static/app/formset_handlers.js
  26. (function($) {
  27. $(document).on('formset:added', function(event, $row, formsetName) {
  28. if (formsetName == 'author_set') {
  29. // Do something
  30. }
  31. });
  32. $(document).on('formset:removed', function(event, $row, formsetName) {
  33. // Row removed
  34. });
  35. })(django.jQuery);
  36. Two points to keep in mind:
  37. * The JavaScript code must go in a template block if you are inheriting
  38. ``admin/change_form.html`` or it won't be rendered in the final HTML.
  39. * ``{{ block.super }}`` is added because Django's
  40. ``admin_change_form_document_ready`` block contains JavaScript code to handle
  41. various operations in the change form and we need that to be rendered too.
  42. Sometimes you'll need to work with ``jQuery`` plugins that are not registered
  43. in the ``django.jQuery`` namespace. To do that, simply change how the code
  44. listens for events. Instead of wrapping the listener in the ``django.jQuery``
  45. namespace, just listen to the event triggered from there. For example:
  46. .. code-block:: html+django
  47. {% extends 'admin/change_form.html' %}
  48. {% load static %}
  49. {% block admin_change_form_document_ready %}
  50. {{ block.super }}
  51. <script type="text/javascript" src="{% static 'app/unregistered_handlers.js' %}></script>
  52. {% endblock %}
  53. .. snippet:: javascript
  54. :filename: app/static/app/unregistered_handlers.js
  55. django.jQuery(document).on('formset:added', function(event, $row, formsetName) {
  56. // Row added
  57. });
  58. django.jQuery(document).on('formset:removed', function(event, $row, formsetName) {
  59. // Row removed
  60. });