-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
146 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 |
---|---|---|
@@ -1,136 +1,48 @@ | ||
// Fetch the JSON data and populate the table | ||
fetch('motors.json') | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error('Network response was not ok ' + response.statusText); | ||
} | ||
return response.json(); | ||
}) | ||
.then(motors => { | ||
generateFilters(motors); | ||
populateTable(motors); | ||
}) | ||
.catch(error => console.error('Error fetching data:', error)); | ||
|
||
function populateTable(motors) { | ||
const tbody = document.querySelector('#motor-table tbody'); | ||
tbody.innerHTML = ''; | ||
|
||
motors.forEach(motor => { | ||
const row = document.createElement('tr'); | ||
|
||
row.innerHTML = ` | ||
<td>${motor.name || ''}</td> | ||
<td>${motor.cooling || ''}</td> | ||
<td>${motor.rated_voltage_v || ''}</td> | ||
<td>${motor.continuous_torque_nm || ''}</td> | ||
<td>${motor.peak_torque_nm || ''}</td> | ||
<td>${motor.max_rotation_speed_rpm || ''}</td> | ||
<td>${motor.pole_pairs || ''}</td> | ||
<td>${motor.rotor_inertia_kg_cm2 || ''}</td> | ||
<td>${motor.diameter_mm || ''}</td> | ||
<td>${motor.length_mm || ''}</td> | ||
<td>${motor.weight_g || ''}</td> | ||
<td>${motor.rotor_inner_diameter_mm || ''}</td> | ||
`; | ||
let motorData = []; | ||
|
||
tbody.appendChild(row); | ||
}); | ||
} | ||
|
||
function generateFilters(motors) { | ||
const filtersDiv = document.getElementById('filters'); | ||
|
||
// Define the parameters you want to create filters for | ||
const filterParams = [ | ||
{ key: 'cooling', label: 'Cooling' }, | ||
{ key: 'rated_voltage_v', label: 'Rated Voltage (V)' }, | ||
{ key: 'continuous_torque_nm', label: 'Continuous Torque (Nm)' }, | ||
{ key: 'peak_torque_nm', label: 'Peak Torque (Nm)' }, | ||
{ key: 'max_rotation_speed_rpm', label: 'Max Rotation Speed (rpm)' }, | ||
{ key: 'pole_pairs', label: 'Pole Pairs' }, | ||
{ key: 'rotor_inertia_kg_cm2', label: 'Rotor Inertia (kg·cm²)' }, | ||
{ key: 'diameter_mm', label: 'Diameter (mm)' }, | ||
{ key: 'length_mm', label: 'Length (mm)' }, | ||
{ key: 'weight_g', label: 'Weight (g)' }, | ||
{ key: 'rotor_inner_diameter_mm', label: 'Rotor Inner Diameter (mm)' } | ||
]; | ||
|
||
filterParams.forEach(param => { | ||
createFilter(motors, param.key, param.label); | ||
}); | ||
} | ||
|
||
function createFilter(motors, key, labelText) { | ||
const filtersDiv = document.getElementById('filters'); | ||
|
||
const values = [...new Set(motors.map(motor => motor[key]).filter(value => value !== null && value !== undefined))]; | ||
|
||
// Sort values if they are numbers | ||
if (typeof values[0] === 'number') { | ||
values.sort((a, b) => a - b); | ||
} else { | ||
values.sort(); | ||
} | ||
|
||
const label = document.createElement('label'); | ||
label.textContent = `${labelText}: `; | ||
label.style.marginRight = '10px'; | ||
|
||
const select = document.createElement('select'); | ||
select.id = `${key}-filter`; | ||
|
||
const allOption = document.createElement('option'); | ||
allOption.value = ''; | ||
allOption.textContent = 'All'; | ||
select.appendChild(allOption); | ||
// Fetch data from the new JSON file structure | ||
fetch("motors.json") | ||
.then(response => response.json()) | ||
.then(data => { | ||
motorData = data.motors; | ||
}) | ||
.catch(error => console.error("Error loading data:", error)); | ||
|
||
values.forEach(value => { | ||
const option = document.createElement('option'); | ||
option.value = value; | ||
option.textContent = value; | ||
select.appendChild(option); | ||
}); | ||
// Filter motors based on user input | ||
function filterMotors() { | ||
const minDiameter = parseFloat(document.getElementById("diameter-min").value); | ||
const maxDiameter = parseFloat(document.getElementById("diameter-max").value); | ||
|
||
select.addEventListener('change', () => { | ||
applyFilters(motors); | ||
const filteredMotors = motorData.filter(motor => { | ||
return (!minDiameter || motor.diameter_mm >= minDiameter) && | ||
(!maxDiameter || motor.diameter_mm <= maxDiameter); | ||
}); | ||
|
||
label.appendChild(select); | ||
filtersDiv.appendChild(label); | ||
displayResults(filteredMotors); | ||
} | ||
|
||
function applyFilters(motors) { | ||
const filterParams = [ | ||
'cooling', | ||
'rated_voltage_v', | ||
'continuous_torque_nm', | ||
'peak_torque_nm', | ||
'max_rotation_speed_rpm', | ||
'pole_pairs', | ||
'rotor_inertia_kg_cm2', | ||
'diameter_mm', | ||
'length_mm', | ||
'weight_g', | ||
'rotor_inner_diameter_mm' | ||
]; | ||
|
||
let filteredMotors = motors; | ||
// Display results in the table | ||
function displayResults(motors) { | ||
const resultsBody = document.getElementById("results-body"); | ||
resultsBody.innerHTML = ""; | ||
|
||
filterParams.forEach(param => { | ||
const filterValue = document.getElementById(`${param}-filter`).value; | ||
|
||
if (filterValue) { | ||
filteredMotors = filteredMotors.filter(motor => { | ||
// Handle numerical and string comparison | ||
if (typeof motor[param] === 'number') { | ||
return motor[param] == filterValue; | ||
} else { | ||
return motor[param] === filterValue; | ||
} | ||
}); | ||
} | ||
motors.forEach(motor => { | ||
const row = document.createElement("tr"); | ||
row.innerHTML = ` | ||
<td>${motor.name || "-"}</td> | ||
<td>${motor.cooling || "-"}</td> | ||
<td>${motor.rated_voltage_v !== null ? motor.rated_voltage_v : "-"}</td> | ||
<td>${motor.continuous_torque_nm !== null ? motor.continuous_torque_nm : "-"}</td> | ||
<td>${motor.peak_torque_nm !== null ? motor.peak_torque_nm : "-"}</td> | ||
<td>${motor.max_power_w !== null ? motor.max_power_w : "-"}</td> | ||
<td>${motor.max_rotation_speed_rpm !== null ? motor.max_rotation_speed_rpm : "-"}</td> | ||
<td>${motor.pole_pairs !== null ? motor.pole_pairs : "-"}</td> | ||
<td>${motor.rotor_inertia_kg_cm2 !== null ? motor.rotor_inertia_kg_cm2 : "-"}</td> | ||
<td>${motor.diameter_mm !== null ? motor.diameter_mm : "-"}</td> | ||
<td>${motor.length_mm !== null ? motor.length_mm : "-"}</td> | ||
<td>${motor.weight_g !== null ? motor.weight_g : "-"}</td> | ||
<td>${motor.rotor_inner_diameter_mm !== null ? motor.rotor_inner_diameter_mm : "-"}</td> | ||
`; | ||
resultsBody.appendChild(row); | ||
}); | ||
|
||
populateTable(filteredMotors); | ||
} |
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 |
---|---|---|
@@ -1,31 +1,38 @@ | ||
body { | ||
font-family: Arial, sans-serif; | ||
margin: 20px; | ||
} | ||
|
||
h1 { | ||
text-align: center; | ||
} | ||
|
||
#filters { | ||
text-align: center; | ||
margin-bottom: 20px; | ||
} | ||
|
||
#filters label { | ||
margin-right: 10px; | ||
} | ||
|
||
#motor-table { | ||
table { | ||
width: 100%; | ||
border-collapse: collapse; | ||
margin: auto; | ||
} | ||
|
||
#motor-table th, #motor-table td { | ||
border: 1px solid #ddd; | ||
th, td { | ||
padding: 8px; | ||
text-align: center; | ||
border: 1px solid #ddd; | ||
} | ||
|
||
th { | ||
background-color: #f4f4f4; | ||
} | ||
|
||
button { | ||
padding: 5px 10px; | ||
font-size: 16px; | ||
cursor: pointer; | ||
} | ||
|
||
#motor-table th { | ||
background-color: #f2f2f2; | ||
button:hover { | ||
background-color: #ddd; | ||
} |