-
-
Notifications
You must be signed in to change notification settings - Fork 326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to custom array field #202
Comments
found out that it's doable with custom templates :) |
i know that i have to modify afArrayField template, but it uses afQuickField inside, and afQuickField has many helpers, uses other templates, etc. |
It can get tricky. Can you explain more which parts need customizing? I can provide an example if I know more specifics. |
part of my schema is :
for the reference, i want to have
and have series.$.reference to render with |
OK, if it's just adding options, you could actually do that by customizing the First copy and customize the <template name="afQuickField_addOptions">
<div class="form-group {{#if afFieldIsInvalid name=this.atts.name autoform=this.atts.autoform}}has-error{{/if}}">
{{#unless this.skipLabel}}
{{> afFieldLabel this.afFieldLabelAtts}}
{{/unless}}
{{> afFieldInput attsPlusOptions}}
<span class="help-block">{{{afFieldMessage name=this.atts.name autoform=this.atts.autoform}}}</span>
</div>
</template> I made only two small changes:
So now we can create an Template["afQuickField_addOptions"].attsPlusOptions = function attsPlusOptions() {
var self = this;
var name = self.atts.name;
var additionalAtts = {};
// For certain field names, add options
if (name === "series.$.reference") {
additionalAtts.options = [
{label: "One", value: 1}
];
}
return _.extend(additionalAtts, self.afFieldInputAtts);
}; And finally we tell autoform to always use our custom template for AutoForm.setDefaultTemplateForType('afQuickField', 'addOptions'); |
Is there a way to achieve this without overriding the default template for afQuickField ? |
Not currently, but something is in the works. |
Ok, nice to know :) I tried to provide my own template for afArrayField, copying the one from the bootstrap template. But if I add Thx for your work, guys, this lib is awesome :) |
Here's my usecase: I have a form for an order which uses the bootstrap3 template. An order has an array of order items and I'd like to use the afArrayfield but with a template which will not output labels and will put every inputs on a single line (like a table). Can you advise me a way to achieve this ? |
Finally found the way to do it ! I made my own afArrayField, afObjectField and afQuickField. This way, I was able to pass the template attribute correctly. My afArrayField is a copy of the bootstrap3 template, removing all bs3 panels, but I call afObjectField instead of afQuickField, specify my template and the name attribute ( My afObjectField also removes bs panels and simply call Finally I can fully customize the afQuickField |
thank you @aldeed |
Oh, sorry, try doing this instead: |
it worked |
@aldeed, updating to 0.13.0 made your code not to work ! |
@pajooh, if your options are static, you can put them in the schema now if you want: series: {
type: [Object]
},
"series.$.type": {
type: String
},
"series.$.reference": {
type: Number,
autoform: {
options: [
{label: "One", value: 1},
// etc...
]
}
}, If you do that, no custom template is needed. I'm surprised that the custom template would have stopped working, though. I don't think any changes should be necessary. I'll have to try it out. |
OK, extending options in a custom template does not and cannot work anymore. You'll have to put them in the schema or write out more of the form. In the latest release, you can also set |
may be, i have to open a new issue for this: |
@pajooh, the AutoForm.getFieldValue method is reactive, so this would be technically possible, but you would have to use a reactive options helper (a function in the schema is not currently reactive) and you would need to pass the current index to the options helper, too, because you would need to specifically look up Given the complexity, I think your best bet is to use a custom template for that particular object field. The custom template: <template name="afObjectField_series">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{afFieldLabelText name=this.atts.name}}</h3>
</div>
<div class="panel-body">
{{#if afFieldIsInvalid name=this.atts.name autoform=this.autoform}}
<span class="help-block">{{{afFieldMessage name=this.atts.name autoform=this.autoform}}}</span>
{{/if}}
{{#each afFieldNames name=this.atts.name fields=this.atts.fields omitFields=this.atts.omitFields}}
{{#with getOptions ../atts.name this}}
{{> afQuickField name=.. options=this}}
{{/with}}
{{/each}}
</div>
</div>
</template> This is a copy of the built-in bootstrap template. I only changed the template name to "afObjectField_series" and changed the Then you need to define the Template["afObjectField_series"].getOptions = function (itemFieldName, fieldName) {
if (fieldName === 'reference') {
var type = AutoForm.getFieldValue(formId, itemFieldName + '.type');
return References.find({type: type}).map(function(doc){
return {label: doc.name, value: doc.id};
});
}
}; The one change you will have to make is to replace Finally, in your schema, tell autoform to use your custom template for that object field: series: {
type: Array
},
"series.$": {
type: Object,
autoform: {
template: "series"
}
},
"series.$.type": {
type: String
},
"series.$.reference": {
type: Number
} I did not test any of this, but hopefully it's close enough to help you figure it out. |
thank you @aldeed , with some minor modifications it works: use _makeGeneric for fieldName: Template["afObjectField_series"].getOptions = function (itemFieldName, fieldName) {
var name = SimpleSchema._makeGeneric(fieldName);
if (name === 'series.$.reference') {
var type = AutoForm.getFieldValue(formId, itemFieldName + '.type');
return References.find({type: type}).map(function(doc){
return {label: doc.name, value: doc.id};
});
}
}; add an else block for non-option fields: <template name="afObjectField_series">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{afFieldLabelText name=this.atts.name}}</h3>
</div>
<div class="panel-body">
{{#if afFieldIsInvalid name=this.atts.name autoform=this.autoform}}
<span class="help-block">{{{afFieldMessage name=this.atts.name autoform=this.autoform}}}</span>
{{/if}}
{{#each afFieldNames name=this.atts.name fields=this.atts.fields omitFields=this.atts.omitFields}}
{{#with getOptions ../atts.name this}}
{{> afQuickField name=.. options=this}}
{{else}}
{{> afQuickField name=this options="auto" fields=../atts.fields omitFields=../atts.omitFields}}
{{/with}}
{{/each}}
</div>
</div>
</template> as a general thought, having object and array components available also as a block helper, will provide the user with proper template (minus/plus buttons, etc) while the user has ability to modify array/object fields more elegantly |
I think customizing on array field is a important feature. Love to had it officially documented. |
i want to render an array of objects field, which it's properties need to be customized.
so i can't use afQuickField.
but i like afQuickField auto generated plus/minus buttons for arrays.
how can i do that?
The text was updated successfully, but these errors were encountered: