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.

118 lines
5.0 KiB

  1. odoo.define('web_export_view', function (require) {
  2. "use strict";
  3. var core = require('web.core');
  4. var Sidebar = require('web.Sidebar');
  5. var session = require('web.session');
  6. var crash_manager = require('web.crash_manager');
  7. var QWeb = core.qweb;
  8. var _t = core._t;
  9. Sidebar.include({
  10. _redraw: function () {
  11. var self = this;
  12. this._super.apply(this, arguments);
  13. if (self.getParent().renderer.viewType == 'list') {
  14. session.user_has_group('web_export_view.group_disallow_export_view_data_excel').then(function (has_group) {
  15. if (!has_group) {
  16. self.$el.find('.o_dropdown').last().append(QWeb.render('WebExportTreeViewXls', { widget: self }));
  17. self.$el.find('.export_treeview_xls').on('click', self.on_sidebar_export_treeview_xls);
  18. }
  19. });
  20. }
  21. },
  22. on_sidebar_export_treeview_xls: function () {
  23. // Select the first list of the current (form) view
  24. // or assume the main view is a list view and use that
  25. var self = this,
  26. view = this.getParent(),
  27. children = view.getChildren();
  28. var c = crash_manager;
  29. if (children) {
  30. children.every(function (child) {
  31. if (child.field && child.field.type == 'one2many') {
  32. view = child.viewmanager.views.list.controller;
  33. return false; // break out of the loop
  34. }
  35. if (child.field && child.field.type == 'many2many') {
  36. view = child.list_view;
  37. return false; // break out of the loop
  38. }
  39. return true;
  40. });
  41. }
  42. var export_columns_keys = [];
  43. var export_columns_names = [];
  44. var column_index = 0;
  45. var column_header_selector;
  46. $.each(view.renderer.columns, function () {
  47. if (this.tag == 'field' && (this.attrs.widget === undefined || this.attrs.widget != 'handle')) {
  48. // non-fields like `_group` or buttons
  49. export_columns_keys.push(column_index);
  50. column_header_selector = '.o_list_view > thead > tr> th:not([class*="o_list_record_selector"]):eq('+column_index+')';
  51. export_columns_names.push(view.$el.find(column_header_selector)[0].textContent);
  52. }
  53. column_index ++;
  54. });
  55. var export_rows = [];
  56. $.blockUI();
  57. if (children) {
  58. // find only rows with data
  59. view.$el.find('.o_list_view > tbody > tr.o_data_row:has(.o_list_record_selector input:checkbox:checked)')
  60. .each(function () {
  61. var $row = $(this);
  62. var export_row = [];
  63. $.each(export_columns_keys, function () {
  64. var $cell = $row.find('td.o_data_cell:eq('+this+')')
  65. var $cellcheckbox = $cell.find('.o_checkbox input:checkbox');
  66. if ($cellcheckbox.length) {
  67. export_row.push(
  68. $cellcheckbox.is(":checked")
  69. ? _t("True") : _t("False")
  70. );
  71. }
  72. else {
  73. var text = $cell.text().trim();
  74. var is_number = (
  75. $cell.hasClass('o_list_number') &&
  76. !$cell.hasClass('o_float_time_cell')
  77. );
  78. if (is_number) {
  79. export_row.push(parseFloat(
  80. text
  81. // Remove thousands separator
  82. .split(_t.database.parameters.thousands_sep)
  83. .join("")
  84. // Always use a `.` as decimal separator
  85. .replace(_t.database.parameters.decimal_point, ".")
  86. // Remove non-numeric characters
  87. .replace(/[^\d\.-]/g, "")
  88. ));
  89. } else {
  90. export_row.push(text);
  91. }
  92. }
  93. });
  94. export_rows.push(export_row);
  95. });
  96. }
  97. session.get_file({
  98. url: '/web/export/xls_view',
  99. data: {data: JSON.stringify({
  100. model: view.modelName,
  101. headers: export_columns_names,
  102. rows: export_rows
  103. })},
  104. complete: $.unblockUI,
  105. error: c.rpc_error.bind(c)
  106. });
  107. }
  108. });
  109. });