0

I'm creating a CMS using strapi for a client and I would like to give them control of what questions are in a questionnaire. In the questionnaire, each question will be under a section:

-section 1
---question
---question
-section 2
---question
---question

So in strapi I created a collection type with two fields: Section (type enumeration) and Question (type text).

My issue is strapi won't allow me to have spaces in my enumerations so I'm forced to make my section "business_info" when on the site I want to display it as "Business Information". I wrote a simple mapping function (method in vue js) to make it work:

sectionMapping(section) {
 switch(section) {
     case 'business_info': 
         return 'Business Information';
     case 'target_market':
         return 'Target Market';
     default: 
         return 'Section'
}

With this, every time my client wants to add a section to the questionnaire, I would have to do a code update which is not ideal. One solution I came up with was changing section from an enumeration to a text data type, but if my client makes a typo, it would create a whole other section. That's why I like the idea of enumeration.

Any tips?

Thank You

ajohnson10209
  • 644
  • 1
  • 6
  • 14

2 Answers2

1

Probably the most dynamic way to accomplish this is to replace underscores with spaces and uppercase the first letter of each word.

First you want to split the string into an array using the split function.

Next you want to iterate over over the resulting array using map, and uppercase the first letter of each word. The example below is take from this question:

section.charAt(0).toUpperCase() + s.slice(1);

Finally join the array of strings together, with a space as a delimiter. When you put everything together you should get a function looking something like:

sectionMapping(section) {
    return section.split("_")
        .map(s => s.charAt(0).toUpperCase() + section.slice(1))
        .join(" ");
}

You will have to change the way you handle default case to return "Section", but this function should eliminate the need to refractor this function each time you want to add a new section title.

joshmeranda
  • 3,001
  • 2
  • 10
  • 24
  • Your solution works great! You just have to replace + "section.slice(1)" with "+ s.slice(1)". This is a good workaround! thank you so much! – ajohnson10209 Aug 22 '20 at 15:08
0

I can see I'm a bit late, but maybe it helps someone.

Strapi (not sure from which version) allows you to manually change enums into "unacceptable" forms through code. So you write "business_info", but then, in code, you can turn it into "Business Information". Won't throw up any problems if you do it that way.

This can be done for both components and models. Easy way to find it is to search for "enumeration". enter image description here

enter image description here