Computed fields in UI Designer

1
0
-1

Hello,

i have looked, searched, tried.... but i cannot find a way.

How is it possible to make computed fields on a form in the new UI Designer (Community Edition).

E.g. Field Sum should display a computed value of the fields price and quantity (Price * Quantity).

Thanks for any hint,help,link, example.

Boris

3 answers

1
+1
-1

Yes it is,

But it's not as easy as you might have hoped...

You need to add the fields as Custom Widgets that use ng-blur so when they loose focus they total up the various fields

for example (tested)

1) Create a new Custom Widget
2) Delete all parameters
3) In the Template delete everything and add the following:

  1. <div>
  2. First number: <input type="text" ng-blur="ctrl.sumFields()" id="pbInput01" name="pbInput01" class="form-control">
  3. <br />
  4. Last number : <input type="text" ng-blur="ctrl.sumFields()" id="pbInput02" name="pbInput02" class="form-control">
  5. <br />
  6. <br />
  7. Summed : <input type="text" id="pbInput09" name="pbInput09" class="form-control">
  8. </div>

4) In the Controller delete everything and add the following:

  1. function ($scope) {
  2.  
  3. // define a function to be used in template with ctrl.sumFields()
  4. this.sumFields = function(){
  5.  
  6. var pbInput01 = document.getElementById("pbInput01");
  7. var pbInput02 = document.getElementById("pbInput02");
  8. if ((pbInput01 !== null && pbInput01.value !== '') &&
  9. (pbInput02 !== null && pbInput02.value !== '')) {
  10.  
  11. document.getElementById("pbInput09").value =
  12. Number(pbInput01.value) +
  13. Number(pbInput02.value);
  14. }
  15. else{
  16. document.getElementById("pbInput09").value = 0;
  17. }
  18. };
  19.  
  20. }

Add to a page and test

regards
Seán

PS: As this reply answers your question, please mark as resolved.

1
0
-1

Hello Sean,
there was a little bit a misunderstood. You described how i can calculate the total of the overall entries in the repeating Container. Thats great and also a Problem of me. Many thanks.

But my origin question is i think an easier Problem. I changed a Little bit my widget that you can easier understand my Problem. The widget has two fields, the Input of the first is copied in the second field.

My Widget:
Template

  1. <span ng-if="environment"><identicon name="{{environment.component.id}}" size="30" background-color="[255,255,255, 0]" foreground-color="[51,51,51]"></identicon> {{environment.component.name}}</span>
  2. <div>
  3. Quelle: <input type="text" ng-blur="ctrl.copy()" id="pbInput" name="pbInput" class="form-control">
  4. <br />
  5. Ziel: <input type="text" id="pbOutput" name="pbOutput" class="form-control">
  6. </div>

Controller:

  1. function ($scope) {
  2. this.copy = function(){
  3.  
  4. var pbInput = document.getElementById("pbInput");
  5. if (pbInput !== null && pbInput.value !== '') {
  6. document.getElementById("pbOutput").value = pbInput.value;
  7. }
  8. else{
  9. document.getElementById("pbOutput").value = 0;
  10. }
  11. };
  12. }

If i insert this widget in a repeating Container then only the first repeating element is calculated (copied).
If you enter a value in the second (e.g.) repeating field, nothing is copied in the Destination field. I think, the function in the widget copies always only the values from the first repeating entry.
How can i handle this? Is this a Problem of the scope of the function?

Many thanks for your Support!

Comments

Submitted by Sean McP on Fri, 03/24/2017 - 21:49

A Tip on displaying CODE/LOGS correctly in Posts:

Do not use the Supplied Buttons above, for some reason they refuse to work correctly, and despite bringing it to Bonitasofts attention, it's low priority.

To Show Code/Logs correctly use

< code >
your code/log
< /code >

removing the spaces to allow the showing of code/logs correctly as here:

  1. your code/log

You should still be able to edit your post to ensure the correct formatting of the code to help us understand it easier.

Thanks and regards
Seán

Submitted by Sean McP on Fri, 03/24/2017 - 21:56

If i insert this widget in a repeating Container then only the first repeating element is calculated (copied).
If you enter a value in the second (e.g.) repeating field, nothing is copied in the Destination field. I think, the function in the widget copies always only the values from the first repeating entry.
How can i handle this? Is this a Problem of the scope of the function?

As we're using JavaScript to directly manipulate the DOM this will always happen, it will use the first element called pbOutput

I think you'll have to design it another way or use a different method (for example a third party table).

I only designed the widget to have one Summation field, you're looking at a summation field per row...something completely different and unfortunately I don't have an answer for that.

1
0
-1

Hi Seán,

many thanks for this answer. A big step Forward for me!!!

But now i have a next Problem and perhaps anyone has an idea!

I have this Custom widget in a repeating Container and now Only the first repeating widget is calculation correctly.
If i made changes in e.g. the second, then the first one is recalculated.

Thanks,
Boris

Comments

Submitted by Sean McP on Mon, 03/20/2017 - 21:56

OK well you can't just use the widget as is...you need to change it accordingly.

However to help you along in this case:

The total field needs to be separate so just create a separate total field at the bottom (I suggest a custom field as you can give it an id, standard widgets can't have one (as yet)) and remove it from the widget I gave you.

Create a simple unique CSS Class that does basically nothing and add it to the input fields in the already existing widgets.

In the code change getElementById to getElementByClass which will pick up all our Class items, then loop through the collection with code something like this:

  1. var summation = 0;
  2. for (each item found in Class){
  3. if (number){
  4. summation = Number(summation) + Number(itemInClass);
  5. }
  6. }

and put it into the total field as previously.

I haven't got time to do it, but this will get you there with a bit of research into JavaScript etc.

regards
Seán

Testing
Notifications