Page 1 of 1

getValue in checkgroup for more than one value

Posted: Thu Dec 14, 2017 5:08 pm
by michalborkowski
Hi,

I have a problem (probably really simple ;) )
I have a checkgroup "checkgroup1" with three values 1, 2, 3
I want to check what values were selected: 1 , 1 and 2, 2 and 3, etc. with the option getValue() and do some math that depends on the checkgroup selection.

Unfortunately I can't get value when more than one option from checkgroup1 is selected:

function checkgroup_math()
{
if ($("#checkgroup1").getValue() == "1"){
function do_math(){
...
...
...
}
do_math();
}
else { }
$("#checkgroup1").setOnchange(checkgroup_math);

How to: if ($("#checkgroup1").getValue() == value 1 and value 2, etc. )


By the way.. Is it better to use getValue or setOnchange( function(newVal, oldVal)?

Thanks for help
Regards,

Re: getValue in checkgroup for more than one value

Posted: Thu Dec 14, 2017 8:28 pm
by amosbatto

Re: getValue in checkgroup for more than one value

Posted: Thu Dec 14, 2017 8:57 pm
by amosbatto
Your code would be something like this:
Code: Select all
function checkgroup_math(aSelected, aOldSelected) {
   if ($.inArray("1", aSelected) != -1 && $.inArray("2", aSelected) != -1) {
          do_math()
   }
}
$("#checkgroup1")setOnchange(checkgroup_math);      //when checkgroup changes
checkgroup_math($("#checkgroup1").getValue(), []);  //when DynaForm loads

Re: getValue in checkgroup for more than one value

Posted: Fri Dec 15, 2017 7:50 am
by michalborkowski
After a few tweaks everything works like a charm :)

Below my if statements:

Option 1 selected in checkgroup
if ($.inArray("1", aSelected) != -1 && $.inArray("2", aSelected) != 0 && $.inArray("3", aSelected) != 0 )

Option 1 and 2 selected in checkgroup
if ($.inArray("1", aSelected) != -1 && $.inArray("2", aSelected) != -1 && $.inArray("3", aSelected) != 0 )
.
.
.
etc.

Thanks for help !

Re: getValue in checkgroup for more than one value

Posted: Fri Dec 15, 2017 6:33 pm
by amosbatto
You are probably going to have problems with your code, because $.inArray() returns the array position of the searched item, so it can be 0, 1, 2, 3, etc. or -1 if not found.

If you want to check if option "1" is selected in the checkgroup, use this code:
if ($.inArray("1", aSelected) != -1)

To check if options "1" and "2" are selected:
if ($.inArray("1", aSelected) != -1 && $.inArray("2", aSelected) != -1)

If you do this: ($.inArray("2", aSelected) == 0)
Then you are checking if "2" is the first selected option, but if the user also selects "1", then $.inArray("2", aSelected) will return 1, not 0.

Re: getValue in checkgroup for more than one value

Posted: Fri Jan 12, 2018 7:21 pm
by michalborkowski
Hello in the new year!
I was away from working on processmaker for almost a month...

I've changed the statement from: !=0 to == -1 for the missing values and everything works fine
example below:

if ($.inArray("1", aSelected) != -1 && $.inArray("2", aSelected) != -1 && $.inArray("3", aSelected) == -1 )

But You were right about the issues with the code :) Thank You for bringing this to my attention :)

By the way i have two questions:

1. Question about form.getField() function and it's description on wiki. I can't force it to work on 3.2.1 community, console says: Uncaught TypeError: $(...).getFields is not a function. Is it only enterprise edition function?

code from wiki
Code: Select all
$('#btnFormFields').on('click', function(e) {
    var fields;
    e.preventDefault();
    fields = $('#590857782596fc2fd9cad32087250836').getFields();
    for (var i=0;i< fields.length;i++){
        fields[i].disableValidation();
    }
});
2. Is it possible to change the whole Form display mode from Edit to View or Disabled using a button or something like that?

Regards,

Re: getValue in checkgroup for more than one value

Posted: Fri Jan 12, 2018 9:25 pm
by amosbatto
I just checked getFields() and it always returns an empty array, so it has a bug in it.

To work around the problem, you can use this code to get an array of the fields in a form:
getFormById( $("form").prop("id") ).model.attributes.project.fields

For example, you can use this code:
Code: Select all
$('#btnFormFields').on('click', function(e) {
    var fields;
    e.preventDefault(); //only necessary for submit buttons
    fields = getFormById( $("form").prop("id") ).model.attributes.project.fields;
    for (var i=0; i < fields.length; i++){
        fields[i].disableValidation();
    }
});

Re: getValue in checkgroup for more than one value

Posted: Fri Jan 12, 2018 9:59 pm
by amosbatto
To make all the input fields read-only, you can do something like this:
Code: Select all
var aFieldTypes = ["text", "textarea", "dropdown", "radio", "checkbox", "checkgroup", "suggest", "datetime"];
$('#btnReadOnly').find("button").click( function() {
    var aFields = getFormById( $("form").prop("id") ).model.attributes.project.fields;
    for (var i=0; i < fields.length; i++) {
        var fieldType = fields[1].getInfo().type;
        if ($.inArray(fieldType, aFieldTypes) != -1) {
            fields[i].getControl().prop("disabled", true);
        }
    }
});
I haven't tested it on all types of fields, so it might require some debug.

Re: getValue in checkgroup for more than one value

Posted: Fri Jan 12, 2018 10:45 pm
by amosbatto
I filed an internal bug report about form.getFields() at https://processmaker.atlassian.net/browse/TRI-3235

Re: getValue in checkgroup for more than one value

Posted: Sat Jan 13, 2018 7:23 am
by michalborkowski
Hmm.. I can't force it to work...
Could you show me the solution in a simple dynaform example?

Regards,

Re: getValue in checkgroup for more than one value

Posted: Sun Jan 14, 2018 11:08 am
by michalborkowski
After a few tests I've found what is wrong, example below:

Example1

one row
col-span:4 4 4
textbox1 | empty | textbox2

button

When I push the Button, then the error shows: VM14551:5 Uncaught TypeError: aFields.getInfo is not a function
textbox1: is disabled
textbox2: is enabed

Example2

one row
col-span:4 4 4
textbox1 | textbox2 | textbox3

button

When I push the Button, then there is no errors and:
textbox1: is disabled
textbox2: is disabled
textbox3: is disabled


Conclusion
If a row is divided by col-span, then all fields need to be filled with any type of the Web Control because if we leave an empty field, then the error will occur.


Code: Select all
var aFieldTypes = ["text", "textarea", "dropdown", "radio", "checkbox", "checkgroup", "suggest", "datetime"];
$('#btnReadOnly').find("button").click( function() {
    var aFields = getFormById( $("form").prop("id") ).model.attributes.project.fields;
    for (var i=0; i < aFields.length; i++) {
        var fieldType = aFields[i].getInfo().type;
        if ($.inArray(fieldType, aFieldTypes) != -1) {
            aFields[i].getControl().prop("disabled", true);
        }
    }
});

Re: getValue in checkgroup for more than one value

Posted: Tue Jan 16, 2018 12:09 am
by amosbatto
Oh, I see the problem. Not all fields/controls in a Dynaform have getInfo().
Try this:
Code: Select all
var aFieldTypes = ["text", "textarea", "dropdown", "radio", "checkbox", "checkgroup", "suggest", "datetime"];
$('#btnReadOnly').find("button").click( function() {
    var aFields = getFormById( $("form").prop("id") ).model.attributes.project.fields;
    for (var i=0; i < aFields.length; i++) {
        //check if properties exist:
        if (aFields[i].getInfo && aFields[i].getInfo().type) {
           var fieldType = aFields[i].getInfo().type;
           if ($.inArray(fieldType, aFieldTypes) != -1) {
             aFields[i].getControl().prop("disabled", true);
           }
        }
    }
});

Re: getValue in checkgroup for more than one value

Posted: Tue Jan 16, 2018 4:19 pm
by michalborkowski
Hi,
Yup, everything works just fine :)

I have a last question ( I HOPE ;P ) about the grids...

Example below:
I have 3 grids in 3 forms:
- Form1, grid1
- Form2, grid2
- Form3, grid3
All three grids have the same fields: userID, Product, Price.

Before Form3 is loaded, the data from the two grids is merged into grid3. When the Form3 is loaded the data from grid1 and grid2 is in grid3.

Now, on Form3, when I click the button (count) I would like to sum the price per each user to the two new fields: user1_sum, user2_sum.

Question:
How to sum the Price per userID in grid.

I was trying to do something like code below but of course it does not work if there is more than one user. (this code is not copied directly from my form because my form is a bit more complicated)
Code: Select all
function roundToFixed(_float, _digits) {
   var rounder = Math.pow(10, _digits);
   return (Math.round(_float * rounder) / rounder).toFixed(_digits);
}

$("#ButtonCount").setOnchange( function(newVal, oldVal) {
  if (newVal == '"1"' || newVal == '["1"]') {
    var userID = ["user1", "user2", "user3"] ;    //issue1: this does not work
    var numRows = $("#Grid3").getNumberRows();
    for (var i=1; i <= numRows; i++) {
      //issue1: this does not work. Works if userID is set manually -> == "user1"
      if ($("#Grid3").getText(i, 1) == userID) { 
        var totalCost = parseFloat($("#Grid3").getSummary("Price"));     //
        for (var i = 1; i <= numRows; i++) {
          price_user = parseFloat($("#Grid3").getValue(i, 3));
          $("#user1_sum").setValue(roundToFixed(totalCost, 2)); 
        }
      }    
    }
  }
}; 
 

Re: getValue in checkgroup for more than one value

Posted: Tue Jan 16, 2018 10:13 pm
by amosbatto
I see a couple problems with your code. Buttons don't have values and the setOnchange() method doesn't work with them. You need to use click() instead. Also you forgot to terminate your event handler code with ).
If you have a fixed number of users and userX_sum fields, then you can do something like this:
Code: Select all
$("#ButtonCount").find('button').click( function() {
  var checkVal = $("#mycheckbox").getValue();
  if (checkVal == '"1"' || checkVal == '["1"]') {
    var aUserIDs = ["user1", "user2", "user3"];
    var aSumFields = ["user1_sum", "user2_sum", "user3_sum"];
    var numRows = $("#Grid3").getNumberRows();

    //loop for each row in Grid3:
    for (var i=1; i <= numRows; i++) {
      //loop for each user:
      for (var ii = 0; ii < aUserIDs.length; ii++) {
        var userSum = 0;
        //add price if matching user:
        if ($("#Grid3").getText(i, 1) == $aUserIDs[ii]) {
          userSum += parseFloat( $("#Grid3").getValue(i, 3) );
        }
        
        $("#" + aSumFields[ii]).setValue( roundToFixed(userSum, 2) ); 
      }    
    }
  }
});

Re: getValue in checkgroup for more than one value

Posted: Wed Jan 17, 2018 6:46 am
by michalborkowski
Hi,
Something is wrong.

Code below and sample process attached that simulates 3rd form with Grid3.
When I add:
- row: 1,2 with user1 and click count button: only price from last row is visible (no sum)
- row 1: user1, row 2: user2: only price from last row is visible ( user1 gets 0, user2: price )
- row 1: user1, row 2: user2, row 3: user2: only price from last row is visible (user1 gets 0, user2: price from row3 (no sum from row2 and row3) )


Code: Select all
function roundToFixed(_float, _digits) {
   var rounder = Math.pow(10, _digits);
   return (Math.round(_float * rounder) / rounder).toFixed(_digits);
}



$("#ButtonCount").find('button').click( function() {
 // var checkVal = $("#mycheckbox").getValue();
  //if (checkVal == '"1"' || checkVal == '["1"]') {
    var aUserIDs = ["user1", "user2", "user3"];
    var aSumFields = ["user1_sum", "user2_sum", "user3_sum"];
    var numRows = $("#Grid3").getNumberRows();

    //loop for each row in Grid3:
    for (var i=1; i <= numRows; i++) {
      //loop for each user:
     
      for (var ii = 0; ii < aUserIDs.length; ii++) {
      var userSum = 0;   
        //add price if matching user:
        if ($("#Grid3").getText(i, 1) == aUserIDs[ii]) {
          userSum += parseFloat( $("#Grid3").getValue(i, 3) );
        }
        
        $("#" + aSumFields[ii]).setValue( roundToFixed(userSum, 2) ); 
      }    
    }
  }
);



Re: getValue in checkgroup for more than one value

Posted: Wed Jan 17, 2018 9:06 pm
by amosbatto
The user needs to be the outer for loop, like this:
Code: Select all
function roundToFixed(_float, _digits) {
   var rounder = Math.pow(10, _digits);
   return (Math.round(_float * rounder) / rounder).toFixed(_digits);
}

$("#ButtonCount").find('button').click( function() {
 // var checkVal = $("#mycheckbox").getValue();
  //if (checkVal == '"1"' || checkVal == '["1"]') {
    var aUserIDs = ["user1", "user2", "user3"];
    var aSumFields = ["user1_sum", "user2_sum", "user3_sum"];
    var numRows = $("#Grid3").getNumberRows();
    
    //loop for each user:
    for (var ii = 0; ii < aUserIDs.length; ii++) {
      var userSum = 0;   
      //loop for each row in Grid3:
      for (var i=1; i <= numRows; i++) {
        //add price if matching user:
        if ($("#Grid3").getText(i, 1) == aUserIDs[ii]) {
          userSum += parseFloat( $("#Grid3").getValue(i, 3) );
        }
      }
      $("#" + aSumFields[ii]).setValue( roundToFixed(userSum, 2) );   
    }
}
);