How to add javascript function and get it reusable inside props? #3754
Replies: 3 comments 1 reply
-
Hi @msiwik-epruf, that's a very good question. I thought it should be possible to define the function within the globally accessible |
Beta Was this translation helpful? Give feedback.
-
What errors are you getting and what do you expect to see? Slightly different, but I use Your func columns = [
{
'name' : 'File' ,
'label' : 'File' ,
'field' : 'filename' ,
'align' : 'left' ,
} ,
{
'name' : 'size' ,
'label' : 'Size' ,
'field' : 'size' ,
} ,
{
'name' : 'mtime' ,
'label' : 'Mod time' ,
'field' : 'dl' ,
'align' : 'left' ,
':format' : 'value => window.fmt_time_js (value, false)' ,
} ,
]
table = ui.table (columns = columns, rows = filedata) |
Beta Was this translation helpful? Give feedback.
-
@msiwik-epruf & @falkoschindler , while I wasn't trying to use a javascript function from inside a slot, I was trying to bind a custom event to a table using So, my workaround was to use the Vue <q-btn size="sm" color="accent" round dense
@click="() => $parent.$emit('edit_button_clicked', props.row.id)"
:icon="'edit'" /> Not exactly the same problem, but I think similar underlying cause (i.e. no access to the global scope from Vue). Here's the full working source for how I worked around the issue (well parts relevant to the discussion anyway). It is essentially using the "Table with expandable rows" example from the nicegui docs https://nicegui.io/documentation/table#table_with_expandable_rows : def display_table(data: List[Dict],
columns: List[Dict],
visible_columns: List[str],
sort_column: str,
display_name: str,
container) -> None:
def handle_edit_button_clicked(event: events.GenericEventArguments) -> None:
global main_content
global right_drawer
color = 'lightgrey' if event.args else 'white'
main_content.style(f"color: {color}; background-color: {color}")
rowid = event.args
logger.info(f"Edit button clicked for row: {rowid}")
ui.notify(f"Edit button clicked for row: (ID: {rowid})")
container.clear()
with container:
row_key = next(iter(data[0]))
table = ui.table(columns=columns,
rows=data,
row_key=row_key,
pagination={'rowsPerPage': 15, 'sortBy': sort_column, 'page': 1}
).props(f'title="{display_name}" dense visible-columns="{visible_columns}"')
table.classes('w-full overflow-x-auto sticky-header-table').mark('main_data_table')
table.add_slot('header', r'''
<q-tr :props="props">
<q-th auto-width />
<q-th auto-width />
<q-th v-for="col in props.cols" :key="col.name" :props="props">
{{ col.label }}
</q-th>
</q-tr>
''')
table.add_slot('body', r'''
<q-tr :props="props">
<q-td auto-width>
<q-btn size="sm" color="accent" round dense
@click="() => $parent.$emit('edit_button_clicked', props.row.id)"
:icon="'edit'" />
</q-td>
<q-td auto-width>
<q-btn size="sm" color="accent" round dense
@click="props.expand = !props.expand"
:icon="props.expand ? 'remove' : 'add'" />
</q-td>
<q-td v-for="col in props.cols" :key="col.name" :props="props">
{{ col.value }}
</q-td>
</q-tr>
<q-tr v-show="props.expand" :props="props">
<q-td colspan="100%">
<div class="text-left">Editing '{{ props.row.name }}' (ID={{ props.row.id }}).</div>
</q-td>
</q-tr>
''')
table.on('edit_button_clicked', handle_edit_button_clicked)
search = ui.input('Search').bind_value(table, 'filter')
search.move(target_index=0) Perhaps you could bind a handler using the |
Beta Was this translation helpful? Give feedback.
-
Question
I want to add custom function getBadgeColor and use is as prop inside
custom_badge_age_slot
.Tried with add_static_files, adding it as <script> inside prop and nothing works as expected.
This is my minimal example
Beta Was this translation helpful? Give feedback.
All reactions