admin管理员组文章数量:1433500
I am trying to use and learn Strapi Headless CMS while implementing it in a small pany. I need to calculate some fields and display it in the form (while filling fields) and table.
I was looking in the model life cycle but I did not find any cicle regarding input changes, just the model.
I have tried beforeSave
cycle but it is obviously triggered after an user clicks on Save button, but according to their own documentation should work:
beforeSave: async (model, attrs, options) => {
model.set('FinalCost', attrs.budget- attrs.cost);
}
This code doesn't work, but I am trying to show how the finalCost
field, after filling cost
and budget
should look like (in real time). I also tried attrs.FinalCost = attrs.budget - attrs.cost
but nothing changes.
Any clues? Thanks in advance.
Edit:
I had to verify that budget
field were truthy before setting FinalCost
:
beforeSave: async (model, attrs, options) => {
if (attrs.FinalCost) {
attrs.FinalCost = attrs.budget- attrs.cost;
}
}
But this does not answer my first issue, that this should work in real time and bot until I press "Save" button.
I am trying to use and learn Strapi Headless CMS while implementing it in a small pany. I need to calculate some fields and display it in the form (while filling fields) and table.
I was looking in the model life cycle but I did not find any cicle regarding input changes, just the model.
I have tried beforeSave
cycle but it is obviously triggered after an user clicks on Save button, but according to their own documentation should work:
beforeSave: async (model, attrs, options) => {
model.set('FinalCost', attrs.budget- attrs.cost);
}
This code doesn't work, but I am trying to show how the finalCost
field, after filling cost
and budget
should look like (in real time). I also tried attrs.FinalCost = attrs.budget - attrs.cost
but nothing changes.
Any clues? Thanks in advance.
Edit:
I had to verify that budget
field were truthy before setting FinalCost
:
beforeSave: async (model, attrs, options) => {
if (attrs.FinalCost) {
attrs.FinalCost = attrs.budget- attrs.cost;
}
}
But this does not answer my first issue, that this should work in real time and bot until I press "Save" button.
Share Improve this question edited Feb 13, 2020 at 15:22 Maramal asked Feb 13, 2020 at 12:36 MaramalMaramal 3,4749 gold badges59 silver badges102 bronze badges1 Answer
Reset to default 4Here are some resources that will help you.
Model's life cycles function are called when an entry is created/update/...
So in your case FinalCost
is an attribute of your model and its value will be updated and saved any times your update your entry.
This is the same system as in this guide - https://docs-v3.strapi.io/developer-docs/latest/guides/slug.html
If you don't want to store the value is a field, you will have to update the API controller to calculate the value on the fly.
That is done in this guide - https://docs-v3.strapi.io/developer-docs/latest/guides/custom-data-response.html
本文标签: javascriptStrapi CMS Add calculated fieldStack Overflow
版权声明:本文标题:javascript - Strapi CMS: Add calculated field - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745605584a2665818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论