使用 nodeJS 创建依赖下拉菜单过滤器
Creating dependent dropdown menu filter using nodeJS
我需要在网页上创建两个下拉菜单 -(第二个下拉菜单依赖于第一个下拉菜单) - 并使用 .csv 文件的两个不同列中的记录填充两个菜单。
我能够使用 NodeJS 读取 csv 文件并将所需的列放入两个不同的数组中,如下所示:
uniqueCountryName
[
'Italy',
'Spain',
'France',
'England'
]
uniqueCityName
[
'Paris',
'Milan',
'Marseilles',
'Venice'
'London',
'Rome',
'Berlin',
'Madrid',
'Barcelona'
]
这是我目前使用的 nodeJS 代码:
const csv = require('csv-parser');
const fs = require('fs');
const results = [];
const CountryName = [];
const CityName = [];
fs.createReadStream('C:/Users/learningsql/Desktop/project.csv')
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
var CountryName = []
for (var item in results) {
CountryName.push(results[item]["CountryName"])
}
/* console.log(CountryName) */
const uniqueCountryName = Array.from(new Set(CountryName));
console.log("uniqueCountryName")
console.log(uniqueCountryName)
});
fs.createReadStream('C:/Users/learningsql/Desktop/project.csv')
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
for (var item in results) {
CityName.push(results[item]["CityName"])
}
/* console.log(CityName) */
const uniqueCityName = Array.from(new Set(CityName));
console.log("uniqueCityName")
console.log(uniqueCityName)
});
现在我需要做两件事:首先我需要在网页上的两个不同下拉菜单中填充这两个不同的数组。 CountryName 应该进入第一个下拉菜单,CityName 应该进入第二个下拉菜单。我的理解是浏览器无法读取终端的输出,所以我们需要使用 Express.JS 设置服务器。我该怎么做?
其次,正如我已经提到的,第二个下拉记录应该依赖于第一个,我们如何 map/link 这两个数组?例如,当我在第一个下拉菜单中 select 意大利时,只有意大利城市(米兰、威尼斯、罗马等)应该出现在第二个下拉菜单中。如果我 select 西班牙,下拉菜单中应该只显示西班牙城市(马德里、巴塞罗那等)。
作为参考,下拉菜单应该是 like this
我是 NodeJS 的新手,非常感谢任何帮助。谢谢
你在这里问了两个问题。一是如何使用express。另一个是如何实现依赖下拉。也许这些应该是两个不同的问题,但让我在这里一一解决。首先,我们如何使用express(这是一个用node编写的web服务器)
1.如何使用快递
- 在您选择的目录中安装 express(有关详细信息,请参阅 link)
$ npm install express --save
- 创建一个新的 javascript 文件
- 使用下面的代码
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send("<h1>hello world</h1>");
});
app.listen(8080, function () {
console.log('Example app listening on port 8080!');
//call this app from localhost://8080
});
这是一个简单的"hello world"。我已经在 blog 中详细解释了这一点。如果您想使用 html 文件而不是简单的 html 代码,请使用以下内容:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('index.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
请注意,您需要使用 npm 或其他包来安装 express、http 和 fs。
您的 html 代码将在 index.html
.
中
你的第二个问题是关于如何使用依赖下拉菜单。
2。从属下拉菜单
对于依赖下拉菜单,您纯粹是在客户端工作。所以香草 javascript 代码可以工作。例如,您可以使用 jquery 或其他有用的库。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- optionally if you need translation for your language then include locale file as mentioned below -->
<script src="path/to/js/locales/<lang>.js"></script>
<h1>Dependent Dropdown</h1>
<form name="myform" id="myForm">
<select name="optone" id="continentSel" size="1">
<option value="" selected="selected">Select continent</option>
</select>
<br>
<br>
<select name="opttwo" id="countrySel" size="1">
<option value="" selected="selected">Please select continent first</option>
</select>
<br>
<br>
<select name="optthree" id="citySel" size="1">
<option value="" selected="selected">Please select state first </option>
</select>
</form>
<hr/>
<script>
var stateObject = {
"Europe": {
"France": ["Paris", "Lyon"],
"UK": ["London", "Birmingham"]
},
"Africa": {
"Senegal": ["Dakar", "Louga"],
"South Africa": ["Cape Town", "Pretoria"]
}
}
window.onload = function () {
var continentSel = document.getElementById("continentSel"),
countrySel = document.getElementById("countrySel"),
citySel = document.getElementById("citySel");
for (var item in stateObject) {
continentSel.options[continentSel.options.length] = new Option(item, item);
}
continentSel.onchange = function () {
countrySel.length = 1; // remove all options bar first
citySel.length = 1; // remove all options bar first
if (this.selectedIndex < 1) return; // done
for (var item in stateObject[this.value]) {
countrySel.options[countrySel.options.length] = new Option(item, item);
}
}
continentSel.onchange(); // reset in case page is reloaded
countrySel.onchange = function () {
citySel.length = 1; // remove all options bar first
if (this.selectedIndex < 1) return; // done
var cities = stateObject[continentSel.value][this.value];
for (var i = 0; i < cities.length; i++) {
citySel.options[citySel.options.length] = new Option(cities[i], cities[i]);
}
}
}
</script>
终于看到完整的工作代码here
我需要在网页上创建两个下拉菜单 -(第二个下拉菜单依赖于第一个下拉菜单) - 并使用 .csv 文件的两个不同列中的记录填充两个菜单。
我能够使用 NodeJS 读取 csv 文件并将所需的列放入两个不同的数组中,如下所示:
uniqueCountryName
[
'Italy',
'Spain',
'France',
'England'
]
uniqueCityName
[
'Paris',
'Milan',
'Marseilles',
'Venice'
'London',
'Rome',
'Berlin',
'Madrid',
'Barcelona'
]
这是我目前使用的 nodeJS 代码:
const csv = require('csv-parser');
const fs = require('fs');
const results = [];
const CountryName = [];
const CityName = [];
fs.createReadStream('C:/Users/learningsql/Desktop/project.csv')
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
var CountryName = []
for (var item in results) {
CountryName.push(results[item]["CountryName"])
}
/* console.log(CountryName) */
const uniqueCountryName = Array.from(new Set(CountryName));
console.log("uniqueCountryName")
console.log(uniqueCountryName)
});
fs.createReadStream('C:/Users/learningsql/Desktop/project.csv')
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
for (var item in results) {
CityName.push(results[item]["CityName"])
}
/* console.log(CityName) */
const uniqueCityName = Array.from(new Set(CityName));
console.log("uniqueCityName")
console.log(uniqueCityName)
});
现在我需要做两件事:首先我需要在网页上的两个不同下拉菜单中填充这两个不同的数组。 CountryName 应该进入第一个下拉菜单,CityName 应该进入第二个下拉菜单。我的理解是浏览器无法读取终端的输出,所以我们需要使用 Express.JS 设置服务器。我该怎么做?
其次,正如我已经提到的,第二个下拉记录应该依赖于第一个,我们如何 map/link 这两个数组?例如,当我在第一个下拉菜单中 select 意大利时,只有意大利城市(米兰、威尼斯、罗马等)应该出现在第二个下拉菜单中。如果我 select 西班牙,下拉菜单中应该只显示西班牙城市(马德里、巴塞罗那等)。
作为参考,下拉菜单应该是 like this
我是 NodeJS 的新手,非常感谢任何帮助。谢谢
你在这里问了两个问题。一是如何使用express。另一个是如何实现依赖下拉。也许这些应该是两个不同的问题,但让我在这里一一解决。首先,我们如何使用express(这是一个用node编写的web服务器)
1.如何使用快递
- 在您选择的目录中安装 express(有关详细信息,请参阅 link)
$ npm install express --save
- 创建一个新的 javascript 文件
- 使用下面的代码
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send("<h1>hello world</h1>");
});
app.listen(8080, function () {
console.log('Example app listening on port 8080!');
//call this app from localhost://8080
});
这是一个简单的"hello world"。我已经在 blog 中详细解释了这一点。如果您想使用 html 文件而不是简单的 html 代码,请使用以下内容:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('index.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
请注意,您需要使用 npm 或其他包来安装 express、http 和 fs。
您的 html 代码将在 index.html
.
你的第二个问题是关于如何使用依赖下拉菜单。
2。从属下拉菜单
对于依赖下拉菜单,您纯粹是在客户端工作。所以香草 javascript 代码可以工作。例如,您可以使用 jquery 或其他有用的库。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- optionally if you need translation for your language then include locale file as mentioned below -->
<script src="path/to/js/locales/<lang>.js"></script>
<h1>Dependent Dropdown</h1>
<form name="myform" id="myForm">
<select name="optone" id="continentSel" size="1">
<option value="" selected="selected">Select continent</option>
</select>
<br>
<br>
<select name="opttwo" id="countrySel" size="1">
<option value="" selected="selected">Please select continent first</option>
</select>
<br>
<br>
<select name="optthree" id="citySel" size="1">
<option value="" selected="selected">Please select state first </option>
</select>
</form>
<hr/>
<script>
var stateObject = {
"Europe": {
"France": ["Paris", "Lyon"],
"UK": ["London", "Birmingham"]
},
"Africa": {
"Senegal": ["Dakar", "Louga"],
"South Africa": ["Cape Town", "Pretoria"]
}
}
window.onload = function () {
var continentSel = document.getElementById("continentSel"),
countrySel = document.getElementById("countrySel"),
citySel = document.getElementById("citySel");
for (var item in stateObject) {
continentSel.options[continentSel.options.length] = new Option(item, item);
}
continentSel.onchange = function () {
countrySel.length = 1; // remove all options bar first
citySel.length = 1; // remove all options bar first
if (this.selectedIndex < 1) return; // done
for (var item in stateObject[this.value]) {
countrySel.options[countrySel.options.length] = new Option(item, item);
}
}
continentSel.onchange(); // reset in case page is reloaded
countrySel.onchange = function () {
citySel.length = 1; // remove all options bar first
if (this.selectedIndex < 1) return; // done
var cities = stateObject[continentSel.value][this.value];
for (var i = 0; i < cities.length; i++) {
citySel.options[citySel.options.length] = new Option(cities[i], cities[i]);
}
}
}
</script>
终于看到完整的工作代码here