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.

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