如何在单个列表中获取动态选择的下拉列表的所有选择的值? [英] How to get all selected values of dynamically selected dropdown in a single list?

查看:122
本文介绍了如何在单个列表中获取动态选择的下拉列表的所有选择的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在一个列表中获取动态选择的下拉列表的所有值? 我尝试使用for循环迭代在回调内部进行回调,但是无法获取所需的列表.

How do I get all values of dynamically selected dropdown in a single list? I tried doing callback inside callback with for loop iteration, but unable to get the desired list.

def a_function 的问题,其中存在回调内部的回调.

The issue with def a_function where callback inside a callback is present.

如何获取多个下拉列表的单个列表,这些列表是动态更新的?

How to get in a single list of multiple dropdowns which is dynamically updated?

import dash
import dash_core_components as dcc
import dash_html_components as html

step = html.Div(
        children=[
            "Menu:",
            dcc.Dropdown(options=[{'label': v, 'value': v} for v in ['option1', 'option2', 'option3']])
        ])

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__,external_stylesheets=external_stylesheets)
app.config['suppress_callback_exceptions']=True

div_list = [step]

app.layout = html.Div(
    children=[
        html.H1(children='Hello Dash'),
        html.Div(id='step_list', children=div_list),
        html.Div(id='local'),
        html.Button('Add Step', id='add_step_button', n_clicks_timestamp='0'),
        html.Button('Remove Step', id='remove_step_button', n_clicks_timestamp='0'),
        html.Div(id='tester_div'),
        html.Div(id='tester_div_2')])

@app.callback(
    [dash.dependencies.Output('step_list', 'children'),
    dash.dependencies.Output('local','value')],
    [dash.dependencies.Input('add_step_button', 'n_clicks_timestamp'),
    dash.dependencies.Input('add_step_button', 'n_clicks'),
     dash.dependencies.Input('remove_step_button', 'n_clicks_timestamp')],
    [dash.dependencies.State('step_list', 'children')])
def add_step(add_ts, clicks, remove_ts, div_list):
    add_ts = int(add_ts)
    remove_ts = int(remove_ts)
    if add_ts > 0 and add_ts > remove_ts and len(div_list) < 4:
        div_list += [html.Div(children=[
            "Menu:",
            dcc.Dropdown(id='dropdown_id_{}'.format(clicks), options=[{'label': v, 'value': v} for v in ['select1', 'select2', 'select3']])
        ])]
    if len(div_list) > 1 and remove_ts > add_ts:
        div_list = div_list[:-1]
    return div_list,len(div_list)


@app.callback(
    dash.dependencies.Output('tester_div', 'children'),
    [dash.dependencies.Input('local', 'value')])
def a_function(value):
    all_output = []
    if value:
        for i in range(1,value+1):
            @app.callback(dash.dependencies.Output('tester_div_2','children'),
                          [dash.dependencies.Input('dropdown_id_{}'.format(i), 'value')])
            def drop_output(valued):
                all_output.append(valued)
    return all_output

if __name__ == '__main__':
    app.run_server(debug=False)

在此图像中,输出应在这样的列表中:[option2,select1]

In this image, the output should be in a list like this: [option2, select1]

在此图像中,输出应在这样的列表中:[option3,select3,select1,select2]

In this image, the output should be in a list like this: [option3, select3, select1, select2]

推荐答案

Dash不适用于其ID在布局的初始构建中不存在的组件.仅当页面首次加载时已经在页面上存在这些组件时,才可以像这样动态添加组件,这些组件可能是您稍后显示的隐藏组件.据我所知,回调中的回调由于类似原因不会起作用,但是您可以在循环中创建回调.

Dash does not work with components whose IDs are not present at the initial build of the layout. Dynamically adding components like this can only work if those components are already present on the page when it first loads, probably as hidden components which you later reveal. Callbacks within callbacks, as far as I know, won't work for similar reasons, but you can create callbacks in a loop.

您需要重新调整布局的设置,如果需要更多帮助,还可以编辑帖子.

You'll need to rework the setup of your layout a bit, and maybe edit your post if you need more help.

这篇关于如何在单个列表中获取动态选择的下拉列表的所有选择的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆