getValue in checkgroup for more than one value

Questions and discussion about using ProcessMaker: user interface, running cases & functionality

Moderator: amosbatto

michalborkowski
Posts: 11
Joined: Mon Apr 24, 2017 12:26 pm
Country: Poland

getValue in checkgroup for more than one value

Unread postby michalborkowski » Thu Dec 14, 2017 5:08 pm

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,

amosbatto
Posts: 5678
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: getValue in checkgroup for more than one value

Unread postby amosbatto » Thu Dec 14, 2017 8:28 pm

Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

amosbatto
Posts: 5678
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: getValue in checkgroup for more than one value

Unread postby amosbatto » Thu Dec 14, 2017 8:57 pm

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
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

michalborkowski
Posts: 11
Joined: Mon Apr 24, 2017 12:26 pm
Country: Poland

Re: getValue in checkgroup for more than one value

Unread postby michalborkowski » Fri Dec 15, 2017 7:50 am

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 !

amosbatto
Posts: 5678
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: getValue in checkgroup for more than one value

Unread postby amosbatto » Fri Dec 15, 2017 6:33 pm

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.
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

michalborkowski
Posts: 11
Joined: Mon Apr 24, 2017 12:26 pm
Country: Poland

Re: getValue in checkgroup for more than one value

Unread postby michalborkowski » Fri Jan 12, 2018 7:21 pm

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,

amosbatto
Posts: 5678
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: getValue in checkgroup for more than one value

Unread postby amosbatto » Fri Jan 12, 2018 9:25 pm

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();
    }
});
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

amosbatto
Posts: 5678
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: getValue in checkgroup for more than one value

Unread postby amosbatto » Fri Jan 12, 2018 9:59 pm

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.
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

amosbatto
Posts: 5678
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: getValue in checkgroup for more than one value

Unread postby amosbatto » Fri Jan 12, 2018 10:45 pm

I filed an internal bug report about form.getFields() at https://processmaker.atlassian.net/browse/TRI-3235
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

michalborkowski
Posts: 11
Joined: Mon Apr 24, 2017 12:26 pm
Country: Poland

Re: getValue in checkgroup for more than one value

Unread postby michalborkowski » Sat Jan 13, 2018 7:23 am

Hmm.. I can't force it to work...
Could you show me the solution in a simple dynaform example?

Regards,

michalborkowski
Posts: 11
Joined: Mon Apr 24, 2017 12:26 pm
Country: Poland

Re: getValue in checkgroup for more than one value

Unread postby michalborkowski » Sun Jan 14, 2018 11:08 am

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[i].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);
        }
    }
});

amosbatto
Posts: 5678
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: getValue in checkgroup for more than one value

Unread postby amosbatto » Tue Jan 16, 2018 12:09 am

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);
           }
        }
    }
});
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

michalborkowski
Posts: 11
Joined: Mon Apr 24, 2017 12:26 pm
Country: Poland

Re: getValue in checkgroup for more than one value

Unread postby michalborkowski » Tue Jan 16, 2018 4:19 pm

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(newValoldVal) {
  if (
newVal == '"1"' || newVal == '["1"]') {
    var 
userID = ["user1""user2""user3"] ;    //issue1: this does not work
    
var numRows = $("#Grid3").getNumberRows();
    for (var 
i=1<= numRowsi++) {
      
//issue1: this does not work. Works if userID is set manually -> == "user1"
      
if ($("#Grid3").getText(i1) == userID) { 
        var 
totalCost parseFloat($("#Grid3").getSummary("Price"));     //
        
for (var 1<= numRowsi++) {
          
price_user parseFloat($("#Grid3").getValue(i3));
          $(
"#user1_sum").setValue(roundToFixed(totalCost2)); 
        }
      }    
    }
  }
}; 
 

amosbatto
Posts: 5678
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: getValue in checkgroup for more than one value

Unread postby amosbatto » Tue Jan 16, 2018 10:13 pm

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) ); 
      
}    
    
}
  }
});
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)

michalborkowski
Posts: 11
Joined: Mon Apr 24, 2017 12:26 pm
Country: Poland

Re: getValue in checkgroup for more than one value

Unread postby michalborkowski » Wed Jan 17, 2018 6:46 am

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) );
      }   
    }
  }
);


Attachments
Test_v2-2.pmx
(28.5 KiB) Not downloaded yet

amosbatto
Posts: 5678
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz
Contact:

Re: getValue in checkgroup for more than one value

Unread postby amosbatto » Wed Jan 17, 2018 9:06 pm

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 0ii aUserIDs.lengthii++) {
      var 
userSum 0;   
      
//loop for each row in Grid3:
      
for (var i=1<= numRowsi++) {
        
//add price if matching user:
        
if ($("#Grid3").getText(i1) == aUserIDs[ii]) {
          
userSum += parseFloat( $("#Grid3").getValue(i3) );
        }
      }
      $(
"#" aSumFields[ii]).setValueroundToFixed(userSum2) );   
    }
}
); 
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA (http://www.illa-a.org)


Return to “Using ProcessMaker 3”

Who is online

Users browsing this forum: No registered users and 6 guests