I have this huge form that only gets pre-filled data if data already exists in a database. Otherwise, none of the text boxes should have the value parameter in them. I thought it would just ignore the value parameters if the variable I get data from does not exist. But instead, I get an error.
How to I handle this case? Will I have to do an if check before each text box in Jade like the following?
if (typeof(prefilled_data) !== 'undefined')
input.form-control#prevalence(type="text", name="prevalence")
else
input.form-control#prevalence(type="text", name="prevalence", value=prefilled_data.tb_burden_estimates.prevalence)
While I don't mind doing this (Sublime Text will help with all the copy-pasting), there are quite a few form fields, so it might get ugly. Is there a way to consolidate this into one check somewhere?
you seemed to be suggesting that the if statement's were going to be bulky/make the code hard to read if they were there.. my suggestion would be to programmatically create the inputs, there by reducing the if statements to a more manageable number and answering your question about being able to "consolidate this into one check somewhere"
EDIT
If you are looking to access data in js.. I have been known to use something like:
script(type='text/javascript').
window.prefilled_data = !{ JSON.stringify(prefilled_data) };
This will allow you then to access the global window.prefilled_data variable to get front end version of your data
you can do this:
- if (typeof(prefilled_data) === 'undefined'){
- prefilled_data = '';
- }
input.form-control#prevalence(type="text", value=#{prefilled_data})
if prefilled_data is undefined you just set a '' value
Related
In Qualtrics, I am trying to set two embedded data fields based on the answer to a yes or no question by using an if/else statement in JavaScript. I tried to come up with the correct code, but I am new to JS and what I've come up with (shown below) isn't working; the fields don't get populated.
I have created the embedded data fields in the beginning of the survey flow, so I don't think that's the issue.
Qualtrics.SurveyEngine.addOnPageLoad(function() {
if("${q://QID14/ChoiceGroup/SelectedChoices}"!="Yes")
{Qualtrics.SurveyEngine.setEmbeddedData("Active_Duty_Yes", "X");
Qualtrics.SurveyEngine.setEmbeddedData("Active_Duty_No", "")}
else if("${q://QID14/ChoiceGroup/SelectedChoices}"!="No")
{Qualtrics.SurveyEngine.setEmbeddedData("Active_Duty_No", "X");
Qualtrics.SurveyEngine.setEmbeddedData("Active_Duty_Yes", "")};
});
Your onload function name is wrong. It should be addOnload instead of addOnPageLoad.
A couple of other suggestions:
Is is generally better to use use recodes instead of strings in
logic (e.g., "${q://QID14/SelectedChoicesRecode}"!="1"). That way
if you change the choice text the logic doesn't have to change.
It is generally better to give embedded data flags values of 1 and 0.
So i've been asked to remake some registration forms. The way its supposed to work is, that an interpreter chooses X amount of languages in the first select box. Then based on the selections of languages, the user must specify from which languages they can translate from/to.
I want to store this data in a key/value array, with the key being "LanguageFrom" and Value being another array, of "LanguagesTo". This is how i have solved this:
function btnTest() {
var fromArray = $('.freelancerLanguagesFrom').map(function() {
return $(this).val();
}).get();
var toArray = $('.freelancerLanguagesTo').map(function() {
return $(this).val();
}).get();
var tempArray = {};
tempArray[fromArray] = toArray;
}
This method is being called with an "onclick" function in the html part. The user should specify which languages he can translate to for each of the chosen languages in the first box,
I am aware that this probably isn't the ideal approach, but im still an inexperienced developer, and i'd love to hear your take on another approach.
Now comes my problem:
1) How do i make it so the array wont overwrite the existing array with each button click, and instead just add to the array?
2) How do i process this array on the server side (php), so that i can store the values in my database?
3) Is it possible to skip the flow where the user has to press the save(gem) button after each language he has chosen?
edit: Question 1 and 3 are now solved, my only problem is accessing the array i made in js, on the php side
1) tempArray exists only in the scope of the btnTest() function. Declare it outside (in the global scope), initialize it as {} and don't reset it every time you click the button. The way you get the fromArray variable may require some tweaking depending on whether the "from" list can accept a multiple selection or not.
2) Ajax may help. Create a php endpoint to receive the request and call it using ajax. You can work on the array using JSON. Send your data using JSON.stringify(tempArray) and read it using json_decode() in your php script, or simply set the request headers as "application/json" to have it done automatically for you.
3) I personally wouldn't automate this process. Let's say I have 4 languages, Italian, English, French and Chinese.
I have selected a desirable state of languages I can handle:
Italian -> English, French
But I also know how to translate French in Italian so I click, in the from list, French, and I get
French -> English
Which is an undesirable state, for me, because I don't know how to do that. Especially if I were to select many languages, I'd get, inbetween 2 states I want to save, an indefinite amount of states I don't want to save.
If you still want to do so, you need to move the even listener from the button to the list(s), with the onchange event.
I'd also suggest you do your event binding trough jQuery, if you aren't already.
Hope this helped.
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.
I have a form for editing a user details, where it should check if the user has make modification if not then don't make changes.
Currently am using if to check which is working but I still feel is not effective way for this, because I have multiple fields which have to check each of them which makes the code repetitive and huge.
if (f.value.first_name) {
this.user.firstName = f.value.first_name;
}
I have tried using elvis operator but it's not working for me
this.user.firstName ? this.user.firstName :f.value.first_name;
Is there any other effective way to do this?
You can loop through each form controls and check if they are dirty (modified).
Object.keys(f.controls).forEach((key: string) => {
if (f.get(key).dirty) {
this.user[key] = f.get(key).value
}
});
I keep a copy of the original object and then do this:
get isDirty(): boolean {
return JSON.stringify(this.originalProduct) !== JSON.stringify(this.currentProduct);
}
I stringify the copy of the original product (before edits) and the current product and compare them. I assume if they are not equal that something changed.
There are some caveats to this approach, especially if your object structure is complex as there may be cases that stringify does not create the strings exactly the same. But I have not run into this case.
Please don’t answer this if you don’t take the time to understand my question or have a reasonable answer. I have got a few answers that is far on the side and I think I explain my problem very clear. Shall this problem drive me nuts or is there somebody out there with a straight and clear answer on Titanium.App.Properties?
I have a login form that stores the username in one:
Titanium.App.Properties.setString("userName",e.value);
and the password in another:
Titanium.App.Properties.setString("passWord",e.value);
The forms TextFields holds these values(after a store) even if I close the window, shut down and restarts the app. This because of the App.Properties.getString("value"); I suppose….?!
But when I copy the hole form with its App.Properties.getString("value"); to another window, the TextFields are empty and contains no value at all. I understand that the Properties string must be there some where in App.Properties, but how can grab it and use it another place in the app?
My question is: How to get the value from my
var userNameValue = Titanium.App.Properties.getString("userNameValue");
to be available in another window or for the hole app(global)? How can I grab it and make use of it a another place in the app? I don’t see a good reason to make these, only separate words, into objects(JSON) since the userName only contains a e-mail address and the passWord consist only of continuing letters or numbers. But if you mean I have too, -how do I set this from my TextField and get it in another TextField somewhere else in my app. I have not had any luck so far. I hope you can help me out and I can keep sane.
Titanium.App.Properties.getString("userNameValue");
This is globally Available, any Propertie of the Titanium Object is accessible in each file.
but if for some reason this doesnt work for you and you want to set a global variable,
you could do the following:
Create a file called myGlobals.js //or anything else,
//Put this in there e.g
var myProperties = {};
in any file you want to use it write in the first line
Ti.include('myGlobals.js');
Then you can make a propertie global available, for example write this in app.js somewhere where the app initializes
myProperties.Username = Titanium.App.Properties.getString("userName");
Then you can get the value in each file by accesing the propertie
myProperties.Username
//of course the Propertie has to be set before you can get them
( Titanium.App.Properties.setString("userName",e.value); ) //like you do it
But, Titanium.App.Properties.getString("userName");
should be avilable from any file anyway, (but you can give this a try although i dont think its nice to do it like this)
i had a similar problem where this didnt get any value from a propertie set in the ios settings as default value.
I had to go to the settings and manually change or edit the default value and then after a restart
Titanium.App.Properties.getString("userName");
returned the value as it should,
i hope this helps you =)
Answer to the Comment
I'm glad i could help you =)
Yes you can Use an focus EventHandler like this :
textfield.addEventListener("focus",function(){
textfield.value = "Test";
});
Beside that , are you using the identical Textfield for both windows ? like
var Textfield = Ti.Ui.createTextField({...});
and add it to 2 different windows ?
win1.add(Textfield);
win2.add(Textfield);
That led for me to Problems with labels in TableViewRows, using an identical Row 2 times in the TableView
The Text displayed only on 1 Label, sometimes it switched the Labels
I think you can't add one and the same titanium object to more then one other object
Maybe that could be the culprit,
dunno just an idea =)