我有一个内联按钮分页和一个数据库。文本[0],回调数据 我需要它们依次输出五个(所以直到数据库用完,它至少可以有100个元素),但我只是将第一个元素重复五次,然后是第二个,第三个,依此类推
cur.execute("SELECT * FROM data")
data = cur.fetchall()
def paginator(page: int=0):
builder = InlineKeyboardBuilder()
for i in range(5):
button = InlineKeyboardButton(text=data[page][0], callback_data=data[page][1])
builder.add(button)
builder.adjust(1)
builder.row(
InlineKeyboardButton(text="⬅", callback_data=kb.Pagination(action="prev", page=page).pack()),
InlineKeyboardButton(text="➡", callback_data=kb.Pagination(action="next", page=page).pack()),
width=2
)
return builder.as_markup()
@dp.callback_query(kb.Pagination.filter(F.action.in_(["prev", "next"])))
async def pagination_handler(call: CallbackQuery, callback_data: kb.Pagination):
page_num = int(callback_data.page)
page = page_num - 1 if page_num > 0 else 0
if callback_data.action == "next":
page = page_num + 1 if page_num < (len(data) - 1) else page_num
with suppress(TelegramBadRequest):
await call.message.edit_text(
f"list",
reply_markup=paginator(page)
)
await call.answer()
@dp.message()
async def echo(message: Message):
msg = message.text.lower()
if msg == "list":
await message.answer("lis", reply_markup=paginator(0))
据我所知,我们的两个解决方案都基于相同的代码,所以我就是这样做的,它可能会有所帮助:
class Pagination(CallbackData, prefix='pag'):
action: str
page: int
def paginator(page: int = 0):
builder = InlineKeyboardBuilder()
builder.row(
InlineKeyboardButton(text='⬅', callback_data=Pagination(action='prev', page=page).pack()),
InlineKeyboardButton(text='➡', callback_data=Pagination(action='next', page=page).pack()),
width=2
)
return builder
@router.callback_query(Teacher(), CourseInteract.pagination, Pagination.filter(F.action.in_(('prev', 'next'))))
async def pagination_handler(query: CallbackQuery, callback_data: Pagination, session: AsyncSession, state: FSMContext):
course_id = await state.get_data()
stmt = select(Publications).where(Publications.course == course_id['course'])
res = await session.execute(stmt)
posts = res.scalars().all()
page_num = int(callback_data.page)
if callback_data.action == 'next':
if page_num < (len(posts) // 5):
page = page_num + 1
else:
page = page_num
await query.answer('This is the last page')
else:
if page_num > 0:
page = page_num - 1
else:
page = 0
await query.answer('This is the first page')
with suppress(TelegramBadRequest):
pag = paginator(page)
builder = InlineKeyboardBuilder()
start_index = page * 5
end_index = min(start_index + 5, len(posts))
for i in range(start_index, end_index):
builder.row(InlineKeyboardButton(text=posts[i].title,
callback_data=f'publication_{posts[i].id}'))
builder.row(*pag.buttons, width=2)
await query.message.edit_reply_markup(reply_markup=builder.as_markup())
await query.answer()
要显示查询中的前 5 条记录,您应该执行类似的操作:
#here must be an SQL query or something
if posts:
pag = paginator()
builder = InlineKeyboardBuilder()
num = 0
for post in posts:
if num < 5:
num += 1
builder.row(InlineKeyboardButton(text=post.title, callback_data=f'publication_{post.id}'))
builder.row(*pag.buttons, width=2)
await state.set_state(CourseInteract.pagination)
await message.answer('Here is publications:', reply_markup=builder.as_markup())