Today, the first time in Cognos need to use JS, mainly when the report page display is likely to get the following data
, I need to have the number of words in the type, the following value, the exact 2 decimal places, and the percentage of the typeface, the value is displayed as a percentage. as follows .
I first tried to write a piece of HTML code to format the table data.
<script type= "Text/javascript" >
function getinnerhtml ()
{
Alert (document.getElementById ("MyTable"). InnerHTML);
}
Window.onload=function () {
Formatmoney ();
}
function Formatmoney () {
var fisrtd = document.getElementsByTagName ("TD");
var i=0;
for (i=0;i<fisrtd.length;i=i+2) {
if (Fisrtd[i].innerhtml.indexof ("percent") >=0) {
Alert (fisrtd[i].innerhtml)
Fisrtd[i+1].innerhtml=accmul (fisrtd[i+1].innerhtml,100) + "%"
}
if (Fisrtd[i].innerhtml.indexof ("Amount") >=0) {
Alert (fisrtd[i].innerhtml)
Fisrtd[i+1].innerhtml=outputmoney (fisrtd[i+1].innerhtml)
}
}
}
multiplication function to get the exact multiplication result
Description: JavaScript multiplication results are error-evident when multiplying two floating-point numbers. This function returns a more accurate multiplication result.
Call: Accmul (ARG1,ARG2)
return value: Arg1 times the exact result of arg2
function Accmul (ARG1,ARG2)
{
var m=0,s1=arg1.tostring (), s2=arg2.tostring ();
Try{m+=s1.split (".") [1].length}catch (e) {}
Try{m+=s2.split (".") [1].length}catch (e) {}
Return number (S1.replace (".", "")) *number (S2.replace (".", ""))/math.pow (10,m)
}
function Outputmoney (number) {
Number = Number.replace (/,/g, "");
if (IsNaN (number) | | number = = "")
Return "";
Number = Math.Round (number * 100)/100;
if (number < 0)
Return '-' + outputdollars (Math.floor (math.abs (number)-0) + ') + outputcents (math.abs (number)-0);
Else
Return Outputdollars (Math.floor (number-0) + ") + outputcents (number-0);
}
Formatting amounts
function Outputdollars (number) {
if (number.length <= 3)
return (number = = "?) ' 0 ': number);
else {
var mod = number.length% 3;
var output = (mod = = 0?) ": (number.substring (0, MoD)));
for (i = 0; i < Math.floor (NUMBER.LENGTH/3); i++) {
if (mod = = 0) && (i = = 0))
Output + = number.substring (mod + 3 * I, mod + 3 * i + 3);
Else
Output + = ', ' + number.substring (mod + 3 * I, mod + 3 * i + 3);
}
return (output);
}
}
function Outputcents (amount) {
Amount = Math.Round (((Amount)-Math.floor (amount)) * 100);
Return (Amount < 10?) '. 0 ' + Amount: '. ' + amount);
}
</script>
<body>
<table border= "1" id= "MyTable" >
<tr id= "TR1" >
<TD > Types </td>
<td> Values </td>
</tr>
<tr id= "TR2" >
<td > Amount </td>
<td>10.1</td>
</tr>
<tr id= "TR3" >
<TD > Number </td>
<td>100</td>
</tr>
<tr id= "TR4" >
<TD > Percent </td>
<td>1</td>
</tr>
<tr id= "TR5" >
<TD > Total Amount </td>
<td>12.12563</td>
</tr>
</table>
<input type= "button" onclick= "getinnerhtml ()"
Value= "Alert InnerHTML of Table row"/>
</body>
Then in Cognos, the table is nested a div, and then through the Div ID, get to the table, and then according to the above JS code to achieve the function I need:
<script type= "Text/javascript" >
SetTimeout (formatmoney,20)
function Formatmoney () {
var tabobj = document.getElementById ("Div1");
var fisrtd = tabObj.firstChild.getElementsByTagName ("TD")
var i=0;
for (i=0;i<fisrtd.length;i=i+1) {
if (Fisrtd[i].innerhtml.indexof ("rate") >=0) {
Alert (Fisrtd[i].innertext)
Fisrtd[i+1].innertext=accmul (fisrtd[i+1].innertext,100) + "%"
}
if (Fisrtd[i].innerhtml.indexof ("%") >=0) {
Alert (Fisrtd[i].innertext)
Fisrtd[i+1].innertext=accmul (fisrtd[i+1].innertext,100) + "%"
}
if (Fisrtd[i].innerhtml.indexof ("Amount") >=0) {
Alert (Fisrtd[i].innertext)
Fisrtd[i+1].innertext=outputmoney (Fisrtd[i+1].innertext)
}
}
}
/* Multiplication function to get the exact multiplication result
Description: JavaScript multiplication results are error-evident when multiplying two floating-point numbers. This function returns a more accurate multiplication result.
Call: Accmul (ARG1,ARG2)
return value: Arg1 times the exact result of arg2
*/
function Accmul (ARG1,ARG2)
{
var m=0,s1=arg1.tostring (), s2=arg2.tostring ();
Try{m+=s1.split (".") [1].length}catch (e) {}
Try{m+=s2.split (".") [1].length}catch (e) {}
Return number (S1.replace (".", "")) *number (S2.replace (".", ""))/math.pow (10,m)
}
function Outputmoney (number) {
Number = Number.replace (/,/g, "");
if (IsNaN (number) | | number = = "") return "";
Number = Math.Round (number * 100)/100;
if (number < 0)
Return '-' + outputdollars (Math.floor (math.abs (number)-0) + ') + outputcents (math.abs (number)-0);
Else
Return Outputdollars (Math.floor (number-0) + ") + outputcents (number-0);
}
/* Formatted amount
*/
function Outputdollars (number) {
if (number.length <= 3)
return (number = = "?) ' 0 ': number);
else {
var mod = number.length% 3;
var output = (mod = = 0?) ": (number.substring (0, MoD)));
for (i = 0; i < Math.floor (NUMBER.LENGTH/3); i++) {
if (mod = = 0) && (i = = 0))
Output + = number.substring (mod + 3 * I, mod + 3 * i + 3);
Else
Output + = ', ' + number.substring (mod + 3 * I, mod + 3 * i + 3);
}
return (output);
}
}
function Outputcents (amount) {
Amount = Math.Round (((Amount)-Math.floor (amount)) * 100);
Return (Amount < 10?) '. 0 ' + Amount: '. ' + amount);
}
</script>
JS code format to modify the value of a table