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.

122 lines
4.2 KiB

10 years ago
  1. .. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
  2. :alt: License: AGPL-3
  3. ===========================
  4. 2D matrix for x2many fields
  5. ===========================
  6. This module allows to show an x2many field with 3-tuples
  7. ($x_value, $y_value, $value) in a table
  8. $x_value1 $x_value2
  9. ========= =========== ===========
  10. $y_value1 $value(1/1) $value(2/1)
  11. $y_value2 $value(1/2) $value(2/2)
  12. ========= =========== ===========
  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 widget, trying to get this in standard x2many lists involves some quite agly hacks.
  20. Usage
  21. =====
  22. Use this widget by saying::
  23. <field name="my_field" widget="x2many_2d_matrix" />
  24. This assumes that my_field refers to a model with the fields `x`, `y` and
  25. `value`. If your fields are named differently, pass the correct names as
  26. attributes::
  27. <field name="my_field" widget="x2many_2d_matrix" field_x_axis="my_field1" field_y_axis="my_field2" field_value="my_field3" />
  28. You can pass the following parameters:
  29. field_x_axis
  30. The field that indicates the x value of a point
  31. field_y_axis
  32. The field that indicates the y value of a point
  33. field_label_x_axis
  34. Use another field to display in the table header
  35. field_label_y_axis
  36. Use another field to display in the table header
  37. field_value
  38. Show this field as value
  39. show_row_totals
  40. If field_value is a numeric field, calculate row totals
  41. show_column_totals
  42. If field_value is a numeric field, calculate column totals
  43. Example
  44. =======
  45. You need a data structure already filled with values. Let's assume we want to use this widget in a wizard that lets the user fill in planned hours for one task per project per user. In this case, we can use ``project.task`` as our data model and point to it from our wizard. The crucial part is that we fill the field in the default function::
  46. class MyWizard(models.TransientModel):
  47. _name = 'my.wizard'
  48. def _default_task_ids(self):
  49. # your list of project should come from the context, some selection
  50. # in a previous wizard or wherever else
  51. projects = self.env['project.project'].browse([1, 2, 3])
  52. # same with users
  53. users = self.env['res.users'].browse([1, 2, 3])
  54. return [
  55. (0, 0, {'project_id': p.id, 'user_id': u.id, 'planned_hours': 0})
  56. # if the project doesn't have a task for the user, create a new one
  57. if not p.task_ids.filtered(lambda x: x.user_id == u) else
  58. # otherwise, return the task
  59. (4, p.task_ids.filtered(lambda x: x.user_id == u)[0].id)
  60. for p in projects
  61. for u in users
  62. ]
  63. task_ids = fields.Many2many('project.task', default=_default_task_ids)
  64. Now in our wizard, we can use::
  65. <field name="task_ids" widget="x2many_2d_matrix" field_x_axis="project_id" field_y_axis="user_id" field_value="planned_hours" />
  66. Known issues / Roadmap
  67. ======================
  68. * it would be worth trying to instantiate the proper field widget and let it render the input
  69. Bug Tracker
  70. ===========
  71. Bugs are tracked on `GitHub Issues <https://github.com/OCA/web/issues>`_.
  72. In case of trouble, please check there if your issue has already been reported.
  73. If you spotted it first, help us smashing it by providing a detailed and welcomed feedback
  74. `here <https://github.com/OCA/web/issues/new?body=module:%20web_widget_x2many_2d_matrix%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
  75. Credits
  76. =======
  77. Contributors
  78. ------------
  79. * Holger Brunn <hbrunn@therp.nl>
  80. Maintainer
  81. ----------
  82. .. image:: https://odoo-community.org/logo.png
  83. :alt: Odoo Community Association
  84. :target: https://odoo-community.org
  85. This module is maintained by the OCA.
  86. OCA, or the Odoo Community Association, is a nonprofit organization whose
  87. mission is to support the collaborative development of Odoo features and
  88. promote its widespread use.
  89. To contribute to this module, please visit https://odoo-community.org.