如何根据 select 元素的值对对象进行排序?- mongodb, nodejs
How sort objects according to value from select element?- mongodb, nodejs
我必须根据 select 选项的值对对象进行排序。
我应该怎么做?
//这是我在另一个文件中的select元素
<select class="form-select" aria-label="Default select example">
<option id="sorting" selected>Sort by</option>
<option value="1">Low to High price</option>
<option value="2">High to low price</option>
<option value="3">From A to Z</option>
<option value="4">From Z to A</option>
</select>
//用我的排序函数编写代码
module.exports.listOfHouses = async (req, res) => {
function selectedValue(){
if(document.getElementById("sorting").value === "1"){
return "title: 1"
} else if(document.getElementById('sorting').value == "2"){
return "title: 1"
}else if(document.getElementById('sorting').value == "3"){
return "title: 1"
}else if(document.getElementById('sorting').value == "4"){
return "title: 1"
} else {
return "title: 1"
}
}
const houses = await House.find({}).sort({selectedValue});
res.render('houses/houses', {houses} );
};
可以使用Array的sort()
方法,它有一个回调函数,回调函数的参数是array
中包含的2个对象(我们称之为a和b)
listOfHouses.sort((a, b) => (a.value > b.value) ? 1 : -1)
排序后开始循环
您需要在您的快速请求中发送排序对象,但您的代码中存在一些错误:
- 要从 DOM select 元素中获取值,您需要将 id 添加到 select 元素而不是选项。
例如:
<select id="sorting" class="form-select" aria-label="Default select example">
<option selected>Sort by</option>
<option value="1">Low to High price</option>
<option value="2">High to low price</option>
<option value="3">From A to Z</option>
<option value="4">From Z to A</option>
</select>
要获得 selected 值,您可以在这种模式下通过您的函数来完成:
function getSortingObject() {
const value = document.getElementById("sorting").value
if (document.getElementById("sorting").value === "1") {
return {price:1}
} else if (document.getElementById('sorting').value == "2") {
return {price:-1}
} else if (document.getElementById('sorting').value == "3") {
return "title: 1"
} else if (document.getElementById('sorting').value == "4") {
return {title:1}
} else {
return {title:-1}
}
}
现在您需要将当前排序对象从您的 front-end 发送到您的后端
所以你需要在这个模式下获取排序对象:
const sortingObject = getSortingObject()
fetch(YOUR URL,YOUR_BODY WITH Sorting object )
在后端,您需要验证是否允许使用排序对象 ar 编码允许专业人士在此模式下排序:
module.exports.listOfHouses = async (req, res) => {
const canSortBy = ["title","price"]
const sortingoBJECT = req.body.sortingObject
const canSort = Object.keys(sortingoBJECT).findIndex(r=>!canSortBy[r])
if(canSort) res.sendStatus(....)
const houses = await House.find({}).sort(sortingoBJECT);
res.render('houses/houses', {
houses
});
};
我必须根据 select 选项的值对对象进行排序。 我应该怎么做?
//这是我在另一个文件中的select元素
<select class="form-select" aria-label="Default select example">
<option id="sorting" selected>Sort by</option>
<option value="1">Low to High price</option>
<option value="2">High to low price</option>
<option value="3">From A to Z</option>
<option value="4">From Z to A</option>
</select>
//用我的排序函数编写代码
module.exports.listOfHouses = async (req, res) => {
function selectedValue(){
if(document.getElementById("sorting").value === "1"){
return "title: 1"
} else if(document.getElementById('sorting').value == "2"){
return "title: 1"
}else if(document.getElementById('sorting').value == "3"){
return "title: 1"
}else if(document.getElementById('sorting').value == "4"){
return "title: 1"
} else {
return "title: 1"
}
}
const houses = await House.find({}).sort({selectedValue});
res.render('houses/houses', {houses} );
};
可以使用Array的sort()
方法,它有一个回调函数,回调函数的参数是array
中包含的2个对象(我们称之为a和b)
listOfHouses.sort((a, b) => (a.value > b.value) ? 1 : -1)
排序后开始循环
您需要在您的快速请求中发送排序对象,但您的代码中存在一些错误:
- 要从 DOM select 元素中获取值,您需要将 id 添加到 select 元素而不是选项。 例如:
<select id="sorting" class="form-select" aria-label="Default select example">
<option selected>Sort by</option>
<option value="1">Low to High price</option>
<option value="2">High to low price</option>
<option value="3">From A to Z</option>
<option value="4">From Z to A</option>
</select>
要获得 selected 值,您可以在这种模式下通过您的函数来完成:
function getSortingObject() {
const value = document.getElementById("sorting").value
if (document.getElementById("sorting").value === "1") {
return {price:1}
} else if (document.getElementById('sorting').value == "2") {
return {price:-1}
} else if (document.getElementById('sorting').value == "3") {
return "title: 1"
} else if (document.getElementById('sorting').value == "4") {
return {title:1}
} else {
return {title:-1}
}
}
现在您需要将当前排序对象从您的 front-end 发送到您的后端 所以你需要在这个模式下获取排序对象:
const sortingObject = getSortingObject()
fetch(YOUR URL,YOUR_BODY WITH Sorting object )
在后端,您需要验证是否允许使用排序对象 ar 编码允许专业人士在此模式下排序:
module.exports.listOfHouses = async (req, res) => {
const canSortBy = ["title","price"]
const sortingoBJECT = req.body.sortingObject
const canSort = Object.keys(sortingoBJECT).findIndex(r=>!canSortBy[r])
if(canSort) res.sendStatus(....)
const houses = await House.find({}).sort(sortingoBJECT);
res.render('houses/houses', {
houses
});
};