Passing data from JSP to jQuery and plotting in Highcharts - javascript

I am reading a text file which contains data to plot a graph in JSP. I am using hidden method and passing the value to the jQuery.
I am able to retrieve the value in jQuery through an alert but when I try to split the value using .split(), the values don't split based on the delimiter specified.
<%
//response.setIntHeader("Refresh", 10);
String jspPath = "C:/Users/Desktop/Out.txt";
BufferedReader reader = new BufferedReader(new FileReader(jspPath));
//BufferedReader br = new InputStreamReader(new FileInputStream(txtFilePath));
StringBuilder sb = new StringBuilder();
String line;
String lastline = "";
while((line = reader.readLine())!= null){
lastline = line;
}
String column3[] = lastline.split("\\*");
%>
<input type="hidden" value="<%=column3[2]%>" id = "filevalue">
jQuery part:
var a = $('#filevalue').text();
var lines = a.split('\n');
alert(lines);
I am referring to this fiddle Link to create the pie chart. Here instead of the hard coded data I am trying to pass the data from the JSP. Since my data is not getting split, i m unable to proceed further.
Please help me to split the data in order to provide it as input to the pie chart.

Two things - try using val() instead of text() to get the value from the hidden field, and you need to quote the "\" in the split command (with an extra "\"):
var a = $('#filevalue').val();
var lines = a.split('\\n');
alert(lines);

Related

How to render backslash without new lines in javascript

I have a string in javascript like var Value = "\\\\192.168.0.1\\\\Dir\\NewDir\\Imp\\file.xml",
but the output is (with new lines):
\\\\192.168.0.1
\\\\Dir
\\NewDir
\\Imp
\\file.xml
form.setValues({
'idInput' : Value
});
form - is a form created in extJs.
idInput - is the id of display field
Value - is the new value on a displayfield
How to keep the backslashes without the new lines? I tried with JSON stringify, but it doesn't work.
I'm thinking that - you want to render value with backslases as it is:
var Value = '\\\\192.168.0.1\\\\Dir\\NewDir\\Imp\\file.xml';
Value = encodeURI(Value);
Value = decodeURI(Value.replace(/%5C/g, "%5C%5C"));
Take a look on example : https://fiddle.sencha.com/#view/editor&fiddle/301t

Storing multi line html encoded string into sql

I want to store html code into sql database which I've encoded by using encodeURI() but it showing me multiple errors as below
I'm using dataType as <CLOB> also tried using NVARCHAR(MAX) but is showing same error.
Sharing my encoded html code below in string format tobe store into sql.
%3Cp%3Ethis%20kind%20of%20text%20i'm%20storing%20into%20database%3C/p%3E%3Cpre%20class=%22code-pre%22%3Evar uri%20= %22my%20test.asp?name=st%C3%A5le&car=saab%22;%0Avar enc%20=%20encodeURI(uri);%0Avar dec%20=%20decodeURI(enc);%0Avar res%20=%20enc%20+ %22<br>%22 +%20dec;%0A%3C/pre%3E
INSERT INTO "Mytable" VALUES(
8/*ID <INTEGER>*/,
'Return matching objects from array of objects'/*QUESTION <NVARCHAR(200)>*/,
'%3Cp%3Ethis%20kind%20of%20text%20i'm%20storing%20into%20database%3C/p%3E%3C pre%20class=%22code-pre%22%3Evar uri%20= %22my%20test.asp ?
name=st%C3%A5le&car=saab%22;%0Avar enc%20=%20encodeURI(uri);%0Avar
dec%20=%20decodeURI(enc);%0Avar res%20=%20enc%20+ %22<br>%22
+%20dec;%0A%3C/pre%3E'/*QUESTION_DESC <CLOB>*/,
'20170508'/*CREATED <TIMESTAMP>*/,
0/*USERID <INTEGER>*/,
1/*TAGID <INTEGER>*/
);
Above command i'm using for pushing data to db. QUESTION_DESC string i've encoded.original string is
<p>this kind of text i'm storing into database</p><pre class="code-
pre">var uri = "my test.asp?name=ståle&car=saab";
var enc = encodeURI(uri);
var dec = decodeURI(enc);
var res = enc + "<br>" + dec;
</pre>
Help will be appriciated
This was quite simple when i tried posting html code from middle ware.
The problem is when i tried to post an html code to database it was showing error because of some random double quotes. So while sending that html code from middle ware i just replaced the double quotes to ignore double quotes. i did like
my html code to be stored in db
var htmlCodeToBeStored =
"<p>this kind of text i'm storing into database</p><pre class="code-
pre">var uri = "my test.asp?name=ståle&car=saab";
var enc = encodeURI(uri);
var dec = decodeURI(enc);
var res = enc + "<br>" + dec;
</pre>"
I replaced above string with as below
htmlCodeToBeStored = htmlCodeToBeStored.replace(/"/g, "\"")
with that simple change i'm able to store my ans into data base.

How can I write data in a single line on text file using javascript

I am trying to write data in a .txt file using JavaScript function.
My function :-
function WriteToFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("test.txt", true);
for(var j=1;j<9;j++)
{
if(document.getElementById('chk'+j).checked)
{
s.WriteLine(' ');
s.WriteLine(document.getElementById('item'+j).innerText);
s.WriteLine(',');
s.WriteLine(document.getElementById('txtBx'+j).value);
s.WriteLine(',');
s.WriteLine(document.getElementById('txtBxx'+j).value);
s.WriteLine(';');
}
}
alert("written");
s.Close();
}
Now the problem is that data being written on a new line.
(maybe because i am using s.WriteLine ?)
I want all the data to be written in a single line. how can i achieve this?
to elaborate more, my current output looks like this
abc
,
1
,
cdf
;
def
,
3
,
ehi
;
I want it like this-
abc,1,cdf;def,3,ehi;
Use s.write() instead of s.writeLine(). As the name implies, the second function is for writing whole lines, and it adds a newline after it.
Just add everything to a variable then write the line.
var st = document.getElementById('item'+j).innerText;
st += ','
st += document.getElementById('txtBx'+j).value;
s.WriteLine(st);

Finding the difference between two fields using JavaScript in iText

I would like to find difference between two fields using JavaScript in iText.
I am able to find the sum of them using below code:
PdfStamper stamperResult = new PdfStamper(readersectionResult, new FileOutputStream(RESULT_NEW));
stamperResult .addJavaScript("var nameField = this.getField(\"total\");"+ "nameField.setAction(\"Calculate\",'AFSimple_Calculate(\"SUM\",\"total1\", \"total2\")')");
Is there any way to find the difference using 'AFSimple_Calculate' similar to what I did in the above code snippet?
Thanks for editing! I tried your suggestion but it does not seem to work for some reason.
stamperResult.addJavaScript(" var total1 = this.getField(\"value1\"); var total2 = this.getField (\"value2\"); var subtr = this.getField(\"total\"); subtr.value = total1.value - total2.value;");
I separated newlines by spaces and added right escape characters.
I was also thinking of using a different logic for subtraction using AF methods : like this
stamperResult.addJavaScript("var nameField = this.getField(\"total\");"+ "nameField.setAction(\"Calculate\",'AFSimple_Calculate(\"SUM\",\"total1\", \"-total2\")')");
In the above code I was trying to add -(negative value) to total 2 so that it will be subtracted from total1 though the AF method is still 'SUM'.
But that does not work.
The below simple code seem to work :
stamperResult.addJavaScript("var nameField = this.getField('total');" +
"nameField.setAction('Calculate'," +
"'subtract()');" +
"" +"function subtract(){this.getField('total').value
= (this.getField('total_1').value -this.getField('total_2').value); }");
I updated your question because it contained many spelling errors. I didn't edit the code snippet because I don't know what the original code snippet is like. In any case: I think something went wrong during the copy/paste process, as I don't think your code snippet compiles in its current state.
In any case: as far as I know the AF-methods (the AF stands for Adobe Forms) may not be present in every viewer, and as far as I know Adobe didn't implement a way to subtract values from each other in the AFSimple_Calculate method.
For these two reasons, you may prefer regular JavaScript instead of using a pre-canned function that may or may not be pre-canned.
This regular JavaScript may look like this:
var total1 = this.getField("total1");
var total2 = this.getField("total2");
var subtr = this.getField("difference");
subtr.value = total1.value - total2.value;
I'm not sure if that answers your question. Maybe you just want:
var total1 = this.getField("total1");
var total2 = this.getField("total2");
var namefield = total1.value - total2.value;
You can put these lines inside a String using the right escape characters and replacing the newlines by spaces or newline characters.
Of course, you need to trigger this code somewhere. Below you'll find an example that puts the negative value of the content of a value1 field into a value2 field.
public static void main(String[] args) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("js.pdf"));
document.open();
writer.addJavaScript("function makeNegative() { this.getField('value2').value = -(this.getField('value1').value); } ");
Rectangle rect1 = new Rectangle(40, 740, 200, 756);
TextField value = new TextField(writer, rect1, "value1");
value.setBorderColor(GrayColor.GRAYBLACK);
value.setBorderWidth(0.5f);
PdfFormField field = value.getTextField();
field.setAdditionalActions(PdfName.BL, PdfAction.javaScript("makeNegative();", writer));
writer.addAnnotation(field);
Rectangle rect2 = new Rectangle(40, 710, 200, 726);
TextField neg = new TextField(writer, rect2, "value2");
neg.setBorderColor(GrayColor.GRAYBLACK);
neg.setBorderWidth(0.5f);
writer.addAnnotation(neg.getTextField());
document.close();
}
Note that I used a Blur action. This means the method will be triggered as soon as you select another field after filling out the value1 field.

Javascript ADO recordset open method not working

I have the following code in an html page in a Javascript tag:
var adOpenDynamic = 2
var adLockOptimistic = 3
var conn_str = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=G:/path_to_myDB.mdb'
var conn = new ActiveXObject("ADODB.Connection")
conn.open(conn_str)
and this is a the beginning of a function that is called from the onload event of the html :
var PassNbrAppel = new Array();
var i=1
var rsPass = new ActiveXObject("ADODB.Recordset")
SQLpass = 'SELECT Avis.[Numéro Passerelle], Count(Avis.[Numéro Passerelle]) AS [CompteDeNuméro Passerelle] FROM Avis WHERE (((Avis.[Date Appel])>#10/19/2011# And (Avis.[Date Appel])<#11/07/2011#) AND (Avis.[Numéro Passerelle] IS NOT NULL)) GROUP BY Avis.[Numéro Passerelle] ORDER BY Val(Avis.[Numéro Passerelle]);'
rsPass.open(SQLpass, conn, adOpenDynamic, adLockOptimistic)
rs2arr(rsPass,arrPass)
rs.close()
I get the following error message (translated from french): "no value given for one or more of the required parameters" and the line number is pointing to rsPass.open(SQLpass, conn, adOpenDynamic, adLockOptimistic)
I keep on re-checking to see if there is a mistake in the code but I can't seem to find anything wrong...
I took bits of code from here
The problem was the special characters in my SQL statement. Instead of trying to make it work with the "é" I changed the feild names so they dont have special characters. So much for French pride...

Categories