Conditional logic with Enumeration field - javascript

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

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.

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

Related

TypeError: Cannot call method "indexOf" of null

I'm triying to find the records that includes "SO -" or "NS - SO" or "SO –" or "SWAT" on THE "RESUMEN" field from a CSV file to asigne a new category (in this cases would be "Call Center"). So, I used "indexOf" funtion witch worked so well.
The problem comes when I change the data source (It is a CSV too), this gave me next error on that step:
"Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot call method "indexOf" of null (script#2)"
The objective is to assign a category by identifying the words on the source file
My code
if (RESUMEN.indexOf("SO -")!=-1 || RESUMEN.indexOf("NS - SO")!=-1 || RESUMEN.indexOf("SO –" )!=-1 || RESUMEN.indexOf("SWAT")!=-1)
{
var RESULTADO = "Call Center"
}
else RESULTADO = ""
I expect to assigne call center category like I got with the first file (I did not change nothing)
regards!
You're overcomplicating the issue.
Before the answer, remember something, there are several steps, and combinations of steps, that achieve an incredible number of transformations to make usable patterns, the last resort IS User defined Java Expression.
Seems like what you want to achieve is a Value Mapping, thou the difference from a direct value map in your case, is that the row you're testing must contain "SO -", and the other cases, somewhere in the text.
With this simple filter, you can transform your data that contains those informations as you desire, and on the "FALSE" side, treat it for errors.
This will expand your transformation a bit, but when you need to change something it will be easier than with a single step with a lot of code.
As another answer pointed out, you can achieve the same result with different steps, you don't need the javascript step.
But, if you want to go that route, you should first convert null values into, e.g., empty strings.
Simply add this to the beginning of your javascript code:
if (!RESUMEN){ RESUMEN = ''}
That'll convert nulls to empty strings and then indexOf returns correctly.

Use AJAX or similar technology to narrow down already existing list

Hopefully I explain this to where it makes sense, the most I could find by searching terms like I used in the title gave plenty of autocomplete examples, but nothing quite what I'm looking for. I have a list of buttons (they're coded as inputs right now) and I want to add a search field that will narrow down the buttons as the user types in a search field.
Say for example, I have 30 buttons with popular websites. If a person wanted to pull Google, they'd start typing it out which would start by including everything with the letter "G" in it, then "O", etc. Everything else would "disappear" from the page.
I can sort of think of a way to do this manually, but I think my code wouldn't be DRY. Possibly set an "on" and "off" ID, and use CSS to display:none or something to that effect.
I think the best way to do this would be via AJAX, but there may be some javascript voodoo more applicable.
To easy. At first, its unneccessary to filter the answers serverside, if all the data is already at the users. Also, you shouldnt write html and filter it with js, you should write it in js and generate an html output. Lets start with the structure:
var links=[
{
name:"google",
url:"http://google.com"
},
{nextone}
];
Now generate the links in html:
window.onload=function(){
var container=document.body;//change this to your needs
for(i=0;i<links.length;i++){
var link=links[i];
link.html=document.createElement("a");
link.html.innerHTML=link.name;
link.html.src=link.url;
container.appendChild(link.html);
}
};
If sth is inputed, hide the the unmatched ones:
function filter(string){
//loop trough links
for(i=0;i<links.length;i++){
var link=links[i];
//if string doesnt match name
if(!link.name.split(string)[1]){
link.html.style.display="none";
}else{
link.html.style.display="block";
}
}
}
Use like this:
filter("goo");
You could bind that to an input:
yourinput.addEventListener("onchange",function(){filter(this.value)},false);

Regex on multiple lines of random order

I am using BIRT to design a report based on a database, and one of the fields of the form contain multiple lines, like that :
Site: Place ThePlace
Room: D2 RMD3
InstanceId: OI-RandomChars
The fact is, they are not always in this order, as it is user input (in an other form, not BIRT prompt).
And please note that these fields contain capital letters.
So what I want to do is to extract in three separate columns, so with three regex in JavaScript the Site, the Room, and the InstanceId.
I have tried many things with like catching each row until the end of the line or playing around substrings with various conditions ... and so far I think the best to do is to use string function replace to remove anything different than what I want to catch.
An example for the row Room would be :
row["Log"].replace(/?![Room:\s\S*\s]/, "")
I get an error with this but you can see what I try to do.
Thanks for all the consideration about my problem.
A single regex can become overly complicated and hard to read and mantain for this kind of job.
I would probably consider doing it programmatically like your first instinct was.
First I would consider splitting the string into lines
var lines = string.split("\n\n");
This will split all your double lines into an array
["Site: Place ThePlace", "Room: D2 RMD3", "InstanceId: OI-RandomChars"]
Then cycle trough all your lines and then make your checks.
Your check now can actually be regex if you want, or using substring.
This is an example:
var site, room, InstanceId;
var siteCheckRegex = new Regex("^Site:");
for(var i=0; i<lines.length; i++){
if(siteCheckRegex.test(lines[i])){
site = lines[i].replace("Site:","");
}
[...]
}
It actually depends on what you really want to get out of it and the problem and difference you can find in the user input data.

jQuery tablesorter - sorting a column with mixed text and numbers

I have a table with a column of data that is mixed text and numbers. I'm sorting it using jQuery and the tablesorter plugin. The data that won't sort correctly is equipment tags, for example, "AHU-1", "AHU-2", "AHU-10". The problem is, given those example values, AHU-10 will be placed between AHU-1 and AHU-2. I've found forcing a 'digit' sort doesn't solve the problem.
Here's my question: 1) Does anyone know of an existing parser that I can use in this situation? If there isn't one then I'll need to write my own parser, in which case 2) How should I write the parser? Should I try and translate every letter to a number and do a numeric sort? That's my initial thought.
One more thing, I don't know for sure that a hyphen will be the delimiter. "AHU-1" could also be "AHU1", or "AHU 1", or "AHU:1", or something else.
You do need to write your own parser. What you are looking for is called "natural sort". There are plenty of javascript natural sort algorithms out there. I couldn't find one prewritten for the tablesorter plugin, but googling turns up quite a few.
Assuming all you need to do is sort any string of the pattern AAA-1 as AAA-01 you could do the following:
var myTextExtraction = function(node)
{
// extract data from markup and return it
return node.childNodes[0].childNodes[0].innerHTML
.replace(/([A-Z]{3}-)(\d)/,'$1-0$2');
}
$(document).ready(function()
{
$("#myTable").tableSorter( {textExtraction: myTextExtraction} );
}
);

Need help with an AJAX workflow

Sorry I couldn't be more descriptive with the title, I will elaborate fully below:
I have a web application that I want to implement some AJAX functionality into. Currently, it is running ASP.NET 3.5 with VB.NET codebehind. My current "problem" is I want to dynamically be able to populate a DIV when a user clicks an item on a list. The list item currently contains a HttpUtility.UrlEncode() (ASP.NET) string of the content that should appear in the DIV.
Example:
<li onclick="setFAQ('The+maximum+number+of+digits+a+patient+account+number+can+contain+is+ten+(10).');">
What is the maximum number of digits a patient account number can contain?</li>
I can decode the string partially with the JavaScript function unescape() but it does not fully decode the string. I would much rather pass the JavaScript function the faq ID then somehow pull the information from the database where it originates.
I am 99% sure it is impossible to call an ASP function from within a JavaScript function, so I am kind of stumped. I am kind of new to AJAX/ASP.NET so this is a learning experience for me.
First of all, if you're pulling the questions from the db on page load you most likely have all the answers too, so just keep going with your current approach by jamming the answers into the page as your code sample is doing. Unless your FAQ list has thousands and thousands of questions, doing it the "AJAX way" by hitting the db on each click of the list item doesn't give you much here IMO. If it does have that many questions then a straight list is the wrong way to go anyway.
Secondly, two things to keep in mind re your approach:
you're placing html inside an html attribute
the attribute is specifying a javascript function to call
So you need to make sure your "answer" escapes both html and is valid js. By valid js I mean it can't have new lines and must escape quotes properly. For example, the following html - although valid html - won't fire the onclick and you'd just get a js syntax error:
<li onclick="setFAQ('This line's
multi line and has a single quote in it!')"
To account for these I would say HttpUtility.HtmlAttributeEncode in tandem with System.Web.Script.Serialization.JavaScriptSerializer is more appropriate to the markup you've shown.
JavaScriptSerializer json = new JavaScriptSerializer();
string answerString = "This line's\nmulti line and has a single quote in it!";
string onClickJS = String.Format("setFAQ({0})", json.Serialize(answerString));
string onClickAttr = HttpUtility.HtmlAttributeEncode(onClickJs);
Even better, use .NET's ListItem object and lose HtmlAttributeEncode altogether:
ListItem faqItem = new ListItem(questionString);
faqItem.Attributes.Add("onclick", String.Format("setFAQ({0})", json.Serialize(answerString)));
The html portion is escaped automatically for you, plus it's a lot cleaner.
As for your javascript, you don't have to decode anything in setFAQ(). Just take its argument and put it in into you "answer" div:
function setFAQ(answer) {
document.getElementById('answer').innerHTML = answer
}
I think just using HttpUtility.HtmlEncode may solve your problem. I'm not sure I follow completely though : \

Categories