量化交易之nicegui篇 - 可编辑的table表格控件 & 增加删除行

from nicegui import ui

columns = [
    {'field': 'id'},
    {'field': 'age', 'editable': True},
    {'field': 'name', 'editable': True, 'sortable': True},
]
rows = [
    {'id': 0, 'name': 'Alice', 'age': 18},
    {'id': 1, 'name': 'Bob', 'age': 21},
    {'id': 2, 'name': 'Carol', 'age': 20},
]


def add_row():
    new_id = max((dx['id'] for dx in rows), default=-1) + 1
    rows.append({'id': new_id, 'name': 'New name', 'age': None})
    ui.notify(f'Added row with ID {new_id}')
    aggrid.update()

def handle_cell_value_change(e):
    new_row = e.args['data']
    ui.notify(f'Updated row to: {e.args["data"]}')
    rows[:] = [row | new_row if row['id'] == new_row['id'] else row for row in rows]

async def delete_selected():
    selected_id = [row['id'] for row in await aggrid.get_selected_rows()]
    rows[:] = [row for row in rows if row['id'] not in selected_id]
    ui.notify(f'Deleted row with ID {selected_id}')
    aggrid.update()

aggrid = ui.aggrid({
    'columnDefs': columns, 'rowData': rows, 'rowSelection': 'multiple', 'stopEditingWhenCellsLoseFocus': True,
}).on('cellValueChanged', handle_cell_value_change)

ui.button('Delete selected', on_click=delete_selected)
ui.button('New row', on_click=add_row)

ui.run()

代码运行结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值