sqlite 如何解决这个问题与内联按钮分页的aigram 3.0?

dw1jzc5e  于 2023-11-21  发布在  SQLite
关注(0)|答案(1)|浏览(193)

我有一个内联按钮分页和一个数据库. text[0],callbackdata 1我需要它们依次输出五个部分(因此直到数据库用完,它至少可以有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))

字符串
enter image description here

2admgd59

2admgd591#

据我所知,我们的两个解决方案都基于相同的代码,所以这就是我是如何做到这一点的,它可能会有所帮助:

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())

相关问题