Populating drop down menu from comma delimited text file - html

I'm trying to populate a HTML select list from a text file located on the server. The file is setup like this:
ttt1111,John Doe
xxx2222,Jane Doe
etc....
The first column would be the <option value=""> and the second would be the displayed text. I read in the file and then split it into an array by each line. I'm having trouble trying to figure out the code to create the correct append line using the two values.
I'm extremely new to this so any help at all is appreciated, even just links to examples. This is my code so far, but it just assigns the entire line to the value and the text output.
function PopulateSupervisorList() {
var Suplist=[];
var SupervisorFile="text.txt";
var DDL = $("#iSupervisor");
var SuperID=[];
$.get(SupervisorFile,function(data) {
Suplist = data.responseText.split("\n");
for (var i=0; i < Suplist.length; i++) {
DDL.append("<option value='" + SuperID[i] + "'>" + Suplist[i] + "</option>")
}
});
}

You have to split each line in columns
try this
function PopulateSupervisorList() {
var SupervisorFile="text.txt";
var DDL = $("#iSupervisor");
$.get(SupervisorFile,function(data) {
var suplist = data.responseText.split("\n"),
cols;
for (var i=0, len=suplist.length; i<len; i++) {
cols = suplist[i].split(','); //split the line in columns
//so cols[0] -> ttt1111
//and cols[1] -> John Doe
//and so on for the rest lines
DDL.append("<option value='" + cols[0] + "'>" + cols[1] + "</option>");
}
});
}

Related

No matter what selection, each selection produces the same value data as the global variable

I'm trying to execute this and be able to get a different value for "new_id", the data is correct when getting this API call and there are 7 different id's. However, no matter what I select produces 7 as the new_id value. Please help, sorry for my noob question in advance!
I've tried making data[0] instead of data[i] but I really don't know where to start
var new_id = "";
$(document).ready(function () {
$.ajax({
"url":api_base+"/endpoint",
"type":"GET",
"contentType":"application/json",
"success":function(data){
var s = '<option value="-1">Please Select</option>';
for (var i = 0; i < data.length; i++) {
s += '<option value="' + data[i].check_id + '">' + data[i].check + '</option>';
new_id = data[i].check_id
}
$("#check_list").html(s);
}
});
I'd like to get a different result each time I select a different value.
#Dan Winnick - So where ever the select tag is, add these attributes - id="mySelect" onchange="dropDownChangeEvent()" and in js file add - function dropDownChangeEvent() { new_id = document.getElementById("mySelect").value; }

google sheet generated html form not "lining up"

How do I get dynamic content from multiple google sheets (within one google sheet) into the same html form?
In the code below, I dynamically create the html form from a main sheet. I want to add a form select, from a supplemental sheet. I cannot get the form.select to be part of one html form.
EDIT: (1) updated var html to include div's for each row. (2) moved form and table html into the html page directly. Form values are still not lining up correctly. Should I be using HTML Template instead?
<head>
<script type="text/javascript">
function setPageValues () {
//this is working as intended
google.script.run.withSuccessHandler(displayForm).getValuesFromSS();
}
function displayForm (values) {
var colsUsed = [4,5,0,13,15,16,17,18]; //sheet cols for form
var html = "<form><table border=0 width=90%>"; //start the form
//HERE IS THE ISSUE: gets range no problem, but html form is wrong.
//setting return value "html +=" doesn't work!
google.script.run.withSuccessHandler(displayPick).getValuesForRngName('CategoryRole');
//make the rest of the table (this all works fine)
for (var j = 0; j < colsUsed.length; j++){
colUsed = colsUsed[j]
html += "<div id=row><tr><th align=right>" + values[0][colUsed] + ":* </th>";
html += "<td><input></input type=text></td></tr></div>";
}
//close out the table and set the value (this all works fine)
html += "<tr><td></td><td><input type=submit></td></tr></form></table>";
html += "<i>* required form data.</i>";
document.getElementById("forminfo").innerHTML = html;
}
//building the pick list works just fine. results not correct in html
//tried to "return html;" but this didn't work!
function displayPick (values) {
var rows = 10000;
var catUsed = "34-31 21: Supplier";
//find limit of column values
for (var i = 0; i < rows; i++) {
if (values[i] == '') {
var rows = i;
break;
}
}
//build the select field description, and set selected option
html = "<div id=row><tr><th align=right>" + values[0] + ":* </th><td><select>";
for (var i = 1; i < rows; i++) {
if (values[i] == catUsed) {
html += "<option selected>" + values[i] + "<option>";
} else {
html += "<option>" + values[i] + "<option>";
}
}
html += "</select></td></tr></div>";
document.getElementById("formpick").innerHTML = html;
}
</script>
</head>
<body onload="setPageValues()">
<form><table border=0 width=90%>
<div id="formpick"></div>
<div id="forminfo"></div>
</table></form>
</body>
Put a function in your project that opens up other Spreadsheets with "openById"; Then return the data to your form with google.script.run.withSuccessHandler(functionName);

Google html creating a list from spreadsheet

I am trying to get a list to populate from a spreadsheet based on the question posted here Previous questioe posted by #Kron011and I'm struggling somewhat. I added these lines to my .gs file:
function getMenuListOne(){
return SpreadsheetApp.openbyId('spreadsheet_key').getSheetByName('sheet1')
.getRange(row, column, numRows, numColumns).getValues();
}
and I added these lines to my HTML file:
<select id="menu">
<option></option>
<option>Google Chrome</option>
<option>Firefox</option>
</select>
<script
src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
// The code in this function runs when the page is loaded.
$(function() {
google.script.run.withSuccessHandler(showThings)
.getMenuListFromSheet();
google.script.run.withSuccessHandler(showMenu)
.getMenuListFromSheet();
});
function showThings(things) {
var list = $('#things');
list.empty();
for (var i = 0; i < things.length; i++) {
list.append('<li>' + things[i] + '</li>');
}
}
function showMenu(menuItems) {
var list = $('#menu');
list.find('option').remove(); // remove existing contents
for (var i = 0; i < menuItems.length; i++) {
list.append('<option>' + menuItems[i] + '</option>');
}
}
</script>
As ever my painfully limited experience is hampering my efforts. I can get a new menu box to appear and show the correct results I want but I cannot get an existing box to show the same list. The existing boxes code is currently:
<input type="text" name="site" list="depotslist" id="site" class="form-control" placeholder="Select depot/site" required>
<datalist id="depotslist">
<option value="one">
<option value="two">
</datalist>
but can someone please point me in the right direction of which parts of my existing menu box I need to change to get the two bits to communicate?
UPDATE 23 JULY
I added the following code to get the another list to operate from another source:
$(function() {
google.script.run.withSuccessHandler(showThings2)
.getMenuListSources();
google.script.run.withSuccessHandler(showMenu2)
.getMenuListSources();
});
function showThings2(things2) {
var list2 = $('#things2');
list.empty();
for (var i = 0; i < things2.length; i++) {
list2.append('<li>' + things2[i] + '</li>');
}
}
function showMenu2(menuItems2) {
var list2 = $('#menu2');
var box2 = $('#sourcelist');
list2.find('option').remove(); // remove existing contents
for (var i = 0; i < menuItems2.length; i++) {
list2.append('<option>' + menuItems2[i] + '</option>');
box2.append('<option>' + menuItems2[i] + '</option>');
}
}
with these lines in the .gs file:
var Bvals = SpreadsheetApp.openById(ssKey).getSheetByName('SourceCodes').getRange("C3:C").getValues();
var Blast = Avals.filter(String).length;
return SpreadsheetApp.openById(ssKey).getSheetByName('SourceCodes').getRange(3,3,Blast,1).getValues();
It would be similar to what you are doing with the element "menu". with jquery you can select the element of the box that contains the options and then append the new values. in this case it's id is: depotslist.
function showMenu(menuItems) {
var list = $('#menu');
var box = $('#depotslist');
list.find('option').remove(); // remove existing contents
for (var i = 0; i < menuItems.length; i++) {
list.append('<option>' + menuItems[i] + '</option>');
box.append('<option>' + menuItems[i] + '</option>');
}
As a difference with the element "menu" in this case the content will remain. This means that in the box you will see the options "one, two" and whatever you added because we are not removing the content like with this line
list.find('option').remove();
I'm not sure if this is exactly what you wanted to do, let me know if this doesn't help.

getting limited rows at each page when clicking next button

This is code from phone gap index HTML page.
function querySuccess(tx, results){
var len = results.rows.length;
var output = '';
for (var i=0; i<len; i++)
{
output = output + '<li id="' + results.rows.item(i).id + '">' + results.rows.item(i).list_action + '</li>';
}
messageElement.html('<p>There are ' + len + ' items in your list:</p>');
listElement.html('<ul>' + output + '</ul>');
}
After populating this database I need that how to get limited rows at each page, for ex 5 rows after clicking to next button. But the button is not present there. Then how to create it. Also how to pass the current ID to another HTML page.

How to serialize HTML DOM to XML in IE 8?

Is there a way to do it(serialization of HTML DOM into XML) in IE 8 or any other older version of IE.
In firefox :
var xmlString = new XMLSerializer().serializeToString( doc );
does it.I haven't tried it, though.
XMLSerializer causes error in IE 8, that it is not defined.
var objSerializeDOM = {
//Variable to hold generated XML.
msg : "",
serializeDOM : function() {
dv = document.createElement('div'); // create dynamically div tag
dv.setAttribute('id', "lyr1"); // give id to it
dv.className = "top"; // set the style classname
// set the inner styling of the div tag
dv.style.position = "absolute";
// set the html content inside the div tag
dv.innerHTML = "<input type='button' value='Serialize' onClick='objSerializeDOM.createXML()'/>"
"<br>";
// finally add the div id to ur form
document.body.insertBefore(dv, document.body.firstChild);
},
/**
* XML creation takes place here.
*/
createXML : function() {
objSerializeDOM.msg += "";
objSerializeDOM.msg += "<?xml version='1.0' encoding='UTF-8'?>\n\n";
// Get all the forms in a document.
var forms = document.forms;
for ( var i = 0; i < forms.length; i++) {
// Get all the elements on per form basis.
elements = document.forms[i].elements;
objSerializeDOM.msg += "<FORM name=\"" + forms[i].name + "\" method=\""
+ forms[i].method + "\" action=\"" + forms[i].action + "\">\n\n";
for ( var j = 0; j < elements.length; j++) {
objSerializeDOM.msg += " <" + elements[j].tagName + " type=\""
+ elements[j].type + "\"" + " name=\""
+ elements[j].name + "\"" + " Value =\""
+ elements[j].value + "\" />\n";
}
alert(document.forms[i].elements[1].event);
}
objSerializeDOM.msg += "\n\n</FORM>\n\n";
alert(objSerializeDOM.msg);
objSerializeDOM.writeToFile(objSerializeDOM.msg);
},
/**
* Writes the msg to file at pre-specified location.
* #param msg
* the XML file created.
*/
writeToFile : function(msg) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.CreateTextFile("c:\\myXML.xml", true);
fh.WriteLine(msg);
fh.Close();
}
};
objSerializeDOM.serializeDOM();
I wrote this JS, I run this javascript using GreaseMonkey4IE. This simply puts a button on every page of the domain you specify in GM4IE. On click of that button it will parse the HTML document and create an XML file. It will also display the same as an alert first and will save the XML in your local drive on path specified.
There a still many improvements I am planning to do, but yes it works and may be give you guys an idea.The program is self-explanatory, I hope.
please have a look here How to get Events associated with DOM elements?Thanks