-
I'm trying to figure out how to add a specific action done when a row of the ui.stat_list_item is clicked. ui.stat_list_item(
label=nb_unshipped_list['SALES_ID'][i],
name=f'order_clicked_{str(i)}',
caption=get_order_list_items(nb_unshipped_list['SALES_ITEMS'][i]),
value=nb_unshipped_list['SALES_DATE'][i],
aux_value=nb_unshipped_list['EC'][i],
value_color=get_order_waiting_color(nb_unshipped_list['SALES_DATE'][i])) for i in range(len(nb_unshipped_list))
], I want the user to be able to have information displayed when a specific row is clicked but the q.args only contain the name of the list row. My main issue being how to check for which row is clicked since the q.args.order_clicked_0, q.args.order_clicked_1, etc... name for the number of row is dynamic. And doing something like this doesn't work: list_clicked_order = "list_clicked_" + str(1)
if q.args.list_clicked_order: .... The name of the row being fixed doesn't make sense as all rows would have the same action behavior. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 8 replies
-
Hi @CH3V4L13R, good question. The quick and ugly workaround could be something like this: from h2o_wave import main, app, Q, ui
# Your custom data structure.
class DataObj:
def __init__(self, prop1, prop2, prop3):
self.prop1 = prop1
self.prop2 = prop2
self.prop3 = prop3
# Fill the dictionary - key has to be same as name of the stat_list_item.
obj_dict = {}
for i in range(10):
obj_dict[i] = DataObj(i, i, i)
@app('/demo')
async def serve(q: Q):
q.page['list'] = ui.stat_list_card(box='1 1 5 5', title='Title', items=[
ui.stat_list_item(name=f'{idx}', label=str(obj.prop1)) for idx, obj in obj_dict.items()
])
# Ugly workaround for getting q.args.check
for key in obj_dict.keys():
if str(key) in q.args:
print(f'Row {key} was clicked!')
print(f'Here is the associated data: {obj_dict[key]}')
# Dummy update - ignore.
q.page['dummy'].items = []
await q.page.save() @lo5 maybe we could add same pattern matching for |
Beta Was this translation helpful? Give feedback.
-
I wanted to mention that because list_clicked_order = "list_clicked_" + str(1)
if q.args.list_clicked_order: can be handled with: list_clicked_order = "list_clicked_" + str(1)
if q.args[list_clicked_order] is not None: In this case @mturoci 's solution is the way to go, checking if each if q.args[ str(key)] is not None:
print(f'Row {key} was clicked!')
print(f'Here is the associated data: {obj_dict[key]}') |
Beta Was this translation helpful? Give feedback.
-
The correct solution (1-liner) is to provide a |
Beta Was this translation helpful? Give feedback.
-
@mturoci @lo5 @mtanco Thank you for all your feedback. The code provided by @mturoci works perfectly. The issue is that the list contain a certain number of orders and the list is dynamically generated from that number:
From that clicked row, I just need to retrieve one specific value (the order id) and can get all the other info from it later. @mtanco I wish I won't have to check all the key in q.args via the for loop, but I don't understand well your explanation. Since I do not know how many keys will be generated or which key (list row) will be clicked, I guess doing the for loop to check is the only way to go. Please let me know if preventing the for loop seems possible. To summarize, the only things that I needed was the class object with one variable to check which row is being clicked. class DataObj: and obj_dict = {} Thanks! |
Beta Was this translation helpful? Give feedback.
-
@CH3V4L13R - the solution from @mturoci is not the right approach. @mturoci - no workarounds/hacks are necessary. You do not have to loop through |
Beta Was this translation helpful? Give feedback.
@CH3V4L13R - the solution from @mturoci is not the right approach.
@mturoci - no workarounds/hacks are necessary.
You do not have to loop through
q.args
.Just provide a
name
, e.g.ui.stat_list_card(, ... name='foo', ...)
and you can simply check the value ofq.args.foo
to know which item was selected.