How to get the value of a cell slot within a table? #3102
Unanswered
thanhnguyen2187
asked this question in
Q&A
Replies: 1 comment 2 replies
-
Hi @thanhnguyen2187, Here's a trick I like to use: COLORS = {
'not-started': 'gray',
'error': 'red',
'success': 'green',
}
LABELS = {
'not-started': 'Not started',
'error': 'Error',
'success': 'Success',
}
columns = [{"name": "status", "label": "Status", "field": "status", "align": "left"}]
rows = [{"status": "not-started"}, {"status": "success"}, {"status": "error"}]
with ui.table(rows=rows, columns=columns, row_key="index", pagination=5) as table:
table.add_slot("body-cell-status", '''
<q-td key="status" :props="props">
<q-badge :color="''' + json.dumps(COLORS).replace('"', "'") + '''[props.value]">
{{ ''' + json.dumps(LABELS) + '''[props.value] }}
</q-badge>
</q-td>
''') Embedding a JSON-serialized dictionary in the template string keeps the JavaScript code simple while moving the lookup dictionaries of colors and labels into the Python world. We just need to replace double quotes |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description
Hi. Thanks for the awesome library!
I'm trying to customize specific cells within a table. To get into details, it is "converting" the strings
not-started
, orerror
, orsuccess
into equivalent badges. My code example is:I found that we can customize
body-cell-...
with JS, but I'm wondering if we can do better. Following "the JS path" are these "roads":ui.add_head_html
, and add custom<script>...</script>
. I probably would settle with it for now, but for long term, I would prefer having everything within Python.The added logic in the second road can be something like:
EDIT: because of how Vue works, I cannot just define a global
var
, orwindow.myFunction
and use it within the<q-td>
😅.EDIT: I found that we can do something like:
Which is... good enough for now, but please let me know what do you think anyway!
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions