Split Data for certain condition In JavaScript Pentaho - javascript

I have a column in csv file named event_name and I am using Modified Java Script Value step to create new columns if the first character of event_name fufill conditions , for example :
event_name ="app_connection: connection_start_time = 10/05/2018 17:29:26: connection_end_time = 10/05/2018 17:29:29: connection_duration_in_seconds = 3"
==> event = app_connection
==> connection_start_time = 10/05/2018 17:29:26
==> connection_end_time = 10/05/2018 17:29:29
==> connection_duration_in_seconds = 3
if it's : event_name ="scene_access"
==> event = scene_access
I tried this code but it doesn't seem to change anything :
if(event_name.substr(0,3).equals("app"))
{
var event = event_name.substring(0,14);
var connection_start_time = event_name.substr(40,59);
var connection_end_time = event_name.substr(83,102);
var connection_duration_in_seconds = event_name.substr(137,139);
}
else
{
event_name = event_name;
}
If you could give me a hint or explaining what I'm missing will be a huge help.
Thank you.

I tried the field in the substring function, and in the substr function the sintaxis should be substr(string, position_from, number_of_characters).
With this change the code should be:
if(substr(event_name,0,3).equals("app"))
{
var event = substr(event_name,0,14);
var connection_start_time = substr(event_name,40,19);
var connection_end_time = substr(event_name,83,19);
var connection_duration_in_seconds = substr(event_name,137,1);
}
else
{
event_name = event_name;
}
After this press the button "Get variables" to make available the fields in the output of the step.

Related

Javascript count two variables goes wrong

I am busy with making a kind of invoice system, where the user can make invoices very easily. Now I am at the point where I have to count up, per product, three different variables/items, but instead of counting them up, my javascript code puts it like text (with the + operator).
Example:
selectmenu 1 = option 0 (where VAT = 8.50 euro's)
selectmenu 2 = option 1 (where VAT = 12.76 euro's)
Now the output has to be (8.50+12.76)= 21.26
The output in my situation is = 8.5012.76
My (partial) javascript code:
$("select#product").on("change", function (e) {
var $row = $(e.target).closest('.productitem');
var selVal = $row.find('#product').val();
var totalvat;
var currentVat = $('#totalvat').val();
var NLhoog = 1.21;
var price0EXC = 40.49;
var price0INC = (price0EXC * NLhoog).toFixed(2);
var price0VAT = (price0INC - price0EXC).toFixed(2);
var price1EXC = 60.74;
var price1INC = (price1EXC * NLhoog).toFixed(2);
var price1VAT = (price1INC - price1EXC).toFixed(2);
if (selVal === "0") {
$row.find("input#vat").val(price0VAT);
$row.find("input#priceEXC").val(price0EXC);
$row.find("input#priceINC").val(price0INC);
totalvat = (currentVat + price0VAT);
$('input#totalvat').val(totalvat);
} else if (selVal === "1") {
$row.find("input#vat").val(price1VAT);
$row.find("input#priceEXC").val(price1EXC);
$row.find("input#priceINC").val(price1INC);
totalvat = currentVat+price1VAT;
$('input#totalvat').val(totalvat);
}
});
I have let the unimportant part of the code away.
If you know what I am doing wrong, please let me know :)
Think this may help?
var currentVat = parseFloat($('#totalvat').val());
You are using var currentVat = $('#totalvat').val(); to get the value from an input I assume? This is a string which will need to be parsed at some to a relevant datatype. When + is used with a string the compiler performs concatenation.
Try something like:
var currentVat = parseFloat($('#totalvat').val());
Or do it later on with:
parseFloat(currentVat);
As you're using numbers as currency I'd consider adding the suffix .ToFixed(2) at the end, and maybe some other formatting

location.search.match in java script

Hi everyone I'm beginner CTF player and I have same lake with javascript and I face following problem
I couldn't understand what is the intention of following line
location.search.match(/e=(.*)/)[1]))
This is complete code
if(location.search) {
var div = document.currentScript.parentNode.appendChild(document.createElement('div'));
div.className = 'alert alert-danger';
div.role = 'alert';
div.appendChild(document.createTextNode(unescape(location.search.match(/e=(.*)/)[1])));
}
can u help me to understand it
location.search basically give you the query string part of the current URL and and match has been used to extract e parameter value from the query string with a regular expression.
As an example, if the current URL is like
https://www.example.com/?e=someone#example.com then location.search.match(/e=(.*)/)[1] will give you 'someone#example.com'.
Rest of the code basically create a div element and set extracted text as a child of it and finally append that div as a child of parent node of the currently running script tag.
Here is my working example:
This URL have multiple query parameters, such as: config=xxx & zoom=17 & lat=xxx etc...
I want to extract each query parameters one by one.
http://localhost:10/mapserver1/viewer/?config=viewer_simple1&mapserver_url=https://maps2.dcgis.dc.gov/dcgis/rest/services/Zoning/MapServer&zoom=17&lat=38.917292&long=-77.036420
This is how you do (working code):
var ___zoom = location.search.match(/zoom=([^&]*)/i)[1];
var ___lat = location.search.match(/lat=([^&]*)/i)[1];
var ___long = location.search.match(/long=([^&]*)/i)[1];
var ___basemap = location.search.match(/basemap=([^&]*)/i)[1];
var ___type = location.search.match(/type=([^&]*)/i)[1];
var ___url = location.search.match(/url=([^&]*)/i)[1];
var ___title = location.search.match(/title=([^&]*)/i)[1];
var ___opacity = location.search.match(/opacity=([^&]*)/i)[1];
//console.log(location.search.match(/zoom=([^&]*)/i)[0]); // 'zoom=17'
//console.log(location.search.match(/zoom=([^&]*)/i)[1]); // '17'
console.log(___zoom);
console.log(___lat);
console.log(___long);
console.log(___basemap);
console.log(___type);
console.log(___url);
console.log(___title);
console.log(___opacity);
This is my fully working code:
first check if parameter exist, if does, then extract it.
var ___zoom;
var ___lat;
var ___long;
var ___basemap;
var ___type;
var ___url;
var ___title;
var ___opacity;
/*
* if (value) {
*
* }
*
* will evaluate to true if value is not:
null
undefined
NaN
empty string ("")
false
0
*
*
*
*/
if ( location.search.match(/zoom=([^&]*)/i) )
{
___zoom = location.search.match(/zoom=([^&]*)/i)[1];
}
if ( location.search.match(/lat=([^&]*)/i) )
{
___lat = location.search.match(/lat=([^&]*)/i)[1];
}
if (location.search.match(/long=([^&]*)/i))
{
___long = location.search.match(/long=([^&]*)/i)[1];
}
if (location.search.match(/basemap=([^&]*)/i))
{
___basemap = location.search.match(/basemap=([^&]*)/i)[1];
}
if (location.search.match(/type=([^&]*)/i))
{
___type = location.search.match(/type=([^&]*)/i)[1];
}
if (location.search.match(/url=([^&]*)/i))
{
___url = location.search.match(/url=([^&]*)/i)[1];
}
if (location.search.match(/title=([^&]*)/i))
{
___title = location.search.match(/title=([^&]*)/i)[1];
}
if (location.search.match(/opacity=([^&]*)/i))
{
___opacity = location.search.match(/opacity=([^&]*)/i)[1];
}
//console.log(location.search.match(/zoom=([^&]*)/i)[0]); // 'zoom=17'
//console.log(location.search.match(/zoom=([^&]*)/i)[1]); // '17'
console.log(___zoom);
console.log(___lat);
console.log(___long);
console.log(___basemap);
console.log(___type);
console.log(___url);
console.log(___title);
console.log(___opacity);

Function to return a list - issue with filtering the output

I am trying to output only articles if authorsId = authorId.
Beside that the whole function works exactly as I want, here it is:
The general idea is to limit access to only own articles.
So, my question is: how do I limit the results to show only articles written by the owner of the page we are on (authorsId = authorId).
function ArticlesListReturn(returned) {
xml = returned.documentElement;
var rel = document.getElementById('related_category_articles');
rel.options.length = 0;
var status = getXMLData('status');
var title = '';
var id = '';
var authorid = '';
if (status == 0) {
alert("%%LNG_jsArticleListError%%" + errormsg);
} else {
var authorid = document.getElementById("authorid").value; // Serge
// authorsid = getNextXMLData('authors',x);
for (var x = 0; x < xml.getElementsByTagName('titles').length; x++) {
title = getNextXMLData('titles', x);
id = getNextXMLData('ids', x);
authorsid = getNextXMLData('authors', x);
alert(authorsid) // authors of each article - it returns the proper values
alert(authorid) // author of the page we are on - it returns the proper value
var count = 0;
rel.options[x] = new Option(title, id, authorid); // lign that returns results
title = '';
id = '';
authorid = '';
}
}
I suspect the problem is when you try performing a conditional statement (if/then/else) that you are comparing a number to a string (or a string to a number). This is like comparing if (1 == "1" ) for example (note the double quotes is only on one side because the left would be numeric, the right side of the equation would be a string).
I added a test which should force both values to be strings, then compares them. If it still gives you problems, make sure there are no spaces/tabs added to one variable, but missing in the other variable.
Also, I changed your "alert" to output to the console (CTRL+SHIFT+J if you are using firefox). The problem using alert is sometimes remote data is not available when needed but your alert button creates a pause while the data is being read. So... if you use alert, your code works, then you remove alert, your code could reveal new errors (since remote data was not served on time). It may not be an issue now, but could be an issue for you going forward.
Best of luck!
function ArticlesListReturn(returned) {
xml = returned.documentElement;
var rel = document.getElementById('related_category_articles');
rel.options.length = 0;
var status = getXMLData('status');
var title = '';
var id = '';
var authorid = '';
if (status == 0) {
alert("%%LNG_jsArticleListError%%" + errormsg);
} else {
var authorid = document.getElementById("authorid").value; // Serge
// authorsid = getNextXMLData('authors',x);
for (var x = 0; x < xml.getElementsByTagName('titles').length; x++) {
title = getNextXMLData('titles', x);
id = getNextXMLData('ids', x);
authorsid = getNextXMLData('authors', x);
console.log("authorsid = "+authorsid); // authors of each article - it returns the proper values
console.log("authorid = "+authorid); // author of the page we are on - it returns the proper value
if( authorsid.toString() == authorid.toString() )
{
rel.options
var count = 0;
console.log( authorsid.toString()+" equals "+authorid.toString() );
rel.options[rel.options.length] = new Option(title, id, authorid); // lign that returns results
}
else
{
console.log( authorsid.toString()+" NOT equals "+authorid.toString() );
}
title = '';
id = '';
authorid = '';
}
Did you check the console for messages? Did it correctly show authorid and authorsid?
I have edited the script and made a couple of additions...
The console will tell you if the conditional check worked or not (meaning you will get a message for each record). See the "if/else" and the extra "console.log" parts I added?
rel.options[x] changed to equal rel.options[rel.options.length]. I am curious on why you set rel.options.length=0 when I would instead have done rel.options=new Array();

how to get a number form value and turn it into string javascript

I want to turn a number I get from a form into a string and combine it with another string, but I receive this:
windows.location='multiSet?type=multi'Number
instead I want to get this:
windows.location='multiSet?type=multiNumber'
Note that the difference is the position of the ' mark is before Number and I want it after it..
All i see in other post is how to put a string as a number not viceversa.
var singleMultiContainer = document.getElementById("singleMultiContainer");
var singleMultiValue = singleMultiContainer.value;
var nextButton = document.getElementById("nextButton");
var multipleSetWindow = "window.location='multiSet.html?type=multi'";
var singleSetWindow = "window.location='SampleInfo.html?type=single'";
var containerCuantity = document.getElementById("containerCuantity");
function setContainers(){
singleMultiValue = singleMultiContainer.value;
containerCuantityValue = parseInt(containerCuantity.value);
if (singleMultiValue == "multi"){
//alert("Multi");
document.getElementById("nextButton").setAttribute("onclick", multipleSetWindow+containerCuantityValue);
} else if (singleMultiValue == "single") {
document.getElementById("nextButton").setAttribute("onclick", singleSetWindow);
}
}
singleMultiContainer.onchange = function(){
setContainers();
}
Don't use setAttribute to define event handlers. And you don't need to parse the value as you want to put it back in a string.
Change
document.getElementById("nextButton").setAttribute("onclick", multipleSetWindow+containerCuantityValue);
to
document.getElementById("nextButton").onclick = function(){
window.location='multiSet.html?type=multi'+containerCuantity.value;
}

Working with associate array in javascript

I making one application which contains 2 input box and one input button.My qestion is when user enter some input eg. "I used to going somewhere" then my result should be "I UD GNG somewhere." For that i am using this code http://pastebin.com/vHkhASdZ
Please,anyone have idea how to solve then pls reply me asap. I solved my issue in php but in case of javascript i don't have any idea. Here is my output link in php http://codepad.viper-7.com/SQvO6Y
I want my result in javascript.Pls anyone know then give reply.
Use this function
var replaceText = function () {
var inputval = document.getElementById('first_text').value;
var arr = {
"going": "GNG",
"used to": "UD",
"as soon as possible": "ASAP",
"to do fast": "tdf"
}
for(var key in arr) {
if (typeof (arr[key]) !== "undefined") inputval = inputval.replace(key, arr[key])
}
document.getElementById("second_text").value = inputval;
}
Do something like this. Define word replace as map.
var wordmap = {
going: 'GNG',
'used to': 'UD',
'as soon as possible': 'asap',
'to do fast': 'tdf',
..
}
and define function that iterates every word in the input string.
function replace( text ) {
var input = text.split(' ');
var outputval = []
for (var i in input) {
outputval.push( wordmap[input[i]] || input[i] )
}
return outputval.join(' ');
}
And use it like this
var output = replace( document.getElementById('first_text').value );

Categories

Resources