function renderResultsTable(products) {
let selectedProduct = products[0];
const numberFormatter = new Intl.NumberFormat("fr-FR", {
maximumSignificantDigits: 2
});
const formatNumber = (v) =>
htl.html`<div style="text-align: right;">${numberFormatter.format(
+v || 0
)}</div>`;
const formatRow = (d) => {
let view;
const onClick = (e) => {
e.preventDefault();
e.stopPropagation();
selectedProduct = d;
e.target.dispatchEvent(
new CustomEvent("input", {
bubbles: true
})
);
};
const code = htl.html`<span>${d.code}</span>`;
const name = htl.html`<a href="#" onClick=${onClick}>${d.name}</a>`;
const quantity = formatNumber(d.quantity);
const productFootprintCO2 = formatNumber(d.productFootprintCO2);
const perKgFootprintCO2 = formatNumber(d.perKgFootprintCO2);
const productFootprintCO2Off = formatNumber(d.productFootprintCO2Off);
const perKgproductFootprintCO2Off = formatNumber(
d.perKgproductFootprintCO2Off
);
view = htl.html`<tr>
<td>${code}</td>
<td>${name}</td>
<td>${quantity}</td>
<td>${productFootprintCO2}</td>
<td>${perKgFootprintCO2}</td>
<td>${productFootprintCO2Off}</td>
<td>${perKgproductFootprintCO2Off}</td>
</tr>`;
return view;
};
const rows = products.map(formatRow);
const table = htl.html`<table class="welow-table">
<thead>
<tr>
<th rowspan="2">ID</th>
<th rowspan="2">Product Name</th>
<th rowspan="2">Quantity (g)</th>
<th colspan="2">CO<sub>2</sub>e Footprint<br/>Greenly</th>
<th colspan="2">CO<sub>2</sub>e Footprint<br/>Open Food Facts</th>
</tr>
<tr>
<th>Per Product</th>
<th>Per Kg</th>
<th>Per Product</th>
<th>Per Kg</th>
</tr>
</thead>
<tbody>
${rows}
</tbody>
</table>`;
Object.defineProperty(table, "value", {
get: () => selectedProduct
});
return table;
}