ui.select doesn't take options as list of dictionaries, like Quasar QSelect does #3977
Replies: 2 comments 2 replies
-
Hi @aabrodskiy, When implementing the For the time being, you add slots yourself to define how to render your options: select = ui.select([
{
'label': 'Google',
'value': 'goog',
'icon': 'mail',
'description': 'google is a search engine',
},
{
'label': 'Facebook',
'value': 'fb',
'icon': 'bluetooth',
'description': 'facebook is old school',
},
])
select.add_slot('option', '''
<q-item :props="props" class="flex items-center gap-2" clickable @click="props.toggleOption(props.opt)">
<q-item-section avatar>
<q-icon :name="props.label.icon"></q-icon>
</q-item-section>
<q-item-section>
<span>{{props.label.label}}</span>
</q-item-section>
</q-item>
''')
select.add_slot('selected-item', '''
<span :props="props">{{props.opt.label.label}}</span>
''') Note that, counter-intuitively, you need to access Oh and there's also a useful option "option-label" to define how to get the label out of an option. So if you simply want to show labels that are defined inside a select.props(':option-label="(opt) => opt.label.label"') |
Beta Was this translation helpful? Give feedback.
-
Thank you @falkoschindler, this is awesome! I have implemented your suggestion as a class and it works almost perfect: from nicegui import ui
class TSelectWithIcon(ui.select):
"""
A ui.select that accepts a list of dictionaries as options:
[
{
'label': 'Google',
'value': 'goog',
'icon': 'mail',
'description': 'google is a search engine',
},
{
'label': 'Facebook',
'value': 'fb',
'icon': 'bluetooth',
'description': 'facebook is old school',
},
]
"""
def __init__(self, options, **kwargs):
super().__init__(options, **kwargs)
self.add_slot('option', '''
<q-item :props="props" :opt="opt" class="flex items-center gap-2" clickable @click="props.toggleOption(props.opt)">
<q-item-section avatar>
<q-icon :name="props.opt.label.icon"></q-icon>
</q-item-section>
<q-item-section>
<span>{{props.opt.label.label}}</span>
</q-item-section>
</q-item>
''')
self.props(add =':option-label="(opt) => opt.label.label"')
self.props(add =':option-value="(opt) => opt.label.value"') The one thing I can't still figure out is how to correctly assign option-value. I have tried: self.props(add =':option-value="(opt) => opt.label.value"') But this doesn't seem to make any difference, the bind_value still returns a full dictionary of that item, and not just the value field of it, which is ideally what I want. What am I missing there? |
Beta Was this translation helpful? Give feedback.
-
Description
Quasar allows setting options of QSelect as list of dictionaries with keys being label, value, icon, etc, as follows:
ui.select allows two ways of setting the options:
List of strings:
['Apple', 'Google']
Dictionary {label=value}:
{'Google': 'google', 'Apple':'apple'}
That doesn't offer flexibility in using and showing icons, as well as storing IDs and some other useful related information to each value, that we could then receive as args on change events.
Is there a way to bring the format of options in compliance with what QSelect accepts?
P.S. Thank you all for the amazing product you built, I absolutely in love with NiceGUI and the ways I can work with it and there is no limit to extending it and go bare JS whenever I need it.
Beta Was this translation helpful? Give feedback.
All reactions