javascript.txt 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. </script>
  24. {% endblock %}
  25. .. snippet:: javascript
  26. :filename: app/static/app/formset_handlers.js
  27. (function($) {
  28. $(document).on('formset:added', function(event, $row, formsetName) {
  29. if (formsetName == 'author_set') {
  30. // Do something
  31. }
  32. });
  33. $(document).on('formset:removed', function(event, $row, formsetName) {
  34. // Row removed
  35. });
  36. })(django.jQuery);
  37. Two points to keep in mind:
  38. * The JavaScript code must go in a template block if you are inheriting
  39. ``admin/change_form.html`` or it won't be rendered in the final HTML.
  40. * ``{{ block.super }}`` is added because Django's
  41. ``admin_change_form_document_ready`` block contains JavaScript code to handle
  42. various operations in the change form and we need that to be rendered too.
  43. Sometimes you'll need to work with ``jQuery`` plugins that are not registered
  44. in the ``django.jQuery`` namespace. To do that, simply change how the code
  45. listens for events. Instead of wrapping the listener in the ``django.jQuery``
  46. namespace, just listen to the event triggered from there. For example:
  47. .. code-block:: html+django
  48. {% extends 'admin/change_form.html' %}
  49. {% load static %}
  50. {% block admin_change_form_document_ready %}
  51. {{ block.super }}
  52. <script type="text/javascript" src="{% static 'app/unregistered_handlers.js' %}></script>
  53. {% endblock %}
  54. .. snippet:: javascript
  55. :filename: app/static/app/unregistered_handlers.js
  56. django.jQuery(document).on('formset:added', function(event, $row, formsetName) {
  57. // Row added
  58. });
  59. django.jQuery(document).on('formset:removed', function(event, $row, formsetName) {
  60. // Row removed
  61. });