Hi
Is it possible to set a default value for an <object-dropdown />
? So for example I want to have a list of possible companies but have the selected value be defaulted to the active user’s company?
Hi
Is it possible to set a default value for an <object-dropdown />
? So for example I want to have a list of possible companies but have the selected value be defaulted to the active user’s company?
Yes, this is pretty easy to do from your view JS, like so …
Let’s assume we have a transaction object that we want to have belong to a company, and we want to allow the end user to select the company, but have the selection defaulted to the user’s company (a parent relationship on the user object)
View XML (partial)
<param name="transaction" type="transaction" />
<var name="companies" type="query:company" />
<object-dropdown query="companies" bind="transaction.company" label="Company" empty-message="Your items will appear here" required="false" />
View JS (partial)
function init() {
view.companies = DB.company.all().orderBy('name');
var defaultCompany = user.company();
// what if user doesn't have a company set, let's get a different default
if (!defaultCompany) {
defaultCompany = DB.company.first("name = ?", "Best Company"); // whatever makes sense
}
view.transaction.company(defaultCompany);
// Note, it is completely fine to NOT have a default value here
// You could even skip the null check and just pass in null here and have the default value be null, i.e. nothing selected
}