-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Kampus-Merdeka-Software-Engineering/nahru…
…l/addtable Add table below Row 3
- Loading branch information
Showing
4 changed files
with
164 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
let productSums = []; | ||
|
||
async function getData() { | ||
try { | ||
const response = await fetch("./data/bikesales.json"); | ||
const data = await response.json(); | ||
|
||
const productSumsMap = {}; | ||
|
||
data.bikesalesdata.forEach(user => { | ||
const { Product, Sub_Category, Country, Revenue, Profit, Order_Quantity } = user; | ||
|
||
if (!productSumsMap[Product]) { | ||
productSumsMap[Product] = { | ||
Product, | ||
Sub_Category, | ||
Country, | ||
Revenue: 0, | ||
Profit: 0, | ||
Order_Quantity: 0 | ||
}; | ||
} | ||
|
||
productSumsMap[Product].Revenue += parseFloat(Revenue); | ||
productSumsMap[Product].Profit += parseFloat(Profit); | ||
productSumsMap[Product].Order_Quantity += parseInt(Order_Quantity, 10); | ||
}); | ||
|
||
productSums = Object.values(productSumsMap); | ||
populateTable(productSums); | ||
|
||
$('#user-table').DataTable({ | ||
data: productSums, | ||
columns: [ | ||
{ data: "Product" }, | ||
{ data: "Sub_Category" }, | ||
{ data: "Country" }, | ||
{ data: "Revenue" }, | ||
{ data: "Profit" }, | ||
{ data: "Order_Quantity" } | ||
] | ||
}); | ||
} catch (error) { | ||
console.error('Error fetching data:', error); | ||
} | ||
} | ||
|
||
function populateTable(data) { | ||
let table = ''; | ||
data.forEach(product => { | ||
table += ` | ||
<tr> | ||
<td>${product.Product}</td> | ||
<td>${product.Sub_Category}</td> | ||
<td>${product.Country}</td> | ||
<td>$${product.Revenue.toFixed(2)}</td> | ||
<td>$${product.Profit.toFixed(2)}</td> | ||
<td>${product.Order_Quantity}</td> | ||
</tr> | ||
`; | ||
}); | ||
document.getElementById("data-output").innerHTML = table; | ||
} | ||
|
||
window.onload = getData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters