I am working on app that manages an office.
i want to create a from with autoform, that contains 2 drop down lists : names of clients and references of debts.
When the user selects a client the second list should only shows debts of the client selected.
Try this, of course youāll need to tweak here and there
'client': {
label: 'Client',
type: String,
/* If client is transitional and you don't want to save it, then keep the
optional and autovalue attributes, but if you want to also save the client
information, then remove optional and autoValue from the schema
*/
optional:true,
autoValue: function() {
this.unset();
},
autoform: {
firstOption: 'Select a client',
options: function() {
return Clients.find({},{sort: {name: 1}).map(function(client){return {label: client.name, value: client._id};});
}
}
},
'debt': {
label: 'Debt',
type: String,
allowedValues: function() {
return Debts.find({}).map(function(debt){return debt._id;});
},
autoform: {
firstOption: 'Select debt option',
options: function() {
var client = AutoForm.getFieldValue('client');
return Debts.find({clientId: client},{sort: {name: 1}}).map(function(debt){return {label: debt.name, value: debt._id};});
}
}
}
thank you so much, that helps me a lot.
I just have one more question, can i add another drop down list, i mean can i have 3 dependant drop down lists. And how does that work ?
You can use as many dropdowns as you want, you just follow the same pattern. You use the options property and AutoForm.getFieldValue('fieldYouWantToDependOn') which is reactive so an extended example would be:
Maybe a silly issue, or a problem with some other part of my app, but I seem to be running into problems trying to use this patter within another collectionās Schema. Was this ever the intention?
I am attempting to create an autoform for making new āPayoutsā with several dependent drop down lists. Here is a dead-simple example trying to mimic your āSelect a clientā options:
The log is just a sanity check while I am coding. Despite testing with autopublish on the Server it gives us the expected object, but not the Client, therefore not populating the list.
After messing with the publish/subscribe setup I now know that the dropdown loads⦠eventually. I think the wait-on needs tweaking because I depends on 2 collections in this form.
including a wait-on and data function in my router controller
Iāve created a change event whenever thereās a selection on the father and i can see the Id of the selected father on the console but no option on the children dropdown.
Note: Since this is not an array and I realized that Iām not making any sense if I put a drop down list if the data has only one option.
Hereās my schema on the children part:
'child1': {
label: 'Child 1',
type: String
},
'child2': {
label: 'Child 2',
type: String
},
'child3': {
label: 'Child 3',
type: String
},
max of 10 children
.....
I might change the context of the query, like map all the children in family collection instead of one child on drop down list.
You probably might be wondering why I separated all the children field, because I have an import CSV functionality and itās a pain to import a file if you have a nested fields using simple schema, I have to flatten my schema in order to work the import.
To better understand my use case, the context is for insurance claims( claims collection), so when the father claims his insurance, he has to choose his dependent children which he enrolled in enrollments collection.
How do I map all the children on the above context?