Use REST or XSL to get a Table with totals
-
10-12-2019 - |
Question
I have a list with items that contains several columns like:
- Year
- Month
- HourCategory1
- HourCategory2
- HourCategory3
Now I want to create a table that shows only totals of the hour categories:
HourCategory1 : Sum of all items
HourCategory2 : Sum of all items
HourCategory3 : Sum of all items
Total hours : Sum of all HourCategory1 + HourCategory2 + HourCategory3
How can I achieve this with out-of-the box functionality using existing WebParts/ Views or Javascript, JSLink XSL etc.
Solution
Solved it with XSL using a XsltListViewWebPart.
Created totalColumn template
<xsl:template name="totalColumn">
<xsl:param name="node"/>
<xsl:param name="fieldName"/>
<xsl:param name="sum" select="0"/>
<xsl:choose>
<xsl:when test="$node">
<xsl:call-template name="totalColumn">
<xsl:with-param name="node" select="$node/following-sibling::Row"/>
<xsl:with-param name="fieldName" select="$fieldName"/>
<xsl:with-param name="sum" select="$sum + translate($node/@*[name() = $fieldName], '.', '')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$sum"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Created three variables like
Then use it like this
<xsl:value-of select="$HourCategory1"/>
<xsl:value-of select="$HourCategory2"/>
<xsl:value-of select="$HourCategory3"/>
<xsl:value-of select="$HourCategory1+ $HourCategory2+ $HourCategory3"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange