Commit ab3182d5 authored by buttle's avatar buttle

Added column sum to entries display when field is an expiry condition

parent e39758cf
......@@ -31,7 +31,7 @@ babel = Babel(app)
csrf = CSRFProtect()
csrf.init_app(app)
app.config['APP_VERSION'] = 26
app.config['APP_VERSION'] = 27
app.config['SCHEMA_VERSION'] = 12
app.config['RESERVED_SLUGS'] = ['static', 'admin', 'admins', 'user', 'users', 'form', 'forms', 'site', 'sites', 'update']
......
......@@ -462,6 +462,17 @@ class Form(object):
def fieldConditions(self):
return self.form["expiryConditions"]["fields"]
def getConditionalFieldPositions(self):
conditionalFieldPositions=[]
if self.fieldConditions:
for fieldName, condition in self.fieldConditions.items():
if condition['type'] == 'number':
for position, field in enumerate(self.fieldIndex):
if field['name'] == fieldName:
conditionalFieldPositions.append(position)
break
return conditionalFieldPositions
def findAll(cls, *args, **kwargs):
if not g.isRootUser:
kwargs['hostname']=g.site.hostname
......
/**
* Fairly simply, this plug-in will take the data from an API result set
* and sum it, returning the summed value. The data can come from any data
* source, including column data, cells or rows.
*
* Note that it will attempt to 'deformat' any string based data that is passed
* into it - i.e. it will strip any non-numeric characters in order to make a
* best effort attempt to sum all data types. This can be useful when working
* with formatting numbers such as currency. However the trade-off is that no
* error is thrown if non-numeric data is passed in. You should be aware of this
* in case unexpected values are returned - likely the input data is not what is
* expected.
*
* @name sum()
* @summary Sum the values in a data set.
* @author [Allan Jardine](http://sprymedia.co.uk)
* @requires DataTables 1.10+
*
* @returns {Number} Summed value
*
* @example
* // Simply get the sum of a column
* var table = $('#example').DataTable();
* table.column( 3 ).data().sum();
*
* @example
* // Insert the sum of a column into the columns footer, for the visible
* // data on each draw
* $('#example').DataTable( {
* drawCallback: function () {
* var api = this.api();
* $( api.table().footer() ).html(
* api.column( 4, {page:'current'} ).data().sum()
* );
* }
* } );
*/
jQuery.fn.dataTable.Api.register( 'sum()', function ( ) {
return this.flatten().reduce( function ( a, b ) {
if ( typeof a === 'string' ) {
a = a.replace(/[^\d.-]/g, '') * 1;
}
if ( typeof b === 'string' ) {
b = b.replace(/[^\d.-]/g, '') * 1;
}
return a + b;
}, 0 );
} );
......@@ -3,6 +3,9 @@
<script src="/static/dataTables/jquery.dataTables.min.js"></script>
<script src="/static/dataTables/dataTables.bootstrap.min.js"></script>
{% if tallyColumns %}
<script src="/static/dataTables/plugins/sum().js"></script>
{% endif %}
<link rel="stylesheet" type="text/css" href="/static/dataTables/dataTables.bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/static/css/font-awesome.min.css">
......@@ -67,6 +70,7 @@
var csrftoken = "{{ csrf_token() }}";
var fieldIndex={{fieldIndex|tojson}};
var editMode = false;
var tallyColumns = {{tallyColumns|tojson}};
function setEditMode(){
editMode = true;
......@@ -94,11 +98,16 @@ function getRowData($row) {
return rowData;
}
$(document).ready(function() {
$('#entriesTable').DataTable({
var table = $('#entriesTable').DataTable({
"language": {
"url": "/static/dataTables-languages/{{g.current_user.language}}.js"
},
"initComplete": function(settings, json) {
tallyColumns.forEach(function(column) {
col=column+1;
sum=table.column( col ).data().sum();
$("#entriesTable th:nth-child("+ (col+1) +")" ).append( " <span class='col_sum'>("+sum+")</span>" );
});
$("#entriesTable").show();
},
"order": [[1, 'desc']],
......@@ -150,6 +159,9 @@ $('#entriesTable tbody').on('click', '.saveCellEdition', function () {
$cell.removeClass('cellEditionMode');
if (data.saved != true) {
$cell.html(initialValue);
}else{
// could improve this to recalculate the given column
$(".col_sum").hide();
}
}
});
......@@ -179,6 +191,7 @@ $('#entriesTable tbody').on('click', '.delete-row-icon', function () {
if (data.deleted == true) {
$row.children(':not(:first-child)').addClass('deleted-row');
$row.children('td').eq(0).html('<i class="fa fa-undo undo-delete-row" aria-hidden="true"></i>');
$(".col_sum").hide();
}
}
});
......
......@@ -644,6 +644,7 @@ def list_entries(_id):
return redirect(make_url_for('my_forms'))
return render_template('list-entries.html', form=queriedForm,
tallyColumns=queriedForm.getConditionalFieldPositions(),
fieldIndex=queriedForm.getFieldIndexForDataDisplay())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment