Dynamically get the values from DataSet using javascript - javascript

What I am trying to do is to print the values from dataset on the aspx page using JavaScript.
I got the filled DataSet as:
var Total_Record_count="<%=dsExistingTranData.Tables(0).Rows.count %>";
var DynRowCount=1;
for(DynRowCount = 1; DynRowCount <= Total_Record_count; DynRowCount++)
{
test = " <%=dsExistingTranData.Tables(0).Rows(0).Item("CUST_NAME")%>";
}
console.log(test);
My dsExistingTranData contains multipal data eg: CUST_NAME from the above code i am able to get the CUST_NAME at Row(0).
Is their any way to dynamically increment the value of Row() usning javascript variable so that I can able to get the other values. I tried
var Total_Record_count="<%=dsExistingTranData.Tables(0).Rows.count %>";
var DynRowCount=1;
for(DynRowCount = 1; DynRowCount <= Total_Record_count; DynRowCount++)
{
test = " <%=dsExistingTranData.Tables(0).Rows(%>"+ DynRowCount +"<%).Item("CUST_NAME")%>";
}
console.log(test);
is their any way to do this in such simple way..??

I think you need to use, if this (presumably) is the backend
"<%=dsExistingTranData.Tables(0).Rows(%><%="+DynRowCount+"%><%).Item('CUST_NAME')%>";

Related

Setting Values in Google Apps Script

I'm trying to set values using the following logic:
function compare(){
var values = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
for(n=0;n<values.length;++n){
var cell = values[n][0] ;
values[n][0].setValue("myValue");
}
}
But I can't set the value this way. I'm trying to use the cell[n][x] notation because its easier for what I have to do later on.
Thanks in advance for your help.
You can't use .setValues() because values[n][0] is not an object, it is just the value of a 2D-array. Just use values[n][0] = "my Value". At the end you will have editted your array, then you'll need to use range.setValues() to set the new values to your range.
Example:
function compare(){
var myRange = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange();
var values = myRange.getValues();
for(var n=0;n<values.length;n++){
values[n][0]="myValue "+n;
values[n][1]="myValue 2 "+n;
}
myRange.setValues(values)
}

how to make index dynamic for json objects

i'm tring to dynamically display eveyrthing that comes back from the server, however to get it displaying i have to explicitly write in the index of the object i'm trying to access, how can i make this dynamic so it goes through them all?
i am Using AngularJS, Express, and Mongoose.
here is the code snippet where the index i want to change on its own located.
app.controller('MainController', ['$scope', 'whipmeet', function($scope, whipmeet) {
whipmeet.getWhipmeets(function(JSON){
$scope.meetinfo = JSON;
$scope.meetparts {
name = JSON.data["0"].name,
location = JSON.data["0"].location,
car = JSON.data["0"].car,
date = JSON.data["0"].date,
time = JSON.data["0"].time,
type = JSON.data["0"].type,
stock = JSON.data["0"].stock
};
what i'm trying to achieve is that the ["0"] index change dynamically to get all the indexes of the objects so i can get those zeroes to go 1 to 2 to 3 and so on untill there are no more
There are few options.
From my understanding, you are trying to iterate through the JSON object that is returned to you.
You can either use:
var meetpartList = [];
JSON.foreach(function(d) {
// do your parsing here
newObject = {};
newObject.name = d.name;
// etc..
meetpartList.append(newObject);
});
or you can use:
for (int i = 0; i < numberOfIndicesInJson; i++) {
// get values using JSON[i + ""].key;
}
to do something similar as above to create a list of meets.
Then you can use that list of meets to render into your view using ng-repeat, which I hope is the solution you're looking for?

How to get the data inside the XML

I have this response data from the server that I authenticate.
<session session-guid="D39C1215-2F31-8641-E5T2-C7G35RT69127" user-id="34"> </session>
How can I get the value of session-guid and user-id and store them into 1 variable for each.
Thank you.
In plain Javascript in a modern browser you can use document.querySelectorAll().
For example, assuming you only have one session tag:
var session = document.querySelectorAll("session")[0];
var session_guid = session.getAttribute("session-guid");
var user_id = session.getAttribute("user-id");
console.log("session-guid: " + session_guid);
console.log("user-id: " + user_id);
If you have more that one session tag you can use forEach on the results of querySelectorAll() to locate the one you want. If you know that you're only going to have one session element you can use document.querySelector() instead of document.querySelectorAll()[0].
Here is one way to get the values client side. For this method you will need to add any custom class to your element like below:
Now write below lines of code inside tag:
var $mySession = jQuery(document.getElementsByClassName("mySession"));
for (i = 0; i < $mySession.length; i++) {
var sessionguid = jQuery($mySession[i]).attr('session-guid');
var userid = jQuery($mySession[i]).attr('user-id');
console.log(sessionguid);
console.log(userid);
}
You can check the values of "sessionguid" and "userid" variable in your browser console.
Here is what you can do to get the required data from the XML.
function getXMLData(xml) {
var txt, xmlDoc;
//get the responseXML
xmlDoc = xml.responseXML;
txt = "";
//get all the session nodes
var sessions = xmlDoc.getElementsByTagName("session");//this returns a array of nodes
for(var i =0;i<sessions.length;i++){
//iterate over nodes and get the attributes
txt += sessions[i].getAttribute("session-guid")+"<br />";
}
document.getElementById("demo").innerHTML = txt;
}
this function accepts the response as a parameter. And then extracts out the sessions nodes and the required attribute. You can modify it according to your requirements.
Here is a PLNKR demo for the same

storing forms and their elements values in a variable

I have a single page web app.
For speed, I store each 'page' in the JS.
I have a problem which happens when there is a form on a page. If you fill in the form, and then store it in a js variable, and then retrieve it, the forms values have disappeared?
I use functions like:
var pages_html = {};
var $page = $('#some-page');
store_page($page);
$page.remove();
//some stuff on another page
var $retrieved_page = get_page('some-page');
console.log($retrieved_page.find('#some-input').val())
//consoles log is always blank / ''
function store_page(page){
var page_id = $(page).attr('id');
pages_html[page_id] = $(page);
}
function get_page(page_id){
var page = pages_html[page_id];
return $(page);
}
Everything else seems to work, i can store and retrieve pages as i wish, its just any values of form elements are lost. How can I work around this?
You cannot store it like that. Instead store it as serialized array. which you can then fill it back when needed. serializeArray returns Array of Objects which have name and value
var values = {};
function store_page(page){
var page_id = $(page).attr('id');
pages_html[page_id] = $(page);
values[page_id] = $(page).find("form").serializeArray(); // serialize it
}
function get_page(page_id){
var page = pages_html[page_id];
values[page_id].forEach(function(obj){
page.find('[name=' + obj.name + ']').val(obj.value) // add it again
});
return page; // and then return
}

Retrieve Perl Hash Values using Javascript functions

I want to change the values of a select box based on the value in other in a Web Page.
When the page is loaded I store the keys and values in a hash. So when I change the value in field1 it calls javascript function and the values in field2 should change as per the hash.
Hash:
%hash = ('factory1','model1','factory2','model2','factory3','model3');
jQuery Function:
\$('#factory').change(function(){
var factoryVal = \$(this).val();
\$('#model').val()="$hash{"+factoryVal+"}" ;
});
How can I do it?
use JSON;
my %hash = ('factory1','model1','factory2','model2','factory3','model3');
my $json_str = encode_json \%hash;
print qq{
var hash = $json_str;
}, q{
$('#factory').change(function(){
var factoryVal = $(this).val();
$('#model').val( hash[factoryVal] );
});
};

Categories