12 Replies Latest reply: May 22, 2013 4:26 AM by leckj RSS

    disabling a submit button if no rows checked in tabular form

    leckj
      Hello,

      I need to prevent user from submitting a page through a button as that button is linked to a page process
      which runs some PL/SQL code . This PL/SQL code and then the page reload afterwards can take a fair amount
      of time, and I would like the button to submit only if items in my tabular form have been checked.

      I've lifted this code from Denes Kubicek page http://apex.oracle.com/pls/otn/f?p=31517:281:101036866999836:::::,
      which count the number of checked rows. (Thanks Denes,great site)
      function f_count_checked ()
      {
      var counter = 0;
      for (var i = 0; i > document.wwv_flow.f01.length; i++)
      {var curr_id = document.wwv_flow.f01.id;

      if ($x(curr_id).checked==true)
      {counter = counter + 1;}}

      alert ('You have checked: '+counter+' rows.');
      }
      How can I use this to prevent the button submitting if no rows are checked?
      
      Can a button run  a javascript code and depending on the outcome,submit the page or not.
      
      Can I use the onclick() method? Are there other ways to run a Javascript function from a button?
      
      Maybe I need to create a dynamic action based on keydown or something and update a hidden item,
      and then disable the submit button if no items checked.
      
      Or maybe all this can be done in one line using jQuery!
      
      When it comes to using Javascript in Apex, I really need some help.
      
      Thanks
      
      Leckraj                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        • 1. Re: disabling a submit button if no rows checked in tabular form
          kvlek
          Hi Lekraj

          You can use an dynamic action for the submit and put the following condition on the DA
          $("input:checked[type='checkbox'][name^='f01']").length > 0 
          In the DA you can submit the page with the name of the button as request.

          This wil prevent the submit to take place when there is no row selected.
          In the false part of the DA you could place an alert that nothing is selected just to make clear to the user that nothing will be executed.

          Regards,
          Kees Vlek
          -----
          Company: http://www.orcado.nl
          Blog: http://www.orcado.nl/blog/blogger/listings/69-kvlek
          Twitter: http://www.twitter.com/skier66
          If the question is answered please change it to answered and mark the appropriate post as correct/helpfull.
          • 2. Re: disabling a submit button if no rows checked in tabular form
            vdsadhi
            You can create the button to call a Javascript function like below,

            Action: Redirect to URL
            URL Target : javascript:f_submit();

            Then add the javascript function in page header, the below function may be useful to you.
            function f_submit() 
            {
            var col_id = document.getElementsByName('f01');
            var cnt = 0;
            
            for (i=0; i < col_id.length; i++){
               if ( col_id.checked == true){
            cnt = cnt +1;
            }
            }

            if(cnt < 1) {
            alert('Please select any row');
            return;
            }

            doSubmit('SUBMIT');
            }



            Thanks,
            Lakshmi
            • 3. Re: disabling a submit button if no rows checked in tabular form
              leckj
              Hello,

              Thanks for your replies,good to know there are 2 ways of doing things,and now I know how
              to run a javascript function from a button followed by a submit.

              In the event, I tried the dynamic action,but I get no action when the button is clicked,
              whether items are checked or not.

              This is what I did: (The checked column name is f01)

              Dynamic Action Name :SUBMIT_IF_CHECKED
              Selection Type :Button
              Condition :Javascript expression
              Value :$("input:checked[type='checkbox'][name^='f01']").length > 0


              True Action:

              Action :Submit Page
              Fire when Event Result is True
              Fire on Page Load: No
              Request Button Name: UPDATE_COLLECTION (The name of the button)
              Show Processing : Yes


              False Action

              Action :Alert
              Fire when Event Result is False
              Fire on Page Load :No
              Text :You must Select at least one item.


              Please show me what I have missed out.

              Thanks.

              Leckraj
              • 4. Re: disabling a submit button if no rows checked in tabular form
                kvlek
                Hi Leckraj,

                Does the DA fires?
                You could add some test code in the DA.
                Add the following javascript as first action in the true and false action from the DA.
                alert('count='+$("input:checked[type='checkbox'][name^='f01']").length)
                You can also debug your page en see in the debug report what is going on.

                Regards,
                Kees Vlek
                -----
                Company: http://www.orcado.nl
                Blog: http://www.orcado.nl/blog/blogger/listings/69-kvlek
                Twitter: http://www.twitter.com/skier66
                If the question is answered please change it to answered and mark the appropriate post as correct/helpfull.
                • 5. Re: disabling a submit button if no rows checked in tabular form
                  leckj
                  Its not firing at all, and I see nothing of note in the Debug.

                  The condition is neither true nor false just like the Oracle NULL!

                  Can a condition evaluate to NULL?


                  Leckraj
                  • 6. Re: disabling a submit button if no rows checked in tabular form
                    kvlek
                    Hi Leckraj,

                    I think you have too check very well the When part form you DA.
                    What do you have at the item Event and are the others exact as you say?


                    Regards,
                    Kees Vlek
                    -----
                    Company: http://www.orcado.nl
                    Blog: http://www.orcado.nl/blog/blogger/listings/69-kvlek
                    Twitter: http://www.twitter.com/skier66
                    If the question is answered please change it to answered and mark the appropriate post as correct/helpfull.
                    • 7. Re: disabling a submit button if no rows checked in tabular form
                      Tom Petrus
                      Did you change the "Action when clicked" property of the button? If it is still set to "Submit page" then the dynamic action will mean nothing as the button's action is still executed. Have you set it to "defined by dynamic action"?
                      • 8. Re: disabling a submit button if no rows checked in tabular form
                        leckj
                        Hello again,

                        It won't even fire if I remove all conditions.

                        Using firebug on the button onclick="void(0);"

                        and this
                        {"triggeringElementType":"BUTTON","triggeringButtonId":"B18926302821591455627","bindType":"bind","bindEventType":"click",actionList:[{"eventResult":true,"executeOnPageInit":false,"stopExecutionOnError":true,javascriptFunction:apex.da.showAlert,"attribute01":"Test","action":"NATIVE_ALERT"},
                        {"eventResult":false,"executeOnPageInit":true,"stopExecutionOnError":true,javascriptFunction:function (){ alert('count='+$("input:checked[type='checkbox'][name^='f01']").length)
                        },"action":"NATIVE_JAVASCRIPT_CODE"}]}]

                        You see that I have removed all conditions for the TRUE action,just an alert( 'Test') if the button is clicked.

                        Even the Alert won't display,but got to see the good part,I am learning a lot of new things. :D

                        The only thing that could be wrong is the triggeringButtonId, but I've checked its the correct button UPDATE_COLLECTION,
                        and anyway the Dynamic Action branch is below it,so that's good.

                        The Action when button is clicked is "Defined by Dynamic Action", Validations :YES

                        Maybe one last try.

                        Leckraj
                        • 9. Re: disabling a submit button if no rows checked in tabular form
                          kvlek
                          Hi Leckraj,

                          Have you checked Tom's remark (thanks Tom i forgot to mention this).
                          >
                          Tom wrote:
                          Did you change the "Action when clicked" property of the button? If it is still set to "Submit page" then the dynamic action will mean nothing as the button's action is still executed. Have you set it to "defined by dynamic action"?
                          >

                          If everything is as you say, it should work. Is there another DA active which could be "in the way"? Check with the debug option and the + browser console+ what is fired and what not.

                          Another option you have is to give the button an static id use this id as an jQuery selector to trap your DA. if you use as static id BTN_UPDATE_COLLECTION then you selector would be
                           [id=BTN_UPDATE_COLLECTION]
                          This should not be necessary! But it could help you to test some things.

                          Regards,
                          Kees Vlek
                          -----
                          Company: http://www.orcado.nl
                          Blog: http://www.orcado.nl/blog/blogger/listings/69-kvlek
                          Twitter: http://www.twitter.com/skier66
                          If the question is answered please change it to answered and mark the appropriate post as correct/helpfull.
                          • 10. Re: disabling a submit button if no rows checked in tabular form
                            leckj
                            Hi Kees,

                            That's an interesting problem no doubt, I've double-checked everything,and it
                            should really work,but does not.

                            I'll have to move on,but earlier I thought as much about maybe conflicting DAs,
                            I removed a plug-in there that I thought may have been the cause,but still no good.

                            I still have 3 DA that are more or less essential to the page,and when I have the
                            time,I'll remove them one by one and check,as well as using the static id.

                            One suspect is a DA on a second region on the page which always displays some time
                            after the first tabular form region.

                            So I used Laxshmi's suggestion of calling a Javascript function from the button,
                            although the function may just require one line using

                            $("input:checked[type='checkbox'][name^='f01']").length > 0 instead of looping.

                            Thanks for your help.

                            Leckraj
                            • 11. Re: disabling a submit button if no rows checked in tabular form
                              Tom Petrus
                              Leckraj,
                              Did you check your button action then?
                              It really should work as Kees has described. And while I'm sure that putting in javascript in the url to call a function would work, I'm not sure why you'd want to do that. Dynamic actions are so great because they also make it so obvious that there is some javascript at work, and is shown on the item itself and in the dynamic actions of the tree view of the page. That makes it so convenient and greatly aids in maintaining a page. I'm not sure what you are missing here, and I highly doubt that you'd actually have a conflicting DA as you are just implementing this. If you changed the button to call a function instead of the DA, and that works, I see almost no reason why the DA would not work provided you set the correct options.
                              You could always upload your application to apex.oracle.com and provide us with developer credentials, and we might see at a glance what went wrong, and would save a lot of writing and guessing.
                              • 12. Re: disabling a submit button if no rows checked in tabular form
                                leckj
                                Hi Tom,

                                I did ensure that the button was set to "Defined by Dynamic Action" each time I ran through the checks.

                                As someone starting out on all things browser based, I want to use best practices from the start.

                                Which is why I tried the Dynamic Action method first. :D

                                I'll check and rerun the whole thing once more and then if still not working,I'll set the app in apex.oracle.

                                Thanks

                                Leckraj