在Strapi中创建一个不可编辑的领域

发布于 2025-02-09 05:51:53 字数 321 浏览 2 评论 0原文

我有一个销售产品的项目。

产品可以具有价格和可选:new_price。我希望能够按Price/new_price组合对它们进行排序,这意味着我需要在DB中进行额外的条目,例如“ final_price”。我有一个想法是在内容型构建器/schemo.json中创建一个不可编辑的字段“ new_price”,然后在lifecycles.js中以“ beforecreate”上的“ beforecreate”上的最终价格更新该字段。但是,我不确定Strapi允许这样做,因为我还没有找到指向文档中指向的资源。如果有一种骇人听闻的方法,请告知。我也向有关如何执行此业务逻辑的所有其他建议开放。

先感谢您。

I have a project that sells products.

A product can have a price and optional: new_price. I would like to be able to sort them by price/new_price combined, meaning that I would need an additional entry in the DB like "final_price". I got an idea to create a non-editable field "new_price" in Content-Type Builder/schema.json and then update that with the final price for the product on "beforeCreate" in the lifecycles.js. However, I am not sure Strapi allows that, as I haven't been able to find a resource pointing in the documentation that it can. If there is a hacky way to do it, please advise. I am open to all kinds of other suggestions on how to do this Business logic as well.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

难理解 2025-02-16 05:51:53

Strapi有一种方法可以使该字段在UI上不可编辑。您可以转到content-type-builder->您的组件 - >配置视图。然后单击文本字段。您可以将可编辑的字段作为错误。

There is a way in strapi to make the field non editable on UI. You can go to Content-type-builder-> Your component-> Configure the view. And click on the text field. You can just make editable field as false.
Strapi view

原来是傀儡 2025-02-16 05:51:53

好的,所以我找到了一种“骇人听闻”的方式来做到这一点。我不喜欢它,因为它不是那么美丽而精致,但它可以完成工作。

  1. 在产品架构中,我添加了

     “ final_price”:{
      “ type”:“十进制”
    }
     
  2. ,我创建了一个新功能,该功能将计算我的最终价格并将其写入“ final_price”属性:

      module.exports = {
      beforecreate(event){
        pricecalc(event);
      },,
      update(event){
        pricecalc(event);
      },,
    };
    
    const pricecalc =(event)=> {
      const {data} = event.params;
      data.final_price = data.new_price? data.new_price:data.price;
    };
     
  3. 在管理面板中,在产品中的content-type构建器中,我单击“配置视图”并删除已删除显示字段部分的最终价格字段。这使场隐藏着,大声笑。

  4. 现在我正在用排序对所有产品进行排序:['final_price:asc']

就是这样。希望它能帮助任何人!

Ok, so I found a "hacky" way to do this. I don't like it, as it is not that beautiful and sophisticated, but it does the job.

  1. In product schema.json I added a

    "final_price": {
      "type": "decimal"
    }
    
  2. In lifecycles.js, I created a new function that will calculate my final price and write it in "final_price" attribute:

    module.exports = {
      beforeCreate(event) {
        priceCalc(event);
      },
      beforeUpdate(event) {
        priceCalc(event);
      },
    };
    
    const priceCalc = (event) => {
      const { data } = event.params;
      data.final_price = data.new_price ? data.new_price : data.price;
    };
    
  3. In the admin panel, in Content-Type Builder in Product, I clicked "Configure the view" and deleted the Final Price field from Displayed Fields section. This made the field hidden, lol.

  4. Now I am sorting all products with sort: ['final_price:asc']

That is it.Hope it helps anyone!

盗梦空间 2025-02-16 05:51:53

我看到了两个选项:

(a)添加该字段并忘记它是不可编辑的。您可以使用LifeCycle Hooks在收藏中插入条目时计算final_price https://docs.strapi.io/developer-developer-docs/latest/development/backend-custend-custend-customization/models.html-wlifecycle-hooks

(b)不要添加一个新字段,而要覆盖控制器以返回一个其他字段: https://docs.strapi.io/developer-docs/latest/development/backend-custend-customization/backend-customization/controllers.html#Evtml#Evtml#Evtml#ervending-core-core-conterers

I see two options:

(a) Add the field and forget about it being non-editable. You can calculate the final_price on insertion of an entry in your collection with Lifecycle hooks: https://docs.strapi.io/developer-docs/latest/development/backend-customization/models.html#lifecycle-hooks.

(b) Don't add a new field, but override the controller to return an additional field: https://docs.strapi.io/developer-docs/latest/development/backend-customization/controllers.html#extending-core-controllers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文