/
cell_editor_dmc_select.py
206 lines (171 loc) · 5.7 KB
/
cell_editor_dmc_select.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""
Grid with dcc.Dropdown component as a cell editor
"""
import dash_ag_grid as dag
from dash import Dash, html, dcc, Input, Output
import pandas as pd
import dash_mantine_components as dmc
app = Dash(__name__)
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/ag-grid/olympic-winners.csv"
)
columnDefs = [
{
"field": "country",
"cellEditor": {"function": "DMC_Select"},
"cellEditorParams": {
"options": df.country.unique(),
"clearable": True,
"placeholder": "Select Country",
"shadow": "xl",
},
"cellEditorPopup": True,
"singleClickEdit": True,
},
{"field": "year"},
{"field": "athlete"},
{"field": "age"},
{"field": "date"},
{
"field": "sport",
"cellEditor": {"function": "DMC_Select"},
"cellEditorParams": {
"options": df.sport.unique(),
"creatable": True,
"clearable": False,
"shadow": "xl",
},
"cellEditorPopup": True,
"singleClickEdit": True,
},
{"field": "total"},
]
defaultColDef = {
"resizable": True,
"sortable": True,
"filter": True,
"minWidth": 100,
"editable": True,
}
app.layout = html.Div(
[
dcc.Markdown(
"This grid has Dash Mantine Components dmc.Select in the Country and Sport columns"
),
dag.AgGrid(
id="cell-editor-dmc-grid",
columnDefs=columnDefs,
rowData=df.to_dict("records"),
columnSize="responsiveSizeToFit",
defaultColDef=defaultColDef,
),
html.Div(id="cell-editor-dmc-output"),
],
style={"margin": 20},
)
@app.callback(
Output("cell-editor-dmc-output", "children"),
Input("cell-editor-dmc-grid", "cellValueChanged"),
)
def selected(changed):
return f"You have selected {changed}"
if __name__ == "__main__":
app.run_server(debug=True)
"""
Add this to the dashAgGridFunctions.js file in the assets folder
----------------
var dagfuncs = window.dashAgGridFunctions = window.dashAgGridFunctions || {};
// cell editor custom component - dmc.Select
dagfuncs.DMC_Select = class {
// gets called once before the renderer is used
init(params) {
// create the cell
this.params = params;
// function for when Dash is trying to send props back to the component / server
var setProps = (props) => {
if (typeof props.value != typeof undefined) {
// updates the value of the editor
this.value = props.value;
// re-enables keyboard event
delete params.colDef.suppressKeyboardEvent;
// tells the grid to stop editing the cell
params.api.stopEditing();
// sets focus back to the grid's previously active cell
this.prevFocus.focus();
}
};
this.eInput = document.createElement('div');
// renders component into the editor element
ReactDOM.render(
React.createElement(window.dash_mantine_components.Select, {
data: params.options,
value: params.value,
setProps,
style: {width: params.column.actualWidth-2, ...params.style},
className: params.className,
clearable: params.clearable,
searchable: params.searchable || true,
creatable: params.creatable,
debounce: params.debounce,
disabled: params.disabled,
filterDataOnExactSearchMatch:
params.filterDataOnExactSearchMatch,
limit: params.limit,
maxDropdownHeight: params.maxDropdownHeight,
nothingFound: params.nothingFound,
placeholder: params.placeholder,
required: params.required,
searchValue: params.searchValue,
shadow: params.shadow,
size: params.size,
styles: params.styles,
switchDirectionOnFlip: params.switchDirectionOnFlip,
variant: params.variant,
}),
this.eInput
);
// allows focus event
this.eInput.tabIndex = '0';
// sets editor value to the value from the cell
this.value = params.value;
}
// gets called once when grid ready to insert the element
getGui() {
return this.eInput;
}
focusChild() {
// needed to delay and allow the component to render
setTimeout(() => {
var inp = this.eInput.getElementsByClassName(
'mantine-Select-input'
)[0];
inp.tabIndex = '1';
// disables keyboard event
this.params.colDef.suppressKeyboardEvent = (params) => {
const gridShouldDoNothing = params.editing;
return gridShouldDoNothing;
};
// shows dropdown options
inp.focus();
}, 100);
}
// focus and select can be done after the gui is attached
afterGuiAttached() {
// stores the active cell
this.prevFocus = document.activeElement;
// adds event listener to trigger event to go into dash component
this.eInput.addEventListener('focus', this.focusChild());
// triggers focus event
this.eInput.focus();
}
// returns the new value after editing
getValue() {
return this.value;
}
// any cleanup we need to be done here
destroy() {
// sets focus back to the grid's previously active cell
this.prevFocus.focus();
}
};
"""