Skip to content

Commit

Permalink
adding the filtering part
Browse files Browse the repository at this point in the history
  • Loading branch information
Kboere committed Jan 23, 2024
1 parent be2979f commit 7321b23
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 939 deletions.
46 changes: 24 additions & 22 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import express from 'express';
import dotenv from 'dotenv';
//import { fileURLToPath } from 'url';
//import { dirname } from 'path';
//import fs from 'fs';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import fs from 'fs';

dotenv.config();

// const __filename = fileURLToPath(import.meta.url);
// const __dirname = dirname(__filename);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const app = express();
const port = process.env.PORT;

// Read the data file and parse it once when the server starts
//const dataPath = new URL('../Insect-dashboard/static/data/data.json', import.meta.url);
//const rawData = fs.readFileSync(fileURLToPath(dataPath));
//const data = JSON.parse(rawData);
const dataPath = new URL('../Insect-dashboard/static/data/data.json', import.meta.url);
const rawData = fs.readFileSync(fileURLToPath(dataPath));
const data = JSON.parse(rawData);

app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: true }));
Expand All @@ -32,31 +32,33 @@ app.get('/', (req, res) => {

app.get('/vergelijken', (req, res) => {
try {
console.log('every item:', data);
res.render('pages/compare.ejs', { data });
// console.log('every item:', data);
res.render('pages/compare.ejs');
} catch (error) {
console.error('Error reading JSON file:', error);
res.status(500).render('pages/error.ejs', { error });
}
});

//app.get('/filteredData', (req, res) => {
//const selectedLocation = req.query.location;
//const selectedIdentification = req.query.identification;
app.get('/filteredData', (req, res) => {
const selectedProvince = req.query.province;
const selectedLocation = req.query.location;
const selectedIdentification = req.query.identification;

// Implement your logic to filter data based on selectedLocation and selectedIdentification
// Assuming your data is stored in the 'data' variable
// const filteredData = data.filter(item => {
//return (
//(selectedLocation === 'All' || item.location_name === selectedLocation) &&
// (selectedIdentification === 'All' || item.identification_nl === selectedIdentification)
//);
// });
const filteredData = data.filter(item => {
return (
(selectedLocation === 'All' || item.location_name === selectedLocation) &&
(selectedIdentification === 'All' || item.identification_nl === selectedIdentification) &&
(selectedProvince === 'All' || item.province === selectedProvince)
);
});

// console.log('Filtered Data:', filteredData); // Log the filtered data
console.log('Filtered Data:', filteredData); // Log the filtered data

// res.json(filteredData);
//});
res.json(filteredData);
});



Expand Down
Loading

0 comments on commit 7321b23

Please sign in to comment.