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.

185 lines
6.2 KiB

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