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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

Here 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