chartManD commited on
Commit
8f7151b
·
1 Parent(s): 5ecd3ea

Descarga de datos de sesiones con Sort

Browse files
tecnicas/static/js/download-table-csv.js CHANGED
@@ -4,7 +4,7 @@ document.addEventListener("DOMContentLoaded", function () {
4
 
5
  btn.addEventListener("click", function () {
6
  // Try set the table in the page
7
- let table = document.getElementById("convencional-table");
8
  if (!table) {
9
  const section = btn.closest("section");
10
  if (section) table = section.querySelector("table");
 
4
 
5
  btn.addEventListener("click", function () {
6
  // Try set the table in the page
7
+ let table = document.getElementById("generic-donwload-table");
8
  if (!table) {
9
  const section = btn.closest("section");
10
  if (section) table = section.querySelector("table");
tecnicas/static/js/download-table-sort-csv.js ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ document.addEventListener("DOMContentLoaded", function () {
2
+ const btn = document.getElementById("download-csv-btn");
3
+ if (!btn) return;
4
+
5
+ btn.addEventListener("click", function () {
6
+ // Try set the table in the page
7
+ let table = document.getElementById("generic-donwload-table");
8
+ if (!table) {
9
+ const section = btn.closest("section");
10
+ if (section) table = section.querySelector("table");
11
+ }
12
+ if (!table) {
13
+ console.warn("No se encontró la tabla para descargar.");
14
+ return;
15
+ }
16
+
17
+ // helper to trim and normalize cell text
18
+ const cellText = (cell) => {
19
+ if (!cell) return "";
20
+ return String(cell.textContent || "").trim();
21
+ };
22
+
23
+ // Collect headers
24
+ const headers = [];
25
+ const ths = table.querySelectorAll("thead th");
26
+ ths.forEach((th) => headers.push(cellText(th)));
27
+
28
+ // Collect rows
29
+ const rows = [];
30
+ const trs = table.querySelectorAll("tbody tr");
31
+ trs.forEach((tr) => {
32
+ const cols = [];
33
+ const tds = tr.querySelectorAll("td");
34
+ tds.forEach((td) => cols.push(cellText(td)));
35
+ rows.push(cols);
36
+ });
37
+
38
+ // Convert to CSV string (escape quotes, wrap in quotes if needed)
39
+ const escapeValue = (val) => {
40
+ if (val == null) return "";
41
+ let normalVal = val.replace(/[\u0300-\u036f]/g, "");
42
+ normalVal = normalVal.replace(/(\r\n|\n|\r|\s)/gm, '');
43
+ const needsQuotes = /[",\n,;]/.test(normalVal);
44
+ let v = normalVal.replace(/"/g, '""');
45
+ if (needsQuotes) v = `"${v}"`;
46
+ return v;
47
+ };
48
+
49
+ const lines = [];
50
+ if (headers.length) lines.push(headers.map(escapeValue).join(","));
51
+ rows.forEach((r) => lines.push(r.map(escapeValue).join(",")));
52
+
53
+ const csvContent = lines.join("\n");
54
+
55
+ // File name: data_{nombre_sesion or codigo_sesion}
56
+ const rawName = (btn.dataset.sessionName || "").trim();
57
+ const code = (btn.dataset.sessionCode || "").trim() || "session";
58
+ const namePart = rawName
59
+ ? rawName.replace(/[^a-zA-Z0-9-_áéíóúÁÉÍÓÚ ]/g, "").replace(/\s+/g, "_")
60
+ : code;
61
+ const fileName = `data_${namePart}.csv`;
62
+
63
+ // Create blob and force download
64
+ const blob = new Blob([csvContent], { type: "text/csv;charset=UTF-8;" });
65
+ if (navigator.msSaveBlob) {
66
+ navigator.msSaveBlob(blob, fileName);
67
+ } else {
68
+ const link = document.createElement("a");
69
+ const url = URL.createObjectURL(blob);
70
+ link.setAttribute("href", url);
71
+ link.setAttribute("download", fileName);
72
+ link.style.visibility = "hidden";
73
+ document.body.appendChild(link);
74
+ link.click();
75
+ document.body.removeChild(link);
76
+ URL.revokeObjectURL(url);
77
+ }
78
+ });
79
+ });
tecnicas/templates/tecnicas/components/table-scales.html CHANGED
@@ -1,7 +1,7 @@
1
  {% load static %}
2
  <section>
3
  <div class="overflow-x-auto rounded-lg border border-surface-general">
4
- <table id="convencional-table" class="min-w-max w-full text-sm text-center border-collapse">
5
  <thead class="bg-surface-sweet text-black font-semibold">
6
  <tr>
7
  {% if sesion.tecnica.tipo_tecnica.nombre_tecnica != "rata" %}
 
1
  {% load static %}
2
  <section>
3
  <div class="overflow-x-auto rounded-lg border border-surface-general">
4
+ <table id="generic-donwload-table" class="min-w-max w-full text-sm text-center border-collapse">
5
  <thead class="bg-surface-sweet text-black font-semibold">
6
  <tr>
7
  {% if sesion.tecnica.tipo_tecnica.nombre_tecnica != "rata" %}
tecnicas/templates/tecnicas/components/table-sort.html CHANGED
@@ -1,5 +1,6 @@
1
  {% load custom_filters %}
2
- <section>
 
3
  <div class="overflow-x-auto rounded-lg border border-surface-general">
4
  <table class="min-w-max w-full text-sm text-center border-collapse">
5
  <thead class="bg-surface-sweet text-black font-semibold">
@@ -15,20 +16,27 @@
15
  <tbody class="bg-surface-ligt divide-y divide-gray-200 text-black">
16
  {% for data_words in data_groups.data %}
17
  <tr>
18
- <td class="py-2 px-3 border border-surface-general font-medium">{{ data_words.codigo_producto }}</td>
 
19
  {% for tester in data_groups.testers %}
20
- <td class="py-2 px-3 border border-surface-general font-medium">
21
- {% for word in data_words.palabras|get_item:tester %}
22
- {{word}}
23
- {% if not forloop.last %}
24
- ;
25
- {% endif %}
26
- {% endfor %}
27
- </td>
28
  {% endfor %}
29
  </tr>
30
  {% endfor %}
31
  </tbody>
32
  </table>
33
  </div>
34
- </section>
 
 
 
 
 
 
 
 
 
 
1
  {% load custom_filters %}
2
+ {% load static %}
3
+ <section class="space-y-4">
4
  <div class="overflow-x-auto rounded-lg border border-surface-general">
5
  <table class="min-w-max w-full text-sm text-center border-collapse">
6
  <thead class="bg-surface-sweet text-black font-semibold">
 
16
  <tbody class="bg-surface-ligt divide-y divide-gray-200 text-black">
17
  {% for data_words in data_groups.data %}
18
  <tr>
19
+ <td class="py-2 px-3 border border-surface-general font-medium">{{ data_words.codigo_producto }}
20
+ </td>
21
  {% for tester in data_groups.testers %}
22
+ <td class="py-2 px-3 border border-surface-general font-medium">
23
+ {% for word in data_words.palabras|get_item:tester %}
24
+ {{word}}{% if not forloop.last %} ;{% endif %}
25
+ {% endfor %}
26
+ </td>
 
 
 
27
  {% endfor %}
28
  </tr>
29
  {% endfor %}
30
  </tbody>
31
  </table>
32
  </div>
33
+
34
+ <div class="flex justify-end">
35
+ <button id="download-csv-btn" class="cts-btn-general cts-btn-primary btn-push"
36
+ data-session-name="{{ sesion.nombre_sesion }}" data-session-code="{{ sesion.codigo_sesion }}">
37
+ Descargar datos en CSV
38
+ </button>
39
+ </div>
40
+ </section>
41
+
42
+ <script src="{% static 'js/download-table-sort-csv.js' %}"></script>
tecnicas/templates/tecnicas/components/table_cata.html CHANGED
@@ -1,7 +1,7 @@
1
  {% load static %}
2
  <section>
3
  <div class="overflow-x-auto rounded-lg border border-surface-general">
4
- <table id="convencional-table" class="min-w-max w-full text-sm text-center border-collapse">
5
  <thead class="bg-surface-sweet text-black font-semibold">
6
  <tr>
7
  <th class="py-2 px-3 border border-surface-general">Usuario</th>
 
1
  {% load static %}
2
  <section>
3
  <div class="overflow-x-auto rounded-lg border border-surface-general">
4
+ <table id="generic-donwload-table" class="min-w-max w-full text-sm text-center border-collapse">
5
  <thead class="bg-surface-sweet text-black font-semibold">
6
  <tr>
7
  <th class="py-2 px-3 border border-surface-general">Usuario</th>
tecnicas/templates/tecnicas/components/table_pf.html CHANGED
@@ -5,7 +5,7 @@
5
  <h3 class="text-xl font-bold">Datos de usuario {{ data.tester }}</h3>
6
 
7
  <div class="overflow-x-auto rounded-lg border border-surface-general">
8
- <table class="convencional-table min-w-max w-full text-sm text-center border-collapse">
9
  <thead class="bg-surface-sweet text-black font-semibold">
10
  <tr>
11
  <th class="py-2 px-3 border border-surface-general">Repetición</th>
 
5
  <h3 class="text-xl font-bold">Datos de usuario {{ data.tester }}</h3>
6
 
7
  <div class="overflow-x-auto rounded-lg border border-surface-general">
8
+ <table class="generic-donwload-table min-w-max w-full text-sm text-center border-collapse">
9
  <thead class="bg-surface-sweet text-black font-semibold">
10
  <tr>
11
  <th class="py-2 px-3 border border-surface-general">Repetición</th>
tecnicas/templates/tecnicas/manage_sesions/details-session-cata.html CHANGED
@@ -218,8 +218,7 @@
218
  </p>
219
  </section>
220
  {% if existen_calificaciones %}
221
- {% include "../components/table_cata.html" with calificaciones=calificaciones palabras=palabras
222
- sesion=sesion %}
223
  {% else %}
224
  {% include "../components/error-message.html" with message='Sin calificaciones que mostrar aún' %}
225
  {% endif %}
 
218
  </p>
219
  </section>
220
  {% if existen_calificaciones %}
221
+ {% include "../components/table_cata.html" with calificaciones=calificaciones palabras=palabras sesion=sesion %}
 
222
  {% else %}
223
  {% include "../components/error-message.html" with message='Sin calificaciones que mostrar aún' %}
224
  {% endif %}
tecnicas/templates/tecnicas/manage_sesions/details-session-sort.html CHANGED
@@ -187,7 +187,7 @@
187
  </p>
188
 
189
  {% if there_data %}
190
- {% include "../components/table-sort.html" with data_groups=data_groups %}
191
  {% else %}
192
  {% include "../components/error-message.html" with message='Sin datos por mostrar aún' %}
193
  {% endif %}
 
187
  </p>
188
 
189
  {% if there_data %}
190
+ {% include "../components/table-sort.html" with data_groups=data_groups sesion=sesion %}
191
  {% else %}
192
  {% include "../components/error-message.html" with message='Sin datos por mostrar aún' %}
193
  {% endif %}