Hey all,
I was planning on creating a datatable that lets user insert data in the cells. Over time, the program will save all inserted data and provide a dropdownlist in the cells, so the user would be able to either choose from the dropdown or type his input AND the user has to be able to be insert values that are not in the dropdownlist. That is the part I am struggling with.
I tried to create a reproducable code. It differs a little from my implementation so far, but it shows the problem anyways. This would be the code:
from dash import html, Output, Input, State, Dash
from dash.dash_table import DataTable
def set_children():
options = {'Column1': ["Value1"], 'Column3': ["Value1"]}
return [
DataTable(
id='editable-table',
columns=[
{'name': 'Col1', 'id': 'Column1', 'editable': True, "presentation": "dropdown"},
{'name': 'Col2', 'id': 'Column2', 'editable': True},
{'name': 'Col3', 'id': 'Column3', 'editable': True, "presentation": "dropdown"},
],
data=[
{'Column1': '', 'Column2': '1', 'Column3': ''}
],
editable=True,
dropdown={
'Column1': {
'options': [{'label': i, 'value': i}
for i in options["Column1"]],
},
'Column3': {
'options': [{'label': i, 'value': i}
for i in options["Column3"]],
},
},
),
# Button to add new row
html.Button("new_row", id="add-row-button")
]
app = Dash()
app.layout = html.Div(id="main_div", children=set_children())
#Callback to add new row
@app.callback(
Output('editable-table', 'data'),
[Input('add-row-button', 'n_clicks')],
[State('editable-table', 'data')],
prevent_initial_call=True
)
def add_row(n_clicks, current_data):
if n_clicks:
current_data.append({'Column1': '', 'Column2': '1', 'Column3': ''})
return current_data
app.run_server(debug=True)
The idea would be that the options variable expands with new user input, but at the moment the program only lets me insert values that are already in the options dictionary (here it would be “Value1”) If I enter something else the dropdown only shows something like “no result” and the moment I exit the cell, it resets to an empty cell. It would be great if you could help me.
Thanks in advance!