I have created a power table linked to a SQL DB.
This is for a checklist where the user will check if an action has been performed
.
At the end I have a button (Adherencia) to calculate the percentage of the actions performed.
But I keep getting this error:
This is the code I have setup for the button to count the checked boxes and then calculate percentage:
ds = event.source.parent.getComponent('Power Table').Checked
count = sum(ds.getValueAt(row, "Checked") for row in range(ds.rowCount))
event.source.parent.getComponent("Numeric Text Field 1").intValue = (count*100)/34
Any Idea what is wrong with the code?
Also if possible, is there a code to clear the check boxes with the same button?
ds = event.source.parent.getComponent('Power Table').data
count = sum(ds.getValueAt(row, "Checked") for row in range(ds.rowCount))
event.source.parent.getComponent("Numeric Text Field 1").intValue = (count*100)/34
I’m not sure what the sum function is, I assume it is a built in function, however, it looks like the error seems to be in that function.
Try the following code to get the sum of checked rows:
ds = event.source.parent.getComponent('Power Table').data
count = 0
for row in range(ds.rowCount):
count += ds.getValueAt(row,'Checked')
event.source.parent.getComponent('Numeric Text Field 1').intValue = (count*100)/34
If you get the same error, you may need to force the type of the value to an integer value
ds = event.source.parent.getComponent('Power Table').data
count = 0
for row in range(ds.rowCount):
count += int(ds.getValueAt(row,'Checked'))
event.source.parent.getComponent('Numeric Text Field 1').intValue = (count*100)/34
I created a new display with just a power table and a button. I set the power table data table up with two columns, a string column name and a boolean column Checked.
I put the following code in the button action preformed event and each variance of code executed without error and calculated the correct number of checked boxes.
ds = event.source.parent.getComponent('Power Table').data
count = 0
for row in range(ds.rowCount):
count += ds.getValueAt(row,'Checked')
print count
count = 0
for row in range(ds.rowCount):
count += int(ds.getValueAt(row,'Checked'))
print count
print sum(ds.getValueAt(row,'Checked') for row in range(ds.rowCount))
This makes me believe that your column ‘Checked’ is some data type that were not expecting. Can you provide more detail on how you have the columns setup, or provide the code for how you fill the table?