WebDB - For each Column, in Each Row - javascript

in webDB, EG: HTML5 SQLLite
How can I do the following:
For i = 0 To RS.Fields.Count -1
Response.Write "Field Name: " & RS.Fields(i).Name & "<br>"
Response.Write "Field Value: " & RS(i) & "<br>"
Next
If, at all...
Or, another question would be, how can I iterate the columns themselves and optimally, retrieve the columns name.

I found the answer
function showQueryResults(tx, r) {
// alert("showing [" + r.rows.length.toString() + "] query results");
var rs = null;
var $tr, $td;
var $t = $("<table border='1' cellpadding='2 cellspacing='0' />");
if (r.rows.length > 0) {
rs = r.rows.item(0);
$tr = $("<tr />");
$.each(rs, function (key, val) {
$tr.append($("<th>" + key + "</th>"));
});
$t.append($tr);
for (var i = 0; i < r.rows.length; i++) {
rs = r.rows.item(i);
$tr = $("<tr />");
$.each(rs, function (key, val) {
$tr.append($("<td>" + rs[key] + "</td>"));
});
$t.append($tr);
$tr = null;
};
};
$("#formHolder").children().remove();
$("#formHolder").append($t).show();
$t = null;
};

Related

Javascript chinese variable being passed to bash script as question mark

I have this javascript function here, the routeno variable will pass a Chinese value
routeno: 粤ZX123港
function getRouteLegNoList()
{
var data = "host="+lphost
+ "&user="+user
+ "&gateway="+gateway
+ "&routedate="+routedate.value
+ "&routeno="+routeno.value
+ "&action=GETROUTELEGNOLIST";
alert(lphost+'|'+user+'|'+gateway+'|'+routedate.value+'|'+routeno.value);
callAjax("../restartAndMoveDeclaration.cgi?"+data, "GET", "",
function(text) {
alert(text);
var html = "<select id=routelegno>";
var count = 0;
var lines = text.split("\n");
for(var i = 0; i < lines.length; i ++)
{
var line = lines[i];
var c;
if(line.indexOf("Data|") == 0) {
count += 1;
c = line.split("|");
html += "<option value=\"" + c[1]
+ "\"";
if(c[1]==origRouteLegNo) {
html += " selected ";
}
html += ">" + c[1] + "</option>";
}
}
html += "</select>";
dvroutelegno.innerHTML = html;
if(count==0){
alert('No Route Legs were found for [' +
gateway + '] [' +
routedate.value + '] [' +
routeno.value + ']');
} else {
if(count==1) {
trroutelegno.style.visibility = 'hidden';
} else {
// More than one, display the select box
trroutelegno.style.visibility = 'visible';
}
}
}
);
}
but when shell script captured the variable it does not read the Chinese character and replaced it as question mark
routeno: ?ZX123?
I've already tried to set different NLS_LANG but encountered the same output
NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
NLS_LANG=AMERICAN_AMERICA.AL32UTF8

Get value from multiple input boxes that have incremental id's

I am trying to get the value from the select element with jquery, but the console.log is showing that the value is NaN. I think it may have to do with the way I am selecting the ID's, but I am not sure.
//Here I am creating the select element with the options in a loop and giving them an incremental ID.
var div = $('<div class="containter">');
$('.rForm').append(div);
for(var x = 0; x < 10; x++)
{
var h3 = $('<h3 style="margin-bottom: 5px;">' + "Question" + " " + (x + 1) + '</h3>');
var label = $('<label>').attr('id', 'q'+ (x + 1)).text(questions[x]);
var br = $('<br>');
div.append(label);
div.append(br);
var select = $('<select>').attr('id', 'q'+ (x + 1));
for(var i = 0; i < 10; i++)
{
if(i === 0){
select.append($('<option>').text(options[i]));
}
else{
select.append($('<option>').attr('value', options[i].val).text(options[i].text));
}
console.log('hi')
}
$('.containter').append(select);
}
$('#submit').on('click', function(){
// var answers = [];
var newProfile = {
name: $('#name').val().trim(),
photo: $('#link').val().trim(),
scores: []
}
for(var x = 0; x < 10; x++){
var num = $('#q' + (x+1)).val(); //Showing the value as NaN
console.log("num", num)
newProfile.scores.push(parseInt(num));
}
console.log('new', newProfile);
console.log('scores' + newProfile.scores);
});
Rather do this :
var questions = [{
title: "Do you like jQuery ?",
expected: [0],
answers: [
"yes",
"no"
]
}, {
title: "jQuery is :",
expected: [1],
answers: [
"a language",
"a library",
"a framework"
]
}, {
title: "What is the type of jQuery.map() ?",
expected: [2],
answers: [
"[a] -> b",
"[a] -> [b]",
"[a] -> (a -> Int -> b) -> [b]"
]
}];
$("form").html($.map(questions, function (q, i) {
return ""
+ "<div>"
+ "<h3>" + q.title + "</h3>"
+ "<select "
+ "multiple "
+ "name=\"q" + i + "\""
+ "size=\"" + q.answers.length + "\""
+ ">"
+ $.map(q.answers, function (a, j) {
return "<option value=\"" + j + "\">" + a + "</option>";
}).join("")
+ "</select>"
+ "</div>";
}).join("") + (
"<button type=\"button\">Submit</button>"
));
$("button").click(function () {
var score = 0;
var $sel = $("select");
$sel.each(function (i) {
var answers = $(this).val() || [];
var expected = questions[i].expected;
if (answers.join() == expected.join()) score++;
});
alert(score + "/" + questions.length);
});
body, h3, select, input {font: normal 12px Arial;}
select {overflow: hidden;}
div {margin-bottom: 1em;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form></form>

Converting Table data to the xml structured data

I stuck the below requirement, Here I have to show all the table data to the xml structured data, I tried with the following, I'm getting xml structure but struggling to show row data with table headers like below
<rowset>
<row name='SequnceNumber'>
<row>Item</row>
<row>Weight</row>
<row>LableNo</row>
<row>Unitcost</row>
<row>Delete</row>
</rowset>
<rowset>
<rowset name='1212121'>
<Item>test1</Item>
<Weight>3000</Weight>
<LableNo>test1</LableNo>
<Unitcost>test1</Unitcost>
<Delete>test1</Delete>
</rowset>
I tried with below code,
$(document).ready(function(){
$("#idXmlData").click(function(){
var xmlStart = "<?xml version=\"1.0\" encoding=\"UTF-8\">";
var xml = xmlStart;
$(".mutliple tr").each(function() {
var cells = $("td", this);
if (cells.length > 0) {
xml +="<rowset name='" + cells.eq(0).text() + "'>\n";
for (var i = 1; i < cells.length; ++i) {
//var tableHeaders=["SequnceNumber","Item","Weight","LableNo","Unitcost","Delete"];
//for(var j=0;j<tableHeaders.length;j++){
// xml += "\t<"+j+">" + cells.eq(i).text() + "</"+j+">\n";
xml += "\t<row>" + cells.eq(i).text() + "</row>\n";
//}
}
xml += "</rowset>\n";
}
});
window.alert(xml);
});
});
JS fiddle
It will be a bit easier if you format your table to include header with th as you can see in the following code, because separating header text from other rows is going to make things a bit simpler.
Here's JS for that :
$("#idXmlData").click(function(){
var xmlStart = "<?xml version=\"1.0\" encoding=\"UTF-8\">";
var xml = xmlStart;
var xmlFirstRowSet = "<rowset><row name='";
var thArray = new Array();
$('th').each( function(index) {
thArray[index] = $(this).html();
});
xmlFirstRowSet = xmlFirstRowSet + thArray[0] + "'>";
var headerRowSet='';
for(var i=1; i< thArray.length; i++) {
headerRowSet = headerRowSet + "<row>" + thArray[i] + "</row>";
}
headerRowSet = headerRowSet + "</rowset>";
xmlFirstRowSet = xmlFirstRowSet + headerRowSet;
var bodyRowSet ='';
$(".mutliple tbody tr").each(function() {
bodyRowSet = bodyRowSet + "<rowset name='" + $(this).find('td').html() + "'>";
$(this).find('td:not(:first-child)').each(function() {
$td = $(this);
var tdValue = $(this).html();
var $th = $td.closest('table').find('th').eq($td.index()).html();
bodyRowSet = bodyRowSet + "<" + $th + ">" + tdValue + "</" + $th + ">";
});
bodyRowSet = bodyRowSet + "</rowset>";
});
xml = xmlStart + xmlFirstRowSet + bodyRowSet;
console.log(xml);
window.alert(xml);
});
Here's a working Fiddle

javascript inline code link generating chrome extension

I'm trying to make an extension for chrome that grabs data from a website and I'm having trouble making the links clickable. I CAN NOT use javascript inside the link (ex: href="javascript:myfunction(param);")
I need to create a div for each title, then create a onclick function that handles the div's innerhtml, and I can't get it to work.
here is my code so far:
document.addEventListener('DOMContentLoaded', function () {
$().ready(function () {
var url = 'http://somewebsite';
$.get(url, function (data) {
data = data.split("<tr name=\"hover\">");
var name;
var link;
var count = data.length;
count++;
for(var i = 1; i < data.length; i++){
data[i] = data[i].replace("<br>","<br />");
data[i] = data[i].replace("class=\"thread_link\"", "");
data[i] = data[i].replace("<td class=\"forum_thread_post\" align=\"center\">0</td>","");
data[i] = data[i].replace("<td class=\"forum_thread_post\">","");
data[i] = data[i].replace("</td>","");
data[i] = data[i].replace('<td class="forum_thread_post" align="center">0</td>','');
if(i != data.length-1){
data[i] = data[i].replace("<a href=\"", "");
data[i] = data[i].replace("</a>", "");
data[i] = data[i].split("\" >");
data[i][1] = data[i][1].split("<");
document.write('<div id="' + data[i][1][0] + '">' + data[i][1][0] + data[i][0] + "</div><br /><br />");
}else{
data[i] = data[i].split("</table>")[0];
data[i] = data[i].replace("<a href=\"", "");
data[i] = data[i].replace("</a>", "");
data[i] = data[i].split("\" >");
data[i][1] = data[i][1].split("<");
document.write('<div id="' + data[i][1][0] + '">' + data[i][1][0] + data[i][0] + "</div>");
}
}
//document.body.innerHTML = '';
//console.log(data);
});
});
});
document.write('</script>');
function getshow(url){
alert(url);
document.body.innerHTML = '';
$.get("http://somewebsite" + url, function (dat) {
document.write(dat);
});
}

get text from attribute and format it

I have a div elements with data-seat and data-row property:
<div class='selected' data-seat='1' data-row='1'></div>
<div class='selected' data-seat='2' data-row='1'></div>
<div class='selected' data-seat='3' data-row='1'></div>
<div class='selected' data-seat='1' data-row='2'></div>
<div class='selected' data-seat='2' data-row='2'></div>
I want print friendly message for selected seats:
var selectedPlaceTextFormated ='';
$(".selected").each(function () {
var selectedPlace = $(this);
selectedPlaceTextFormated += "Row " + selectedPlace.attr("data-row") + " (seat " + selectedPlace.attr("data-seat") + ")\n";
});
alert(selectedPlaceTextFormated);
This code works well and shows the following:
Row 1 (seat 1)
Row 1 (seat 2)
Row 1 (seat 3)
Row 2 (seat 1)
Row 2 (seat 2)
But, I want group seats by row, i.e I want the following:
Row 1(seats: 1,2,3)
Row 2(seats: 1,2)
also, order by row number. How can I do this?
Thanks. DEMO
Here is the code
var selectedPlaceTextFormated ='';
var row_array = [];
$(".selected").each(function () {
var selectedPlace = $(this);
if (!row_array[selectedPlace.attr("data-row")]){
row_array[selectedPlace.attr("data-row")] = selectedPlace.attr("data-seat");
}
else row_array[selectedPlace.attr("data-row")] += ','+selectedPlace.attr("data-seat");
});
for (row in row_array){
alert("Row "+ row +"(seat " + row_array[row] + ")\n" );
}
And here the link to the working fiddle: http://jsfiddle.net/3gVHg/
First of all, jQuery is kind enough to automatically grab data- attributes into its data expando object, that means, you can access those data via:
jQueryObject.data('seat');
for instance.
Your actual question could get solved like
var $selected = $('.selected'),
availableRows = [ ],
selectedPlaceTextFormated = '',
currentRow,
currentSeats;
$selected.each(function(_, node) {
if( availableRows.indexOf( currentRow = $(node).data('row') ) === -1 ) {
availableRows.push( currentRow );
}
});
availableRows.forEach(function( row ) {
selectedPlaceTextFormated += 'Row ' + row + '(';
currentSeats = $selected.filter('[data-row=' + row + ']').map(function(_, node) {
return $(this).data('seat');
}).get();
selectedPlaceTextFormated += currentSeats.join(',') + ')\n';
});
jsFiddle: http://jsfiddle.net/gJFJW/3/
You need to use another variable to store the row, and format accordingly.
var selectedPlaceTextFormated ='';
var prevRow = 0;
$(".selected").each(function () {
var selectedPlace = $(this);
var row = selectedPlace.attr("data-row");
var seat = selectedPlace.attr("data-seat");
if(prevRow == row){
selectedPlaceTextFormated += "," + seat;
}
else{
if(selectedPlaceTextFormated != ''){
selectedPlaceTextFormated += ')\n';
}
selectedPlaceTextFormated += "Row " + row + " (seat " + seat;
prevRow = row;
}
});
selectedPlaceTextFormated += ')\n';
alert(selectedPlaceTextFormated);
Check http://jsfiddle.net/nsjithin/R8HHC/
This can be achieved with a few slight modifications to your existing code to use arrays; these arrays are then used to build a string:
var selectedPlaceTextFormated = [];
var textFormatted = '';
$(".selected").each(function(i) {
var selectedPlace = $(this);
var arr = [];
selectedPlaceTextFormated[selectedPlace.attr("data-row")] += "," + selectedPlace.attr("data-seat");
});
selectedPlaceTextFormated.shift();
for (var i = 0; i < selectedPlaceTextFormated.length; i++) {
var arr2 = selectedPlaceTextFormated[i].split(",");
arr2.shift();
textFormatted += "Row " + (i + 1) + " seats: (" + arr2.join(",") + ")\n";
}
alert(textFormatted);
​
Demo
I'd just do this:
var text = [];
$(".selected").each(function () {
var a = parseInt($(this).data('row'), 10),
b = $(this).data('seat');
text[a] = ((text[a])?text[a]+', ':'')+b;
});
var selectedPlaceTextFormated ='';
$.each(text, function(index, elem) {
if (!this.Window) selectedPlaceTextFormated += "Row " + index + " (seat " + elem + ")\n";
});
alert(selectedPlaceTextFormated);
FIDDLE

Categories