Javascript 在蚂蚁设计中使用可移植的过滤器
我在Ant Design Pro中使用。API从服务器接收表的数据源。如果用户不是管理员,我会根据他的Javascript 在蚂蚁设计中使用可移植的过滤器,javascript,reactjs,antd,ant-design-pro,Javascript,Reactjs,Antd,Ant Design Pro,我在Ant Design Pro中使用。API从服务器接收表的数据源。如果用户不是管理员,我会根据他的userid过滤数据源。问题是,我无法获得这两种情况(管理员和用户)的实际总页数 下面是我的API请求函数的代码: function getRule(req, res, u) { let realUrl = u; if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') { re
userid
过滤数据源。问题是,我无法获得这两种情况(管理员和用户)的实际总页数
下面是我的API请求函数的代码:
function getRule(req, res, u) {
let realUrl = u;
if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
realUrl = req.url;
}
const { current = 1, pageSize = 5 } = req.query;
const params = parse(realUrl, true).query;
let dataSource = [...tableListDataSource].slice((current - 1) * pageSize, current * pageSize);
const filter = JSON.parse(params.filter);
if (filter) {
if (Object.keys(filter).length > 0) {
dataSource = tableListDataSource.filter((item) => {
return Object.keys(filter).some((key) => {
if (!filter[key]) {
return true;
}
if (filter[key].includes(`${item[key]}`)) {
return true;
}
return false;
});
});
}
}
// TODO: Fix total count of pages for admin
const total = () => {
if (Object.keys(filter).length > 0) {
return dataSource.length
} else {
return tableListDataSource.length
}
}
const result = {
data: dataSource,
total: total,
success: true,
pageSize,
current: parseInt(`${params.currentPage}`, 10) || 1,
};
return res.json(result);
}
const total = Object.keys(filter).length > 0 ? dataSource.length : tableListDataSource.length
但是,对于用户和管理员,我得到的都是
dataSource.length
,而不是dataSource.length
。如何修复它?总数应该是一个赋值,而不是一个函数:
function getRule(req, res, u) {
let realUrl = u;
if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
realUrl = req.url;
}
const { current = 1, pageSize = 5 } = req.query;
const params = parse(realUrl, true).query;
let dataSource = [...tableListDataSource].slice((current - 1) * pageSize, current * pageSize);
const filter = JSON.parse(params.filter);
if (filter) {
if (Object.keys(filter).length > 0) {
dataSource = tableListDataSource.filter((item) => {
return Object.keys(filter).some((key) => {
if (!filter[key]) {
return true;
}
if (filter[key].includes(`${item[key]}`)) {
return true;
}
return false;
});
});
}
}
// TODO: Fix total count of pages for admin
const total = () => {
if (Object.keys(filter).length > 0) {
return dataSource.length
} else {
return tableListDataSource.length
}
}
const result = {
data: dataSource,
total: total,
success: true,
pageSize,
current: parseInt(`${params.currentPage}`, 10) || 1,
};
return res.json(result);
}
const total = Object.keys(filter).length > 0 ? dataSource.length : tableListDataSource.length