Hi!
I'm working with SharePoint Designer and I have a webpart based on a document library that I have converted to a xslt dataview.
What I'm trying to do is that I need to access a numeric value in my webpart so I can perform calulations with it in JavaScript. The value will always be a 5 digit value, e.g. "14 104". Now, notice that the 1000 delimiter is a space (I'm working with swedish regional settings, english versions uses a comma i.e. 14,104). I need to remove that delimiter to perform calulations.
This is what I tried first:
Code:
<xsl:variable name="NbDaysLeft" select="number(translate(@Dagar_x0020_kvar, ' ', '')" />
<script type="text/javascript">
//Fetch value from the xsl variable
var DaysToDue = '<xsl:value-of disable-output-escaping="no" select="$NbDaysLeft" />';
... ... ...
If I then try to print DaysToDue I get NaN (Not a Number) returned. And if I leave out the number() function call and print the value I still get "14 104" indicating that the translate failed or isn't applicable in this case.
So I then tried to format the value in JavaScript, here's my current code:
Code:
<xsl:variable name="NbDaysLeft" select="@Dagar_x0020_kvar" />
<script type="text/javascript">
//Fetch value from the xsl variable
var DaysToDue = '<xsl:value-of disable-output-escaping="no" select="$NbDaysLeft" />';
var newDaysToDue;
var validChars = "0123456789";
for(var i = 0; i < DaysToDue.length-1; i++)
{
if(validChars.indexOf(DaysToDue.substring(i, 1)) != -1) /*DaysToDue[i] doesn't work. Printing DaysToDue[i] returns "undefined".*/
{
alert(DaysToDue.substring(i, 1) + " " + "matched");
//Move valid char to a new variable.
newDaysToDue += DaysToDue.substring(i, 1);
}
alert("Updated value: " + newDaysToDue);
}
newDaysToDue = "";
</script>
Output from the first alert:
Iteration : Output
1 - "1 matched"
2 - " matched"
3 - "4 matched"
Output from the second alert:
Iteration - Output
1 - "Updated value: undefined1"
2 - "Updated value: undefined1"
3 - "Updated value: undefined14"
4 - "Updated value: undefined14"
5 - "Updated value: undefined14"
As you can see the code successfully finds the first digit. But then spooky things starts to happen. Where does it find this "undefined" character? Why is it inserted before 1? And why does the matchings fail after 4?
Just to narrow it down I created a local variable initiated to "14 104", removed the space with a .split.join, and parsed it to an integer. Printing the value resultet in "14104" and I also validated that it was in fact a number by running it through typeof().
The idea behind all this can be found in this blog if it helps as reference:
http://pathtosharepoint.wordpress.com/2008/08/25/a-countdown-for-tasks-lists/'
Any input is greatly appriciated!