5 Replies Latest reply on Nov 11, 2015 7:06 AM by frameexpert

    Seeing which checkboxes are checked

    frameexpert Level 4

      I need some help with a ScriptUI algorithm. As part of a dialog box, I have a series of 15 checkboxes with statictext labels underneath. To get the layout correct, each checkbox and label are part of a group. I am looking for an efficient way to see which checkboxes are checked when the user clicks OK. I was thinking of adding a "checked" array property to the window itself and update it when a user clicks a checkbox, but I am not sure how to figure out which checkbox is being clicked. Any advice would be appreciated. Here is code that isolates the checkboxes and OK and Cancel buttons. Thank you. -Rick

       

      var w = new Window('dialog',"Resize Table Columns",undefined);
      buildWindow ();
      w.show ();
      
      function buildWindow () {
      
          var cbGrp, btnGrp, btn, i;
      
          // Properties for cbGrp
          cbGrp = w.add ('group',undefined);
          cbGrp.alignChildren = ['fill',' '];
           cbGrp.orientation = "row";
           cbGrp.checked = [];
           
           for (i = 1; i <= 15; i += 1) {
               cbGrp["grp" + i] = cbGrp.add ("group", undefined, {name: "grp" + i});
               cbGrp["grp" + i].orientation = "column";
               cbGrp["grp" + i].spacing = 4;
               cbGrp["grp" + i].addEventListener ("click", toggleCb, true);
               cbGrp["grp" + i]["cb" + i] = cbGrp["grp" + i].add ("checkbox", undefined, undefined);
               cbGrp["grp" + i]["lb" + i] = cbGrp["grp" + i].add ("statictext", undefined, i);
               cbGrp.checked.push (0);
           }
      
           // Add the OK/Cancel buttons.
          btnGrp = w.add ('group', undefined);
          btn = btnGrp.add ("button",undefined,'OK',{name:"ok"});
          btn.onClick = validate;
          btn = btnGrp.add ("button",undefined,'Cance',{name:"cancel"});
          
          function validate () {
              alert (cbGrp.checked);
          }
      
          function toggleCb (e) {
              
              var cb;
                      
              if (e.target.type === "group") {
                  cb = e.target.children[0];
                  cb.value = !cb.value;
              }
              else if (e.target.type === "statictext") {
                  cb = e.target.parent.children[0];
                  cb.value = !cb.value;
              }
              else {
                  alert (e.target.name);
              }
          }
      }
      
        • 1. Re: Seeing which checkboxes are checked
          Ronald63 Level 4

          Hi,

           

          A quick idea ...

           

          var w = new Window('dialog',"Resize Table Columns",undefined);
          buildWindow ();
          w.show ();
          
          function buildWindow () {
              var cbGrp, btnGrp, btn, i,cbNbr,cbValue;
          
              // Properties for cbGrp
              cbGrp = w.add ('group',undefined);
              cbGrp.alignChildren = ['fill',' '];
              cbGrp.orientation = "row";
              cbNbr = 15;
              cbValue=[];
          
               for (i = 1; i <= cbNbr; i += 1) {
                   cbGrp["grp" + i] = cbGrp.add ("group", undefined, {name: "grp" + i});
                   cbGrp["grp" + i].orientation = "column";
                   cbGrp["grp" + i].spacing = 4;
                   cbGrp["grp" + i].addEventListener ("click", toggleCb, true);
                   cbGrp["grp" + i]["cb" + i] = cbGrp["grp" + i].add ("checkbox", undefined, undefined);
                   cbGrp["grp" + i]["lb" + i] = cbGrp["grp" + i].add ("statictext", undefined, i); 
               }
          
               // Add the OK/Cancel buttons.
              btnGrp = w.add ('group', undefined);
              btn = btnGrp.add ("button",undefined,'OK',{name:"ok"});
              btn.onClick = validate;
              btn = btnGrp.add ("button",undefined,'Cance',{name:"cancel"});
          
          function validate () {
              // array with cb value
              for (i= 1; i < cbNbr; i+= 1) {
                  cbValue.push(cbGrp["grp" + i]["cb" + i].value);
              }
              alert(cbValue)
          }
          
          function toggleCb (e) {
              var cb;
              if (e.target.type === "group") {
                  cb = e.target.children[0];
                  cb.value = !cb.value;
              }
              else if (e.target.type === "statictext") {
                  cb = e.target.parent.children[0];
                  cb.value = !cb.value;
              }
              else {
                  alert (e.target.name);
              }
              } 
          }
          
          • 2. Re: Seeing which checkboxes are checked
            Jump_Over Level 5

            Hi,

             

            Look into this sample:

            var 
              cbGrp, i, cCheck, checkedArr = [],
              w = new Window('dialog',"Resize Table Columns",undefined);  
              
                // Properties for cbGrp  
              cbGrp = w.add ('group',undefined);  
                   
                 for (i = 0; i < 5; i ++) {
                     cCheck = cbGrp.add ("checkbox", undefined, "item 0" + ( i + 1 ) );  
              checkedArr[i] = false;
              cCheck.helpTip = i;
              cCheck.onClick = function () {
              checkedArr[ Number( this.helpTip) ] = this.value;
              }
              }
              w.show ();
              alert (checkedArr);
            

             

            Jarek

            • 3. Re: Seeing which checkboxes are checked
              Peter Kahrel Adobe Community Professional & MVP

              Hi Romano,

               

              You made things very complicated for yourself. On top of Ronald's and Jarek's suggestions, here's another one. There's no need for an eventListener. Checkboxes toggle themselves, you don't need to toggle them explicitly. And it's easier simply to check the state of all checkboxes when you're done rather than tracking their state continuously. And using the 'children' property you don't have to create a variable or property for each checkbox.

               

              Two changes to your script. First, replace your  for (i = 1; i <= 15; i += 1) loop with the following:

               

              for (var i = 1; i <= 15; i += 1) {
                with (cbGrp.add ('group {orientation: "column", spacing: 4}')) {
                  add ("checkbox");
                  add ("statictext", undefined, i);
                }
              }
              

               

              That's all you need to add the items to the window. Then replace your validate() function with this:

               

              function validate () {
                var arr = [];
                for (var i = 1; i < cbGrp.children.length; i++) {
                  arr.push (cbGrp.children[i].children[0].value);
                }
                alert (arr);  
              }  
              

               

              Peter

              • 4. Re: Seeing which checkboxes are checked
                Peter Kahrel Adobe Community Professional & MVP

                Sorry, Rick, for addressing you as Romano! (Jive's edit doesn't work)

                P.

                • 5. Re: Seeing which checkboxes are checked
                  frameexpert Level 4

                  Hi Peter, Thanks for the reply. I added the event listener so that if the user clicks on the statictext (or group), the checkbox value will toggle. I will try your code in my script and see how it works. Thanks. -Rick