javascript.txt 2.7 KB

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