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.

104 lines
4.4 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 QWeb = core.qweb;
  6. var _t = core._t;
  7. Sidebar.include({
  8. redraw: function () {
  9. var self = this;
  10. this._super.apply(this, arguments);
  11. if (self.getParent().ViewManager.active_view.type == 'list') {
  12. self.$el.find('.o_dropdown').last().append(QWeb.render('WebExportTreeViewXls', {widget: self}));
  13. self.$el.find('.export_treeview_xls').on('click', self.on_sidebar_export_treeview_xls);
  14. }
  15. },
  16. on_sidebar_export_treeview_xls: function () {
  17. // Select the first list of the current (form) view
  18. // or assume the main view is a list view and use that
  19. var self = this,
  20. view = this.getParent(),
  21. children = view.getChildren();
  22. if (children) {
  23. children.every(function (child) {
  24. if (child.field && child.field.type == 'one2many') {
  25. view = child.viewmanager.views.list.controller;
  26. return false; // break out of the loop
  27. }
  28. if (child.field && child.field.type == 'many2many') {
  29. view = child.list_view;
  30. return false; // break out of the loop
  31. }
  32. return true;
  33. });
  34. }
  35. var export_columns_keys = [];
  36. var export_columns_names = [];
  37. $.each(view.visible_columns, function () {
  38. if (this.tag == 'field' && (this.widget === undefined || this.widget != 'handle')) {
  39. // non-fields like `_group` or buttons
  40. export_columns_keys.push(this.id);
  41. export_columns_names.push(this.string);
  42. }
  43. });
  44. var rows = view.$el.find('.o_list_view > tbody > tr');
  45. var export_rows = [];
  46. $.each(rows, function () {
  47. var $row = $(this);
  48. // find only rows with data
  49. if ($row.attr('data-id')) {
  50. var export_row = [];
  51. var checked = $row.find('.o_list_record_selector input[type=checkbox]').is(':checked');
  52. if (children && checked === true) {
  53. $.each(export_columns_keys, function () {
  54. var $cell = $row.find('td[data-field="' + this + '"]')
  55. var $cellcheckbox = $cell.find('.o_checkbox input[type=checkbox]');
  56. if ($cellcheckbox.length) {
  57. if ($cellcheckbox.is(':checked')) {
  58. export_row.push(_t("True"));
  59. }
  60. else {
  61. export_row.push(_t("False"));
  62. }
  63. }
  64. else {
  65. var cell = $cell.get(0);
  66. var text = cell.text || cell.textContent || cell.innerHTML || "";
  67. if (cell.classList.contains("o_list_number")) {
  68. var tmp2 = text;
  69. do {
  70. var tmp = tmp2;
  71. tmp2 = tmp.replace(_t.database.parameters.thousands_sep, "");
  72. } while (tmp !== tmp2);
  73. tmp2 = tmp.replace(_t.database.parameters.decimal_point, ".");
  74. export_row.push(parseFloat(tmp2));
  75. }
  76. else {
  77. export_row.push(text.trim());
  78. }
  79. }
  80. });
  81. export_rows.push(export_row);
  82. }
  83. }
  84. });
  85. $.blockUI();
  86. view.session.get_file({
  87. url: '/web/export/xls_view',
  88. data: {data: JSON.stringify({
  89. model: view.model,
  90. headers: export_columns_names,
  91. rows: export_rows
  92. })},
  93. complete: $.unblockUI
  94. });
  95. }
  96. });
  97. });