Hi everyone!
I am trying to create an AG Grid with multiple columns. Two of those columns should have dependent dropdowns, e.g. if I am choosing a country from dropwdown in column “Country”, then in column “City” I should see dropdown containing a list of cities only for that specific country chosen in the previous column.
I found this documentaiton about Dynamic Parameters.
As instructed, I defined this function in dashAgGridFunctions.js
file in the assets
folder.
var dagfuncs = window.dashAgGridFunctions = window.dashAgGridFunctions || {};
dagfuncs.dynamicOptions = function(params) {
const selectedCountry = params.data.country;
if (selectedCountry === 'United States') {
return {
values: ['Boston', 'Chicago', 'San Francisco'],
};
} else {
return {
values: ['Montreal', 'Vancouver', 'Calgary']
};
}
}
But this function example is just for two countries.
What if I have dozens of countries with multiple cities for each?
I have a separate file (let’s call it country.py
) where I am pulling a dictionary of countries with respective cities and dumping it into a json file.
def sql_cat_json():
#pulling a df from database and converting it into a dictionary country_dict
data = [
{"country": "USA", "city": ["Boston", "NY", "LA"]},
{"country": "Canada", "city": ["Montreal", "Vancouver", "Calgary'"]},
{"country": "Japan", "city": ["Kyoto", "Osaka"]},
{"country": "Germany", "city": ["Berlin", "Frankfurt", "Hamburg", "Dresden"]}
]
country_json = json.dumps(data, indent=4)
return country_json
Question 1: How do I send that “country_json” from country.py
to the dashAgGridFunctions.js
file in the assets
folder ?
Question 2: How do I edit the dashAgGridFunctions.js
to be really dynamic and show as many countries & cities as in the country_json
?
Thank for all the help in advance.