首页 > 解决方案 > 保存下拉列表的选定值并默认在另一个下拉列表/重复下拉列表中分配它

问题描述

我想知道是否可以保存下拉列表的选定值并默认将其放在具有相同值但位于另一个选项卡中的下拉列表中:

uriCounts 选项卡

 dcc.Tab(label='uriCounts', value = 'es_uricounts', children = [
    html.Div([
    html.Br(),
    html.H3("Choose uriCounts file version: "),
    dcc.Dropdown(id='uriCounts_dropdown',options=[
        {'label': 'Oct 1st 2016', 'value': 'Oct 1st 2016'},
        {'label': 'Oct 1st 2020', 'value': 'Oct 1st 2020'},
        {'label': 'May 25th 2021', 'value': 'May 25th 2021'},
        {'label': 'Jun 25th 2021', 'value': 'Jun 25th 2021'}], 
        placeholder="Version"),
     html.Br(),
    html.Div(id='uriCounts_container')
    ],style = {'margin-left': '3.2552083333333335vw', 'margin-right': '3.2552083333333335vw'})
     ], style = tab_style, selected_style = tab_selected_style)

对计数选项卡

dcc.Tab(label='pairCounts', children = [
         html.Div([
            html.Br(),
    html.H3("Choose pairCounts file version: "),
    dcc.Dropdown(id='pairCounts_dropdown',options=[
         {'label': 'Oct 1st 2016', 'value': 'Oct 1st 2016'},
        {'label': 'Oct 1st 2020', 'value': 'Oct 1st 2020'},
        {'label': 'May 25th 2021', 'value': 'May 25th 2021'},
        {'label': 'Jun 25th 2021', 'value': 'Jun 25th 2021'}], 
        value=**uriCounts_value**),
     html.Br(),
    html.Div(id='pairCounts_container')
    ], style = {'margin-left': '3.2552083333333335vw', 'margin-right': '3.2552083333333335vw'})
      ], style = tab_style, selected_style = tab_selected_style)

当您在选项卡的下拉列表中选择一个值时uriCounts,我希望选项卡的下拉列表中pairCounts分配该值。

如果它可以以相反的顺序完成也很好:首先在选项卡的下拉列表中选择一个值,pairCounts然后在选项卡的下拉列表中选择该uriCounts

希望你能帮我解决这个问题。提前致谢。

标签: plotlyplotly-dashdashboardplotly-python

解决方案


@AnnMarieW 在Dash 社区论坛中给了我这个解决方案

在回调中同步下拉列表是一种可能的解决方案:

@app.callback(
    Output('dropdown1', 'value'),
    Output('dropdown2', 'value'),
    Input('dropdown1', 'value'),
    Input('dropdown2', 'value'),
)
def sync_dropdowns(val1, val2):
    ctx = dash.callback_context
    input_id = ctx.triggered[0]['prop_id'].split('.')[0]
    if input_id == 'dropdown1':
        return dash.no_update, val1
    else:
        return val2, dash.no_update

推荐阅读