2
0

table_block.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. TableBlock
  2. ==========
  3. The TableBlock module provides an HTML table block type for StreamField. This module uses `handsontable <https://handsontable.com/>`_ to provide users with the ability to create and edit HTML tables in Wagtail.
  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. Just 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. Advanced Usage
  22. --------------
  23. Default Configuration
  24. ^^^^^^^^^^^^^^^^^^^^^
  25. When defining a TableBlock, Wagtail provides the ability to pass an optional ``table_options`` dictionary. The default TableBlock dictionary looks like this:
  26. .. code-block:: python
  27. default_table_options = {
  28. 'minSpareRows': 0,
  29. 'startRows': 3,
  30. 'startCols': 3,
  31. 'colHeaders': False,
  32. 'rowHeaders': False,
  33. 'contextMenu': True,
  34. 'editor': 'text',
  35. 'stretchH': 'all',
  36. 'height': 108,
  37. 'language': language,
  38. 'renderer': 'text',
  39. 'autoColumnSize': False,
  40. }
  41. Configuration Options
  42. ^^^^^^^^^^^^^^^^^^^^^
  43. 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:
  44. * `minSpareRows <https://docs.handsontable.com/0.24.3/Options.html#minSpareRows>`_ - The number of rows to append to the end of an empty grid. The default setting is 0.
  45. * `startRows <https://docs.handsontable.com/0.24.3/Options.html#startRows>`_ - The default number of rows for a new table.
  46. * `startCols <https://docs.handsontable.com/0.24.3/Options.html#startCols>`_ - The default number of columns for new tables.
  47. * `colHeaders <https://docs.handsontable.com/0.24.3/Options.html#colHeaders>`_ - Can be set to ``True`` or ``False``. This setting designates if new tables should be created with column rows. **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.
  48. * `rowHeaders <https://docs.handsontable.com/0.24.3/Options.html#rowHeaders>`_ - Operates the same as colHeaders to designate if new tables should be created with the first row as a header. Just like colHeaders this option can be overridden by the page editor in the Wagtail admin.
  49. * `contextMenu <https://docs.handsontable.com/0.24.3/Options.html#contextMenu>`_ - Enables or disables the handsontable right-click menu. By default this is set to ``True``.
  50. * `editor <https://docs.handsontable.com/0.24.3/Options.html#editor>`_ - Defines the editor used for table cells. The default setting is text.
  51. * `stretchH <https://docs.handsontable.com/0.24.3/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.
  52. * `height <https://docs.handsontable.com/0.24.3/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 appearence in the editor.
  53. * `language <https://docs.handsontable.com/0.24.3/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.
  54. * `renderer <https://docs.handsontable.com/0.24.3/Options.html#renderer>`_ - The default setting handsontable uses to render the content of table cells.
  55. * `autoColumnSize <https://docs.handsontable.com/0.24.3/Options.html#autoColumnSize>`_ - Enables or disables the autoColumnSize plugin. The TableBlock default setting is ``False``.
  56. A `complete list of handsontable options <https://docs.handsontable.com/0.24.3/Options.html>`_ can be found on the handsontable website.
  57. Changing the default table_options
  58. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  59. To change the default table options just pass a new table_options dictionary when a new TableBlock is declared.
  60. .. code-block:: python
  61. new_table_options = {
  62. 'minSpareRows': 0,
  63. 'startRows': 6,
  64. 'startCols': 4,
  65. 'colHeaders': False,
  66. 'rowHeaders': False,
  67. 'contextMenu': True,
  68. 'editor': 'text',
  69. 'stretchH': 'all',
  70. 'height': 216,
  71. 'language': 'en',
  72. 'renderer': 'text',
  73. 'autoColumnSize': False,
  74. }
  75. class DemoStreamBlock(StreamBlock):
  76. ...
  77. table = TableBlock(table_options=new_table_options)