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.

149 lines
5.1 KiB

9 years ago
9 years ago
9 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. | | $x_value1 | $x_value2 |
  10. | :---: | :---: | :---: |
  11. | $y_value1 | $value(1/1) | $value(2/1) |
  12. | $y_value2 | $value(1/2) | $value(2/2) |
  13. where `value(n/n)` is editable.
  14. An example use case would be: Select some projects and some employees so that
  15. a manager can easily fill in the planned_hours for one task per employee. The
  16. result could look like this:
  17. .. image:: /web_widget_x2many_2d_matrix/static/description/screenshot.png
  18. :alt: Screenshot
  19. The beauty of this is that you have an arbitrary amount of columns with this
  20. widget, trying to get this in standard x2many lists involves some quite ugly
  21. hacks.
  22. Note: The order of axis values depends on their order in the matrix you provide.
  23. Usage
  24. =====
  25. Use this widget by saying::
  26. <field name="my_field" widget="x2many_2d_matrix" />
  27. This assumes that my_field refers to a model with the fields `x`, `y` and
  28. `value`. If your fields are named differently, pass the correct names as
  29. attributes::
  30. <field name="my_field" widget="x2many_2d_matrix" field_x_axis="my_field1" field_y_axis="my_field2" field_value="my_field3" />
  31. You can pass the following parameters:
  32. field_x_axis
  33. The field that indicates the x value of a point
  34. field_y_axis
  35. The field that indicates the y value of a point
  36. x_axis_clickable
  37. It indicates if the X axis allows to be clicked for navigating to the field
  38. (if it's a many2one field). True by default
  39. y_axis_clickable
  40. It indicates if the Y axis allows to be clicked for navigating to the field
  41. (if it's a many2one field). True by default
  42. field_label_x_axis
  43. Use another field to display in the table header
  44. field_label_y_axis
  45. Use another field to display in the table header
  46. field_value
  47. Show this field as value
  48. show_row_totals
  49. If field_value is a numeric field, calculate row totals
  50. show_column_totals
  51. If field_value is a numeric field, calculate column totals
  52. field_att_<name>
  53. Declare as many options prefixed with this string as you need for binding
  54. a field value with an HTML node attribute (disabled, class, style...)
  55. called as the `<name>` passed in the option.
  56. .. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
  57. :alt: Try me on Runbot
  58. :target: https://runbot.odoo-community.org/runbot/162/8.0
  59. Example
  60. =======
  61. You need a data structure already filled with values. Let's assume we want to
  62. use this widget in a wizard that lets the user fill in planned hours for one
  63. task per project per user. In this case, we can use ``project.task`` as our
  64. data model and point to it from our wizard. The crucial part is that we fill
  65. the field in the default function::
  66. class MyWizard(models.TransientModel):
  67. _name = 'my.wizard'
  68. def _default_task_ids(self):
  69. # your list of project should come from the context, some selection
  70. # in a previous wizard or wherever else
  71. projects = self.env['project.project'].browse([1, 2, 3])
  72. # same with users
  73. users = self.env['res.users'].browse([1, 2, 3])
  74. return [
  75. (0, 0, {'project_id': p.id, 'user_id': u.id, 'planned_hours': 0})
  76. # if the project doesn't have a task for the user, create a new one
  77. if not p.task_ids.filtered(lambda x: x.user_id == u) else
  78. # otherwise, return the task
  79. (4, p.task_ids.filtered(lambda x: x.user_id == u)[0].id)
  80. for p in projects
  81. for u in users
  82. ]
  83. task_ids = fields.Many2many('project.task', default=_default_task_ids)
  84. Now in our wizard, we can use::
  85. <field name="task_ids" widget="x2many_2d_matrix" field_x_axis="project_id" field_y_axis="user_id" field_value="planned_hours" />
  86. Note that all values in the matrix must exist, so you need to create them
  87. previously if not present, but you can control visually the editability of
  88. the fields in the matrix through `field_att_disabled` option with a control
  89. field.
  90. Known issues / Roadmap
  91. ======================
  92. * it would be worth trying to instantiate the proper field widget and let it render the input
  93. Bug Tracker
  94. ===========
  95. Bugs are tracked on `GitHub Issues
  96. <https://github.com/OCA/web/issues>`_. In case of trouble, please
  97. check there if your issue has already been reported. If you spotted it first,
  98. help us smashing it by providing a detailed and welcomed feedback.
  99. Credits
  100. =======
  101. Contributors
  102. ------------
  103. * Holger Brunn <hbrunn@therp.nl>
  104. * Pedro M. Baeza <pedro.baeza@tecnativa.com>
  105. Maintainer
  106. ----------
  107. .. image:: https://odoo-community.org/logo.png
  108. :alt: Odoo Community Association
  109. :target: https://odoo-community.org
  110. This module is maintained by the OCA.
  111. OCA, or the Odoo Community Association, is a nonprofit organization whose
  112. mission is to support the collaborative development of Odoo features and
  113. promote its widespread use.
  114. To contribute to this module, please visit https://odoo-community.org.