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.

92 lines
3.1 KiB

  1. Use this widget by saying::
  2. <field name="my_field" widget="x2many_2d_matrix" />
  3. This assumes that my_field refers to a model with the fields `x`, `y` and
  4. `value`. If your fields are named differently, pass the correct names as
  5. attributes:
  6. .. code-block:: xml
  7. <field name="my_field" widget="x2many_2d_matrix" field_x_axis="my_field1" field_y_axis="my_field2" field_value="my_field3">
  8. <tree>
  9. <field name="my_field"/>
  10. <field name="my_field1"/>
  11. <field name="my_field2"/>
  12. <field name="my_field3"/>
  13. </tree>
  14. </field>
  15. You can pass the following parameters:
  16. field_x_axis
  17. The field that indicates the x value of a point
  18. field_y_axis
  19. The field that indicates the y value of a point
  20. field_label_x_axis
  21. Use another field to display in the table header
  22. field_label_y_axis
  23. Use another field to display in the table header
  24. field_value
  25. Show this field as value
  26. show_row_totals
  27. If field_value is a numeric field, it indicates if you want to calculate
  28. row totals. True by default
  29. show_column_totals
  30. If field_value is a numeric field, it indicates if you want to calculate
  31. column totals. True by default
  32. Example
  33. ~~~~~~~
  34. You need a data structure already filled with values. Let's assume we want to
  35. use this widget in a wizard that lets the user fill in planned hours for one
  36. task per project per user. In this case, we can use ``project.task`` as our
  37. data model and point to it from our wizard. The crucial part is that we fill
  38. the field in the default function:
  39. .. code-block:: python
  40. from odoo import fields, models
  41. class MyWizard(models.TransientModel):
  42. _name = 'my.wizard'
  43. def _default_task_ids(self):
  44. # your list of project should come from the context, some selection
  45. # in a previous wizard or wherever else
  46. projects = self.env['project.project'].browse([1, 2, 3])
  47. # same with users
  48. users = self.env['res.users'].browse([1, 2, 3])
  49. return [
  50. (0, 0, {
  51. 'name': 'Sample task name',
  52. 'project_id': p.id,
  53. 'user_id': u.id,
  54. 'planned_hours': 0,
  55. 'message_needaction': False,
  56. 'date_deadline': fields.Date.today(),
  57. })
  58. # if the project doesn't have a task for the user,
  59. # create a new one
  60. if not p.task_ids.filtered(lambda x: x.user_id == u) else
  61. # otherwise, return the task
  62. (4, p.task_ids.filtered(lambda x: x.user_id == u)[0].id)
  63. for p in projects
  64. for u in users
  65. ]
  66. task_ids = fields.Many2many('project.task', default=_default_task_ids)
  67. Now in our wizard, we can use:
  68. .. code-block:: xml
  69. <field name="task_ids" widget="x2many_2d_matrix" field_x_axis="project_id" field_y_axis="user_id" field_value="planned_hours">
  70. <tree>
  71. <field name="task_ids"/>
  72. <field name="project_id"/>
  73. <field name="user_id"/>
  74. <field name="planned_hours"/>
  75. </tree>
  76. </field>