← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16053: Data entry, supporting indicators with data element totals in custom forms

 

------------------------------------------------------------
revno: 16053
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-07-09 14:03:34 +0200
message:
  Data entry, supporting indicators with data element totals in custom forms
modified:
  dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js'
--- dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js	2014-06-24 14:58:45 +0000
+++ dhis-2/dhis-web/dhis-web-dataentry/src/main/webapp/dhis-web-dataentry/javascript/entry.js	2014-07-09 12:03:34 +0000
@@ -25,22 +25,11 @@
 	
 	$( 'input[name="total"]' ).each( function( index )
 	{
-		var targetId = $( this ).attr( 'dataelementid' );
-		
-		var totalValue = new Number();
-		
-		$( 'input[name="entryfield"]' ).each( function( index )
-		{	
-			var key = $( this ).attr( 'id' );
-			var entryFieldId = key.substring( 0, key.indexOf( '-' ) );
-			
-			if ( targetId && $( this ).attr( 'value' ) && targetId == entryFieldId )
-			{
-				totalValue += new Number( $( this ).attr( 'value' ) );
-			}
-		} );
-		
-		$( this ).attr( 'value', totalValue );
+		var de = $( this ).attr( 'dataelementid' );
+		
+		var total = dhis2.de.getDataElementTotalValue( de );
+		
+		$( this ).attr( 'value', total );
 	} );
 }
 
@@ -77,6 +66,46 @@
 }
 
 /**
+ * Returns the total sum of values in the current form for the given data element
+ * identifier.
+ */
+dhis2.de.getDataElementTotalValue = function( de )
+{
+	var sum = new Number();
+	
+	$( 'input[name="entryfield"]' ).each( function( index )
+	{	
+		var key = $( this ).attr( 'id' );
+		var entryFieldId = key.substring( 0, key.indexOf( '-' ) );
+		
+		if ( de && $( this ).attr( 'value' ) && de == entryFieldId )
+		{
+			sum += new Number( $( this ).attr( 'value' ) );
+		}
+	} );
+	
+	return sum;
+}
+
+/**
+ * Returns the value in the current form for the given data element and category
+ * option combo identifiers. Returns 0 if the field does not exist in the form.
+ */
+dhis2.de.getFieldValue = function( de, coc )
+{
+	var fieldId = '#' + dataElementId + '-' + categoryOptionComboId + '-val';
+	
+    var value = '0';
+    
+    if ( $( fieldId ).length )
+    {
+        value = $( fieldId ).val() ? $( fieldId ).val() : '0';
+    }
+    
+    return vale;
+}
+
+/**
  * Parses the expression and substitues the operand identifiers with the value
  * of the corresponding input entry field.
  */
@@ -92,16 +121,19 @@
 
         var operand = match.replace( /[#\{\}]/g, '' );
 
-        var dataElementId = operand.substring( 0, operand.indexOf( SEPARATOR ) );
-        var categoryOptionComboId = operand.substring( operand.indexOf( SEPARATOR ) + 1, operand.length );
-
-        var fieldId = '#' + dataElementId + '-' + categoryOptionComboId + '-val';
-
+        var isTotal = !!( operand.indexOf( SEPARATOR ) == -1 );
+        
         var value = '0';
         
-        if ( $( fieldId ).length )
-        {
-            value = $( fieldId ).val() ? $( fieldId ).val() : '0';
+        if ( isTotal )
+        {
+        	value = dhis2.de.getDataElementTotalValue( operand );
+        }
+        else
+        {
+	        var de = operand.substring( 0, operand.indexOf( SEPARATOR ) );
+	        var coc = operand.substring( operand.indexOf( SEPARATOR ) + 1, operand.length );	
+	        value = dhis2.de.getFieldValue( de, coc );
         }
 
         expression = expression.replace( match, value );