2
0

table_block.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. TableBlock
  2. ==========
  3. The TableBlock module provides an HTML table block type for StreamField. This module uses `handsontable 6.2.2 <https://handsontable.com/>`_ to provide users with the ability to create and edit HTML tables in Wagtail. Table blocks provides a caption field for accessibility.
  4. .. image:: ../../_static/images/screen40_table_block.png
  5. Installation
  6. ------------
  7. Add ``"wagtail.contrib.table_block"`` to your INSTALLED_APPS:
  8. .. code-block:: python
  9. INSTALLED_APPS = [
  10. ...
  11. "wagtail.contrib.table_block",
  12. ]
  13. Basic Usage
  14. -----------
  15. After installation, the TableBlock module can be used in a similar fashion to other StreamField blocks in the Wagtail core.
  16. Import the TableBlock ``from wagtail.contrib.table_block.blocks import TableBlock`` and add it to your StreamField declaration.
  17. .. code-block:: python
  18. class DemoStreamBlock(StreamBlock):
  19. ...
  20. table = TableBlock()
  21. Then, on your page template, the ``{% include_block %}`` tag (called on either the individual block, or the StreamField value as a whole) will render any table blocks it encounters as an HTML ``<table>`` element:
  22. .. code-block:: html+django
  23. {% load wagtailcore_tags %}
  24. {% include_block page.body %}
  25. Or:
  26. .. code-block:: html+django
  27. {% load wagtailcore_tags %}
  28. {% for block in page.body %}
  29. {% if block.block_type == 'table' %}
  30. {% include_block block %}
  31. {% else %}
  32. {# rendering for other block types #}
  33. {% endif %}
  34. {% endfor %}
  35. Advanced Usage
  36. --------------
  37. Default Configuration
  38. ^^^^^^^^^^^^^^^^^^^^^
  39. When defining a TableBlock, Wagtail provides the ability to pass an optional ``table_options`` dictionary. The default TableBlock dictionary looks like this:
  40. .. code-block:: python
  41. default_table_options = {
  42. 'minSpareRows': 0,
  43. 'startRows': 3,
  44. 'startCols': 3,
  45. 'colHeaders': False,
  46. 'rowHeaders': False,
  47. 'contextMenu': [
  48. 'row_above',
  49. 'row_below',
  50. '---------',
  51. 'col_left',
  52. 'col_right',
  53. '---------',
  54. 'remove_row',
  55. 'remove_col',
  56. '---------',
  57. 'undo',
  58. 'redo'
  59. ],
  60. 'editor': 'text',
  61. 'stretchH': 'all',
  62. 'height': 108,
  63. 'language': language,
  64. 'renderer': 'text',
  65. 'autoColumnSize': False,
  66. }
  67. Configuration Options
  68. ^^^^^^^^^^^^^^^^^^^^^
  69. Every key in the ``table_options`` dictionary maps to a `handsontable <https://handsontable.com/>`_ option. These settings can be changed to alter the behaviour of tables in Wagtail. The following options are available:
  70. * `minSpareRows <https://handsontable.com/docs/6.2.2/Options.html#minSpareRows>`_ - The number of rows to append to the end of an empty grid. The default setting is 0.
  71. * `startRows <https://handsontable.com/docs/6.2.2/Options.html#startRows>`_ - The default number of rows for a new table.
  72. * `startCols <https://handsontable.com/docs/6.2.2/Options.html#startCols>`_ - The default number of columns for new tables.
  73. * `colHeaders <https://handsontable.com/docs/6.2.2/Options.html#colHeaders>`_ - Can be set to ``True`` or ``False``. This setting designates if new tables should be created with column headers. **Note:** this only sets the behaviour for newly created tables. Page editors can override this by checking the the “Column header” checkbox in the table editor in the Wagtail admin.
  74. * `rowHeaders <https://handsontable.com/docs/6.2.2/Options.html#rowHeaders>`_ - Operates the same as ``colHeaders`` to designate if new tables should be created with the first column as a row header. Just like ``colHeaders`` this option can be overridden by the page editor in the Wagtail admin.
  75. * `contextMenu <https://handsontable.com/docs/6.2.2/Options.html#contextMenu>`_ - Enables or disables the Handsontable right-click menu. By default this is set to ``True``. Alternatively you can provide a list or a dictionary with [specific options](https://handsontable.com/docs/6.2.2/demo-context-menu.html#page-specific).
  76. * `editor <https://handsontable.com/docs/6.2.2/Options.html#editor>`_ - Defines the editor used for table cells. The default setting is text.
  77. * `stretchH <https://handsontable.com/docs/6.2.2/Options.html#stretchH>`_ - Sets the default horizontal resizing of tables. Options include, 'none', 'last', and 'all'. By default TableBlock uses 'all' for the even resizing of columns.
  78. * `height <https://handsontable.com/docs/6.2.2/Options.html#height>`_ - The default height of the grid. By default TableBlock sets the height to ``108`` for the optimal appearance of new tables in the editor. This is optimized for tables with ``startRows`` set to ``3``. If you change the number of ``startRows`` in the configuration, you might need to change the ``height`` setting to improve the default appearance in the editor.
  79. * `language <https://handsontable.com/docs/6.2.2/Options.html#language>`_ - The default language setting. By default TableBlock tries to get the language from ``django.utils.translation.get_language``. If needed, this setting can be overridden here.
  80. * `renderer <https://handsontable.com/docs/6.2.2/Options.html#renderer>`_ - The default setting Handsontable uses to render the content of table cells.
  81. * `autoColumnSize <https://handsontable.com/docs/6.2.2/Options.html#autoColumnSize>`_ - Enables or disables the ``autoColumnSize`` plugin. The TableBlock default setting is ``False``.
  82. A `complete list of handsontable options <https://handsontable.com/docs/6.2.2/Options.html>`_ can be found on the Handsontable website.
  83. Changing the default table_options
  84. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  85. To change the default table options just pass a new table_options dictionary when a new TableBlock is declared.
  86. .. code-block:: python
  87. new_table_options = {
  88. 'minSpareRows': 0,
  89. 'startRows': 6,
  90. 'startCols': 4,
  91. 'colHeaders': False,
  92. 'rowHeaders': False,
  93. 'contextMenu': True,
  94. 'editor': 'text',
  95. 'stretchH': 'all',
  96. 'height': 216,
  97. 'language': 'en',
  98. 'renderer': 'text',
  99. 'autoColumnSize': False,
  100. }
  101. class DemoStreamBlock(StreamBlock):
  102. ...
  103. table = TableBlock(table_options=new_table_options)
  104. Supporting cell alignment
  105. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  106. You can activate the `alignment` option by setting a custom `contextMenu` which allows you to set the alignment on a cell selection.
  107. HTML classes set by handsontable will be kept on the rendered block. You'll then be able to apply your own custom CSS rules to preserve the style. Those class names are:
  108. * Horizontal: ``htLeft``, ``htCenter``, ``htRight``, ``htJustify``
  109. * Vertical: ``htTop``, ``htMiddle``, ``htBottom``
  110. .. code-block:: python
  111. new_table_options = {
  112. 'contextMenu': [
  113. 'row_above',
  114. 'row_below',
  115. '---------',
  116. 'col_left',
  117. 'col_right',
  118. '---------',
  119. 'remove_row',
  120. 'remove_col',
  121. '---------',
  122. 'undo',
  123. 'redo',
  124. '---------',
  125. 'copy',
  126. 'cut'
  127. '---------',
  128. 'alignment',
  129. ],
  130. }
  131. class DemoStreamBlock(StreamBlock):
  132. ...
  133. table = TableBlock(table_options=new_table_options)