上一页的最后一个元素作为 Sequelize 分页中下一页的第一个元素
Last element of previous page come as first element of next page in Sequelize pagination
我正在使用 MySQL 作为 Sequelize 分页的数据库。我将上一页的最后一个元素作为下一页的第一个元素。
exports.searchProductCategories = (body, username, shopId) => {
return new Promise((resolve, reject) => {
let searchText = body.searchText.toLowerCase();
let limit = body.limit;
let offset = body.offset;
Category.findAndCountAll({
where: {
[Sequelize.Op.or]:[
{name: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col(KEY_NAME_COLUMN)), Sequelize.Op.like, '%' + searchText + '%')},
{description: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col(KEY_DESCRIPTION_COLUMN)), Sequelize.Op.like, '%' + searchText + '%')}
],
[Sequelize.Op.and]:[
{shopId: Sequelize.where(Sequelize.col(KEY_SHOP_ID_COLUMN), Sequelize.Op.eq, shopId)}
]
},
order: [
[KEY_NAME_COLUMN, 'ASC']
],
offset: offset,
limit: limit
}).then(result => {
resolve ({ [KEY_STATUS]: 1, [KEY_MESSAGE]: "Categories listed successfully", [KEY_DATA]: result.rows, [KEY_TOTAL_COUNT]: result.count });
}).catch(error => {
reject ({ [KEY_STATUS]: 0, [KEY_MESSAGE]: "Categories list failed" + error });
});
})
}
以上代码,我给
{
"searchText": "cat",
"limit": 2,
"offset": 0
}
然后我得到了 2 件商品。接下来,我给
{
"searchText": "cat",
"limit": 2,
"offset": 1
}
然后我将上一页的最后一个元素作为当前页面的第一个元素。
您应该使用 page 和 limit 来计算偏移量
例如:
page = 1 // for first page
offset = (page - 1) * limit // for the first page it skips (1 - 1) * 2 = 0 records and show 2(limits) records
第二页
limit = 2
page = 2
offset = (2-1)*2 = 2 // so it skips 2 records and show next 2 records
我正在使用 MySQL 作为 Sequelize 分页的数据库。我将上一页的最后一个元素作为下一页的第一个元素。
exports.searchProductCategories = (body, username, shopId) => {
return new Promise((resolve, reject) => {
let searchText = body.searchText.toLowerCase();
let limit = body.limit;
let offset = body.offset;
Category.findAndCountAll({
where: {
[Sequelize.Op.or]:[
{name: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col(KEY_NAME_COLUMN)), Sequelize.Op.like, '%' + searchText + '%')},
{description: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col(KEY_DESCRIPTION_COLUMN)), Sequelize.Op.like, '%' + searchText + '%')}
],
[Sequelize.Op.and]:[
{shopId: Sequelize.where(Sequelize.col(KEY_SHOP_ID_COLUMN), Sequelize.Op.eq, shopId)}
]
},
order: [
[KEY_NAME_COLUMN, 'ASC']
],
offset: offset,
limit: limit
}).then(result => {
resolve ({ [KEY_STATUS]: 1, [KEY_MESSAGE]: "Categories listed successfully", [KEY_DATA]: result.rows, [KEY_TOTAL_COUNT]: result.count });
}).catch(error => {
reject ({ [KEY_STATUS]: 0, [KEY_MESSAGE]: "Categories list failed" + error });
});
})
}
以上代码,我给
{
"searchText": "cat",
"limit": 2,
"offset": 0
}
然后我得到了 2 件商品。接下来,我给
{
"searchText": "cat",
"limit": 2,
"offset": 1
}
然后我将上一页的最后一个元素作为当前页面的第一个元素。
您应该使用 page 和 limit 来计算偏移量 例如:
page = 1 // for first page
offset = (page - 1) * limit // for the first page it skips (1 - 1) * 2 = 0 records and show 2(limits) records
第二页
limit = 2
page = 2
offset = (2-1)*2 = 2 // so it skips 2 records and show next 2 records