JavaScript multiple checkboxes - delimited list - store and parse - javascript

I was wondering if anybody can help
I'm new and don't know any  Javascript.
I need help for my caspio app.
The code below works I just need to get the second part
I got the first part of storing the values of checked checkboxes in a database field as a comma de-limited list.
Now I need to read the comma de-limited list from the database and update the checkboxes accordingly.
<SCRIPT LANGUAGE="JavaScript">
function concatenate()
{
var Resultfieldname = "CheckboxChoices";
var firstVirtual = 1;
var lastVirtual = 3;
var ResultString = "";
var virtualFieldName = "";
for (i=firstVirtual ;i<=lastVirtual; i++)
{
virtualFieldName = "cbParamVirtual"+i;
if (document.getElementById(virtualFieldName).checked) ResultString = ResultString + "," + document.getElementById(virtualFieldName).value;
}
Resultfieldname = "EditRecord"+Resultfieldname;
if (ResultString.length>0) ResultString = ResultString.substr(1);
document.getElementById(Resultfieldname ).value = ResultString;
}
document.getElementById("caspioform").onsubmit=concatenate;
</SCRIPT>

Related

Search strings of an array in content of several div referenced by class and perform action depending on the condition

I have this basic code for the development of what I need, who can help me is a lot of help for me !!
note: I used the function .search () and replace () without getting good results, I do not know what else way to take
var fjstr = document.getElementsByClassName("js-product-variants-list");
var fjinnerColor = '';
var fjscolor2 = ["Rojo-Wengue",
"Aqua-Natural",
"Gris/Decape",
"Rosa-Blanco",
"Turqueza-Wengue",
"Turqueza-Blanco",
"Naranja-Natural",
"Amarillo-Natural"];
var resultado = "";
var pos = -1
fjscolor2.forEach(function(element) {
pos = fjstr.innerHTML.search(element.toString());
if(pos!=-1){
resultado += "<a href='#" + (fjscolor[i]) + "' ><li class='fj-product-color-icon' id='color-" + (fjscolor[i]) + "'></li></a>"
document.getElementsByClassName('cont-product-variacion-color')[0].firstElementChild.innerHTML = resultado;
}
});

Save Array Values Column wise to CSV file using Javascript

Trying to save my Array values to CSV file column wise using javascript. Right now my code saves the array values row wise.
Current Output -
country1,country2,country3,country4
capital1,capital2,capital3,capital4,
currency1,currency2,currency3,currency4
Required Output -
country1,capital1,currency1
country2,capital2,currency2
country3,capital3,currency3
country4,capital4,currency4
My code so far -
<form>
<input id="download" type="button" value="Download">
</form>
function downloadableCSV(rows) {
var content = "data:text/csv;charset=utf-8,";
rows.forEach(function(column, index) {
content = content + column.join(",") + "\n";
});
return encodeURI(content);
}
var country = ["England","Australia","Mexico","Brazil","Spain","Portugal","Italy","Thailand","Japan"];
var capital = ["London","Canberra","Mexico City","Brasilia","Madrid","Lisbon","Rome","Bangkok","Tokyo"];
var currency = ["Pound","Dollar","peso","Brazilian real","Euro","Euro","Euro","Thai baht","Japanese yen"];
var continent = ["Europe","Australia","NorthAmerica","SouthAmerica","Europe","Europe","Europe","Asia","Asia"];
var language = ["English","English","Spanish","Portuguese","Spainish","Portuguese","Italian","Thailand","Japanese"];
var heading = ["Country","Capital","currency","continent","language"];
var rows = [[heading],[country],[capital],[currency],[continent],[language]];
$("#download").click(function() {
window.open(downloadableCSV(rows));
});
I tried using various functions before but was unsuccessful.Is there a way to implement the required output. Any help would be greatly appreciated.
To group your data you can use zip from lodash (https://lodash.com/docs/4.17.4#zip)
Here is working js code (https://jsbin.com/ditodosime/edit?html,js,output):
function downloadableCSV(heading, rows) {
var content = "data:text/csv;charset=utf-8,";
var dataRows = rows.map(function(columnValues, index) {
return columnValues.join(",");
});
content += heading + "\n" + dataRows.join("\n");
return encodeURI(content);
}
var country = ["England","Australia","Mexico","Brazil","Spain","Portugal","Italy","Thailand","Japan"];
var capital = ["London","Canberra","Mexico City","Brasilia","Madrid","Lisbon","Rome","Bangkok","Tokyo"];
var currency = ["Pound","Dollar","peso","Brazilian real","Euro","Euro","Euro","Thai baht","Japanese yen"];
var continent = ["Europe","Australia","NorthAmerica","SouthAmerica","Europe","Europe","Europe","Asia","Asia"];
var language = ["English","English","Spanish","Portuguese","Spainish","Portuguese","Italian","Thailand","Japanese"];
var heading = ["Country","Capital","currency","continent","language"];
var rows = _.zip(country,capital,currency,continent,language);
window.open(downloadableCSV(heading, rows));

firebug TypeError: array element is null

I have a file which contains a list of emails addresses and a CSV file which contains the list of some of the addresses with more information such as names, title etc.
I am trying to compare the two files by check each line of the csv file. If the email in a line of the csv file matches any email in the email list the line is pushed to another array.
I came up with the following code:
function getCsvMatches(){
var holder = [];
var sep = "\n";
var match_holder = "";
var csv = document.getElementById("rawdata2").value;
var csv_array = csv.split('\n');
var email_list = document.getElementById("rawdata1").value;
var emaillist_array = email_list.split('\n');
var csv_length = csv_array.length;
var emaillist_length = email_list.length;
for (var i = 0; i < csv_length; i++){
var email_line = csv_array[i].match(/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)[0];
//alert(email_line) //this works as expected
if(emaillist_array.indexOf(email_line)>= 0){
holder.push(csv_array[i]);
}
}
for (var n = 0; n < holder.length; n++) {
matched_holder += holder[n];
matched_holder += sep;
}
document.extractor3.rawdata3.value = match_holder;
document.extractor3.match_count.value = holder.length;
document.extractor1.emaillist_count.value = emaillist_length;
document.extractor2.csv_count.value = csv_length;
}
Here jsbin link for more code and html: http://jsbin.com/fabumexucu/edit?output
The problem is if I run the script in Firefox I get TypeError: csv_array[i].match(...) is null. But, if alert function is uncommented, I get alert of each emails in the csv file.
Please tell me what I am doing wrong. Thanks.

extract javascript variable from a function

this is my first post. Hope I've observed all the rules properly.
I'm a JS beginner and I've been watching tutorials on thenewboston.com and w3schools and some others on Youtube but can't find the answer to my question.
I have a form that uses JS to dynamically add input rows and that works fine. However the last part I just can't get to work. It is the bit that is supposed to collate all the data entered by the user.
This is what I have so far:
//get all the row data
function getData(TechRiskTable){
try {
var table = document.getElementById(TechRiskTable);
var rowCount = table.rows.length;
var jsonArray = new Array();
for(var index=0; index < rowCount; index++) {
var mapObj = {};
var row = table.rows[index];
var name1 = row.cells[0].childNodes[0];
var name2 = row.cells[1].childNodes[0];
var name3 = row.cells[2].childNodes[0];
var name4 = row.cells[3].childNodes[0];
var name5 = row.cells[4].childNodes[0];
mapObj['name1'] = name1.value;
mapObj['name2'] = name2.value;
mapObj['name3'] = name3.value;
mapObj['name4'] = name4.value;
mapObj['name5'] = name5.value;
// document.write("Value in jsonArray " + name1.value + "<br />");
}
}catch(e) {
alert(e);
}
}
Ok, so I'm running this on a classic ASP page and the "onclick" does this:
response.write input type=submit onclick='getData(TechRiskTable);' value='Send to Reviewer'><input type=reset value='Start Again'>
My question is this: How can I extract the values the user entered into the added rows in the table "TechRiskTable" so I can insert them into a database. I don't need help with getting it into the dbase, I can do that myself. I'm just having trouble extracting the actual values. That "document.write" bit does actually display the correct values on the page when I have it uncommented, but that is still within the function. I can't find a way to access the entered data from OUTSIDE the function. I've tried using request.querystring but that doesn't return any data either.
I assume that I need to get them out of jsonArray() but I can't find anywhere I can get this to work.
Any clarification required please let me know. I didn't include all the code as this post would then be too long but if you need more just ask.
Cheers
function getData(TechRiskTable){
try {
var table = document.getElementById(TechRiskTable);
var rowCount = table.rows.length;
var jsonArray = new Array();
for(var index=0; index < rowCount; index++) {
var mapObj = {};
var row = table.rows[index];
var name1 = row.cells[0].childNodes[0];
var name2 = row.cells[1].childNodes[0];
var name3 = row.cells[2].childNodes[0];
var name4 = row.cells[3].childNodes[0];
var name5 = row.cells[4].childNodes[0];
mapObj['name1'] = name1.value;
mapObj['name2'] = name2.value;
mapObj['name3'] = name3.value;
mapObj['name4'] = name4.value;
mapObj['name5'] = name5.value;
// document.write("Value in jsonArray " + name1.value + "<br />");
return mapObj;
}
}catch(e) {
alert(e);
}
return null;
I am assuming you are calling this method from somewhere else,
var mapObj = getData(TechRiskTable);
if(mapObj!=null)
{
alert("name1 is "+mapObj.name1+" and name2 is "+mapObj.name2);
}

How to find the number of form elements that are getting passed to e.parameter in GAS?

In Google App Scripts (GAS), I want to be able to add and remove TextBox and TextArea elements to a FlexTable (that's being used as a form) and not worry about how many there are. I've named the text elements based on a counter to make this process easier.
So, is there a way to get the number of inputs (TextBox + TextArea) passed to e.parameter after the form is submitted?
Here's the relevant code from the FlexTable:
function doGet() {
var app = UiApp.createApplication();
var flex = app.createFlexTable().setId('myFlex');
var counter = 0;
var row_counter = 0;
...
var firstnameLabel = app.createLabel('Your FIRST Name');
var firstnameTextBox = app.createTextBox().setWidth(sm_width).setName('input' + counter).setText(data[counter]);
flex.setWidget(row_counter, 1, firstnameLabel);
flex.setWidget(row_counter, 2, firstnameTextBox);
row_counter++;
counter++;
var lastnameLabel = app.createLabel('Your LAST Name');
var lastnameTextBox = app.createTextBox().setWidth(sm_width).setName('input' + counter).setText(data[counter]);
flex.setWidget(row_counter, 1, lastnameLabel);
flex.setWidget(row_counter, 2, lastnameTextBox);
row_counter++;
counter++;
...
var submitButton = app.createButton('Submit Proposal');
flex.setWidget(row_counter, 2, submitButton);
var handler = app.createServerClickHandler('saveProposal');
handler.addCallbackElement(flex);
submitButton.addClickHandler(handler);
var scroll = app.createScrollPanel().setSize('100%', '100%');
scroll.add(flex);
app.add(scroll);
return app;
}
And here's the code for the ClickHandler (notice that I currently have 39 elements in my FlexTable):
function saveProposal(e){
var app = UiApp.getActiveApplication();
var userData = [];
var counter = 39;
for(var i = 0; i < counter; i++) {
var input_name = 'input' + i;
userData[i] = e.parameter[input_name];
}
So, is there a way to get the number of elements (in this case 39) without manually counting them and assigning this value to a variable?
I'm new at this stuff and I'd appreciate your help.
Cheers!
The simplest way is to add a hidden widget in your doGet() function that will hold the counter value like this :
var hidden = app.createHidden('counterValue',counter);// don't forget to add this widget as a callBackElement to your handler variable (handler.addCallBackElement(hidden))
then in the handler function simply use
var counter = Number(e.parameter.counterValue);// because the returned value is actually a string, as almost any other widget...
If you want to see this value while debugging you can replace it momentarily with a textBox...
You can search for arguments array based object.
function foo(x) {
console.log(arguments.length); // This will print 7.
}
foo(1,2,3,4,5,6,7) // Sending 7 parameters to function.
You could use a while loop.
var i = 0;
var userData = [];
while (e.parameter['input' + i] != undefined) {
userData[i] = e.parameter['input' + i];
i++;
};
OR:
var i = 0;
var userData = [];
var input_name = 'input0';
while (e.parameter[input_name] != undefined) {
userData[i] = e.parameter[input_name];
i++;
input_name = 'input' + i;
};

Categories