最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Strapi CMS: Add calculated field - Stack Overflow

matteradmin8PV0评论

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

Post a comment

comment list (0)

  1. No comments so far