You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.0 KiB

10 years ago
10 years ago
10 years ago
  1. .. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
  2. :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
  3. :alt: License: AGPL-3
  4. ===========================
  5. 2D matrix for x2many fields
  6. ===========================
  7. This module allows to show an x2many field with 3-tuples
  8. ($x_value, $y_value, $value) in a table
  9. +-----------+-------------+-------------+
  10. | | $x_value1 | $x_value2 |
  11. +===========+=============+=============+
  12. | $y_value1 | $value(1/1) | $value(2/1) |
  13. +-----------+-------------+-------------+
  14. | $y_value2 | $value(1/2) | $value(2/2) |
  15. +-----------+-------------+-------------+
  16. where `value(n/n)` is editable.
  17. An example use case would be: Select some projects and some employees so that
  18. a manager can easily fill in the planned_hours for one task per employee. The
  19. result could look like this:
  20. .. image:: /web_widget_x2many_2d_matrix/static/description/screenshot.png
  21. :alt: Screenshot
  22. The beauty of this is that you have an arbitrary amount of columns with this
  23. widget, trying to get this in standard x2many lists involves some quite ugly
  24. hacks.
  25. Usage
  26. =====
  27. Use this widget by saying::
  28. <field name="my_field" widget="x2many_2d_matrix" />
  29. This assumes that my_field refers to a model with the fields `x`, `y` and
  30. `value`. If your fields are named differently, pass the correct names as
  31. attributes:
  32. .. code-block:: xml
  33. <field name="my_field" widget="x2many_2d_matrix" field_x_axis="my_field1" field_y_axis="my_field2" field_value="my_field3">
  34. <tree>
  35. <field name="my_field"/>
  36. <field name="my_field1"/>
  37. <field name="my_field2"/>
  38. <field name="my_field3"/>
  39. </tree>
  40. </field>
  41. You can pass the following parameters:
  42. field_x_axis
  43. The field that indicates the x value of a point
  44. field_y_axis
  45. The field that indicates the y value of a point
  46. field_label_x_axis
  47. Use another field to display in the table header
  48. field_label_y_axis
  49. Use another field to display in the table header
  50. field_value
  51. Show this field as value
  52. show_row_totals
  53. If field_value is a numeric field, it indicates if you want to calculate
  54. row totals. True by default
  55. show_column_totals
  56. If field_value is a numeric field, it indicates if you want to calculate
  57. column totals. True by default
  58. .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
  59. :alt: Try me on Runbot
  60. :target: https://runbot.odoo-community.org/runbot/162/11.0
  61. Example
  62. =======
  63. You need a data structure already filled with values. Let's assume we want to
  64. use this widget in a wizard that lets the user fill in planned hours for one
  65. task per project per user. In this case, we can use ``project.task`` as our
  66. data model and point to it from our wizard. The crucial part is that we fill
  67. the field in the default function:
  68. .. code-block:: python
  69. from odoo import fields, models
  70. class MyWizard(models.TransientModel):
  71. _name = 'my.wizard'
  72. def _default_task_ids(self):
  73. # your list of project should come from the context, some selection
  74. # in a previous wizard or wherever else
  75. projects = self.env['project.project'].browse([1, 2, 3])
  76. # same with users
  77. users = self.env['res.users'].browse([1, 2, 3])
  78. return [
  79. (0, 0, {
  80. 'name': 'Sample task name',
  81. 'project_id': p.id,
  82. 'user_id': u.id,
  83. 'planned_hours': 0,
  84. 'message_needaction': False,
  85. 'date_deadline': fields.Date.today(),
  86. })
  87. # if the project doesn't have a task for the user,
  88. # create a new one
  89. if not p.task_ids.filtered(lambda x: x.user_id == u) else
  90. # otherwise, return the task
  91. (4, p.task_ids.filtered(lambda x: x.user_id == u)[0].id)
  92. for p in projects
  93. for u in users
  94. ]
  95. task_ids = fields.Many2many('project.task', default=_default_task_ids)
  96. Now in our wizard, we can use:
  97. .. code-block:: xml
  98. <field name="task_ids" widget="x2many_2d_matrix" field_x_axis="project_id" field_y_axis="user_id" field_value="planned_hours">
  99. <tree>
  100. <field name="task_ids"/>
  101. <field name="project_id"/>
  102. <field name="user_id"/>
  103. <field name="planned_hours"/>
  104. </tree>
  105. </field>
  106. Known issues / Roadmap
  107. ======================
  108. * Support extra attributes on each field cell via `field_extra_attrs` param.
  109. We could set a cell as not editable, required or readonly for instance.
  110. The `readonly` case will also give the ability
  111. to click on m2o to open related records.
  112. * Support limit total records in the matrix. Ref: https://github.com/OCA/web/issues/901
  113. * Support cell traversal through keyboard arrows.
  114. * Entering the widget from behind by pressing ``Shift+TAB`` in your keyboard
  115. will enter into the 1st cell until https://github.com/odoo/odoo/pull/26490
  116. is merged.
  117. * Support extra invisible fields inside each cell.
  118. Bug Tracker
  119. ===========
  120. Bugs are tracked on `GitHub Issues
  121. <https://github.com/OCA/web/issues>`_. In case of trouble, please
  122. check there if your issue has already been reported. If you spotted it first,
  123. help us smash it by providing a detailed and welcomed feedback.
  124. Credits
  125. =======
  126. Contributors
  127. ------------
  128. * Holger Brunn <hbrunn@therp.nl>
  129. * Pedro M. Baeza <pedro.baeza@tecnativa.com>
  130. * Artem Kostyuk <a.kostyuk@mobilunity.com>
  131. * Simone Orsi <simone.orsi@camptocamp.com>
  132. * Timon Tschanz <timon.tschanz@camptocamp.com>
  133. * Jairo Llopis <jairo.llopis@tecnativa.com>
  134. * Dennis Sluijk <d.sluijk@onestein.nl>
  135. Maintainer
  136. ----------
  137. .. image:: https://odoo-community.org/logo.png
  138. :alt: Odoo Community Association
  139. :target: https://odoo-community.org
  140. This module is maintained by the OCA.
  141. OCA, or the Odoo Community Association, is a nonprofit organization whose
  142. mission is to support the collaborative development of Odoo features and
  143. promote its widespread use.
  144. To contribute to this module, please visit https://odoo-community.org.