【发布时间】:2021-08-24 10:00:30
【问题描述】:
const [customerList, setCustomerList] = useState([]); //store all that information of the database in a list
//make an axios request to get information from database
useEffect(() => {
Axios.get("http://localhost:3001/customers").then((response) => {
setCustomerList(response.data);
});
}, []);
const [counterCount, setCounterCount] = useState(0);
function sortCountCustomer() {
const sortedCountCustomer = [...customerList];
let sortCountVisit = counterCount;
//check the current sortCount, if it is 2 then go back to 1, if not then increase by 1
if (sortCountVisit === 2) {
sortCountVisit = 1;
setCounterCount(1);
} else {
sortCountVisit += 1;
setCounterCount(sortCountVisit);
}
console.log(sortCountVisit);
if (sortCountVisit < 3) {
sortedCountCustomer.sort(function (x, y) {
if (sortCountVisit === 1) {
return x.counts_of_visit > y.counts_of_visit
? 0
: x.counts_of_visit
? -1
: 1;
} else if (sortCountVisit === 2) {
return x.counts_of_visit < y.counts_of_visit
? 0
: x.counts_of_visit
? 1
: -1;
}
});
setCustomerList(sortedCountCustomer);
}
}
function getCountArrow() {
if (counterCount === 1) return "↑";
return "↓";
}
<th class="countheader" onClick={() => sortCountCustomer()}>
Counts of Visit {getCountArrow()}
我在整数列上有以下功能,但是当我单击标题计数访问时它不起作用。因此,当我单击具有不同整数值的访问计数标题列时,我的代码不起作用,想要将其排序为升序和降序。
【问题讨论】:
-
排序代码还能运行吗?您在浏览器开发人员工具控制台中获得任何输出吗?我看到了
sortCountCustomer函数,但不是你怎么称呼它 -
@JaromandaX 嗨 Jaro 我在下面编辑了我的代码问题以显示
sortCountCustomer}> 访问计数 {getCountArrow()} () => sortCountCustomer不调用函数......它返回函数,这是你的意思吗?还是你的意思是() => sortCountCustomer()@JaromandaX 好的,我在其中进行了编辑,但是我对整数值的排序不起作用所以我的列有几个整数。 5 7 9 2 13,我要按升序和降序排序而不是三元运算符,只是return x.counts_of_visit - y.counts_of_visit和return y.counts_of_visit - x.counts_of_visit酌情 - 因为你的三元没有意义
标签: javascript node.js reactjs frontend backend