admin管理员组文章数量:1278720
NiceGUI documentation on conditional formatting in Table suggests the following:
table.add_slot('body-cell-age', '''
<q-td key="age" :props="props">
<q-badge :color="props.value < 21 ? 'red' : 'green'">
{{ props.value }}
</q-badge>
</q-td>
''')
This has a very simple logic, namely props.value < 21 ? 'red' : 'green'
I do want to put way more advanced formatting logic here, and reuse it across the entire app - in many slots of many different components. I do not want to just inline the same code snippet for each slot as this will result in a lot of extra code duplication on the client side. Instead, I want to define a function once, and call it from each slot.
How can I do it?
I tried attaching a function to window
by adding <script>
with ui.add_head_html
but it was not recognized in my slot.
I tried using custom VUE component for my slot definition but then it doesn't appear to populate props
.
NiceGUI documentation on conditional formatting in Table suggests the following:
table.add_slot('body-cell-age', '''
<q-td key="age" :props="props">
<q-badge :color="props.value < 21 ? 'red' : 'green'">
{{ props.value }}
</q-badge>
</q-td>
''')
This has a very simple logic, namely props.value < 21 ? 'red' : 'green'
I do want to put way more advanced formatting logic here, and reuse it across the entire app - in many slots of many different components. I do not want to just inline the same code snippet for each slot as this will result in a lot of extra code duplication on the client side. Instead, I want to define a function once, and call it from each slot.
How can I do it?
I tried attaching a function to window
by adding <script>
with ui.add_head_html
but it was not recognized in my slot.
I tried using custom VUE component for my slot definition but then it doesn't appear to populate props
.
1 Answer
Reset to default 1Defining a global JavaScript function to be used in props and templates is currently an open question: https://github/zauberzeug/nicegui/discussions/3754
One possible workaround is to augment the table rows with an additional pre-computed color attribute:
columns = [
{'name': 'name', 'label': 'Name', 'field': 'name'},
{'name': 'age', 'label': 'Age', 'field': 'age'},
]
rows = [
{'name': 'Alice', 'age': 18},
{'name': 'Bob', 'age': 21},
]
for row in rows:
row['color'] = 'red' if row['age'] < 21 else 'green'
table = ui.table(columns=columns, rows=rows, row_key='name')
table.add_slot('body-cell-age', '''
<q-td key="age" :props="props">
<q-badge :color="props.row.color">
{{ props.value }}
</q-badge>
</q-td>
''')
版权声明:本文标题:javascript - How can I reuse a function in NiceGUI slots without client-side code duplication? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741298506a2370951.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
logic = "props.value < 21 ? 'red' : 'green'"
and later usef-strings
to generate slotsf'''<q-td .... {logic} ... /g-td>'''
– furas Commented Feb 24 at 1:40NiceUI
but if you need dynamic logic then first it may need to usedevtools
in chrome/firefox to see what HTML it generates - and maybe it explains how to use<script>
– furas Commented Feb 24 at 2:17