excopstig commited on
Commit
d062737
·
verified ·
1 Parent(s): bd6625d

add a button that will generate and download a CSV file of the items added, format the generated csv file in order item name, length, width, height, weight

Browse files
Files changed (1) hide show
  1. script.js +28 -0
script.js CHANGED
@@ -55,6 +55,7 @@ this.addItemBtn = document.getElementById('add-item-btn');
55
  this.updateContainerBtn.addEventListener('click', () => this.updateContainer());
56
  this.clearAllBtn.addEventListener('click', () => this.clearAll());
57
  this.exportBtn.addEventListener('click', () => this.exportToCSV());
 
58
  // Allow adding items with Enter key
59
  this.itemNameInput.addEventListener('keypress', (e) => {
60
  if (e.key === 'Enter') this.addItem();
@@ -381,6 +382,33 @@ const totalArea = this.containerWidth * this.containerHeight;
381
  link.click();
382
  document.body.removeChild(link);
383
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
 
385
  gridPackItems(cellSize = 5) {
386
  const cols = Math.max(1, Math.floor(this.containerWidth / cellSize));
 
55
  this.updateContainerBtn.addEventListener('click', () => this.updateContainer());
56
  this.clearAllBtn.addEventListener('click', () => this.clearAll());
57
  this.exportBtn.addEventListener('click', () => this.exportToCSV());
58
+ this.exportBtn.addEventListener('click', () => this.exportToCSV());
59
  // Allow adding items with Enter key
60
  this.itemNameInput.addEventListener('keypress', (e) => {
61
  if (e.key === 'Enter') this.addItem();
 
382
  link.click();
383
  document.body.removeChild(link);
384
  }
385
+ exportToCSV() {
386
+ if (this.items.length === 0) {
387
+ alert('No items to export');
388
+ return;
389
+ }
390
+
391
+ const headers = ['Item Name', 'Length (cm)', 'Width (cm)', 'Height (cm)', 'Weight (kg)'];
392
+ const csvRows = [
393
+ headers.join(','),
394
+ ...this.items.map(item =>
395
+ [item.name, item.length, item.width, item.length, item.weight] // Using length for height since height isn't tracked separately
396
+ .map(field => `"${String(field).replace(/"/g, '""')}"`) // Proper CSV escaping
397
+ .join(',')
398
+ )
399
+ ];
400
+
401
+ const csvContent = csvRows.join('\n');
402
+ const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
403
+ const url = URL.createObjectURL(blob);
404
+ const link = document.createElement('a');
405
+ link.setAttribute('href', url);
406
+ link.setAttribute('download', `space-allocation-${new Date().toISOString().slice(0,10)}.csv`);
407
+ link.style.visibility = 'hidden';
408
+ document.body.appendChild(link);
409
+ link.click();
410
+ document.body.removeChild(link);
411
+ }
412
 
413
  gridPackItems(cellSize = 5) {
414
  const cols = Math.max(1, Math.floor(this.containerWidth / cellSize));