This discussion is archived
10 Replies Latest reply: Mar 8, 2013 2:54 AM by xarg-Xrc RSS

Checking if a checkbox is checked

xarg-Xrc Explorer
Currently Being Moderated
Hi All,

I have a tabular form created manually where I use apex_item.checkbox2() function to create a checkbox.

In my page process how can I work out if this item is checked or not? For example, my checkbox corresponds to f03 (#3 from left, after a textfield and a date picker which corresponds to f01 & f02 respectively), and I have seen some people use this way (not sure what they are doing):
for i in 1..apex_application.g_f01.count loop
     if apex_application.g_f03( apex_application.g_f01(i) ) == 'Y' then 
          -- do something
     end if;
end loop;
I look in the page source in firebug and see there is a checkbox with name="f01" with the value I set in the param of apex_item.checkbox2(1,l.line_no) but there is also a f01 in the same < td > tag like this:
<input name="fo1" type="checkbox" UNCHECKED="" VALUE="2" />
<input name="f03" id="f03_002" type="hidden" value="124482038" />
My Apex version is 4.1.1 (cloudy theme) on IE7.
  • 1. Re: Checking if a checkbox is checked
    Joel_C Pro
    Currently Being Moderated
    xarg-Xrc wrote:
    Hi All,

    I have a tabular form created manually where I use apex_item.checkbox2() function to create a checkbox.

    In my page process how can I work out if this item is checked or not? For example, my checkbox corresponds to f03 (#3 from left, after a textfield and a date picker which corresponds to f01 & f02 respectively),
    ...
    I look in the page source in firebug and see there is a checkbox with name="f01" with the value I set in the param of apex_item.checkbox2(1,l.line_no) but there is also a f01 in the same < td > tag like this:
    <input name="fo1" type="checkbox" UNCHECKED="" VALUE="2" />
    <input name="f03" id="f03_002" type="hidden" value="124482038" />
    According to that code F01 is the checkbox and f03 is a hidden item - you should be able to tell this by examining the query you wrote - what number did you pass into the call to apex_item.checkbox2()?

    In any case, to go over the code you quote:
    for i in 1..apex_application.g_f01.count loop
         if apex_application.g_f03( apex_application.g_f01(i) ) == (sic) 'Y' then 
              -- do something
         end if;
    end loop;
    In a tabular form, apex holds the values of each column in arrays labelled F0n, where n is the tabular column number. e.g. F03 is the 3rd tabular form column. For most tabular form items, the size of this array corresponds to the number of rows on the page. So if 15 rows are presented, each array will have a cardinality (i.e. size) of 15.

    Checkbox items are treated slightly differently, in that the cardinality of the array is determined by the number of items selected when the page is submitted. e.g. if you checked the checkboxes in rows 1, 3 and 7, the array would have an cardinality of 3, which is slightly less intuitive (but faster) than looping over all rows and testing for some "checked" boolean value.

    In the example above, the checkbox column (F01) will hold the ROWID (or apex equivalent) to identify the actual selected row. This is subsequently being passed in as the index of the array f03, in order to get the required array element for that particular row. Note that for the test in that code (the "= 'Y'" bit) the value held in the hidden item F03 is the important part - there isn't a direct correlation between what that value is and the fact that it the row is checked.

    Edited by: Joel_C on 04-Mar-2013 15:17

    Just notice that you'd specified 1 as the checkbox - which then begs the question: what does F03 hold? I should add as well that the tabular form column names don't necessarily correlate with their absolute column position in your table - it is perfectly legal to have an item F09 in the first column and F01 in the 7th column etc, etc.

    Edited by: Joel_C on 04-Mar-2013 15:20
  • 2. Re: Checking if a checkbox is checked
    xarg-Xrc Explorer
    Currently Being Moderated
    Hi Thanks for explanation. Yep I muddled up witht he apex_item.checkbox2() passing 1 in first parameter, should be 3 as 1 & 2 is already used up.

    In my case f01 is the date field so not holding the rowid.

    When I check the debug via (apex_debug) its looping over all checkbox entries. Bit lost how I should be code the test of
    if apex_application.g_f03( apex_application.g_f01(i) ) = (sic) 'Y' then
    F03 is the checkbox item
  • 3. Re: Checking if a checkbox is checked
    xarg-Xrc Explorer
    Currently Being Moderated
    Just to add, when I add my apex_item.checkbox2(5,l.req_line_id) for example, the underlying checkbox input elment does not have an id like: f05_001 etc. So by adding:
    select
    col1,
    col2
     apex_item.checkbox2(5,l.req_line_id) as req_line_id
    from table_name
    The underlying input elment looks like:
    <input type="checkbox" value="383931" name="f05">
    nothing with id attribute
  • 4. Re: Checking if a checkbox is checked
    Joel_C Pro
    Currently Being Moderated
    xarg-Xrc wrote:
    Hi Thanks for explanation. Yep I muddled up witht he apex_item.checkbox2() passing 1 in first parameter, should be 3 as 1 & 2 is already used up.

    In my case f01 is the date field so not holding the rowid.

    When I check the debug via (apex_debug) its looping over all checkbox entries. Bit lost how I should be code the test of
    if apex_application.g_f03( apex_application.g_f01(i) ) = (sic) 'Y' then
    F03 is the checkbox item
    You've kind of got it back to front in that case: the intention of the sort of example you provided previously is that you loop through all the elements in the checkbox array and pass that as the index to the desired apex item array (e.g. a textbox). This only works if the checkbox holds the current rowid or equivalent. In the above example, you have three problems:

    1. You are cycling through the wrong array - you should be looping over F03.
    2. Your checkbox doesn't contain valid array index - it looks like a key of some sort (req_line_id ?)
    3. Your "data" element is (probably) malformed

    In the case of 1. I bet you've probably hit an "index out of bounds" type error (can't remember the exact wording) - this is because (unless you select every visible checkbox in your table), the number of elements in the checkbox array will be less than the number of rows and - to compound the problem elements in the checkbox array won't necessarily align with the other arrays e.g.: F03(3) will almost certain not be equivalent to F01(3).

    In the case of 2., even if you had that code snipped in the correct order:
    apex_application.g_f01( apex_application.g_f03(i))
    It wouldn't evaluate properly, because F03(i) will contain a key value (e.g. 123456) instead of an array index, therefore for a given iteration of your loop (e.g. 3), the above might evaluate to:
    apex_application.g_f01(123456)
    ... which will give you another "index out of bounds" error.

    As for 3. well you said in your post that it's a Date picker, but in your code example, you are comparing to a string "Y" - again, you'll probably hit a "data type mismatch" error.

    sigh okay... so where to from here?

    My (admittedly) radical solution would be to avoid using tabular forms, or at least only the minimum amount. Ask yourself if the users are really likely to want to amend data in this manner - would a "master/detail" style form be more appropriate?

    You could still make use of a minimal tabular form which only contains a checkbox element, which then contains the primary key of the elements you want to manipulate.
  • 5. Re: Checking if a checkbox is checked
    xarg-Xrc Explorer
    Currently Being Moderated
    Joel_C wrote:
    2. Your checkbox doesn't contain valid array index - it looks like a key of some sort (req_line_id ?)
    It uniquely identifies in item in the tabular form (not null). Stands for requisition line id.


    I have tried looping over f03 (the checkbox array) but no luck.

    I have (kind of) reproduced the problem in apex.oracle

    http://apex.oracle.com/pls/apex/f?p=36987:1

    workspace: aiworskpace
    username:test
    password:test
    App: 36987

    As you can see, if I check TURNER or KING (one or 2 items) it will just pick the top most. Where as if I check everything It works fine.

    All I want is to get the ID (or empno in my case ) of every checked items.

    As for tabular form, well the use case is people do shopping and in the end they view their shopping in a list (line items, price etc).
    And then they need tick box which after pressing will flick an email. So tabular form (much as hate it) is needed unfortunately as users want it. Basically it was a report and when you add check box , date field, quantity (number field) it became a manual tabular form.

    Many thanks for your help & suggestion.

    Edited by: xarg-Xrc on Mar 5, 2013 3:36 AM
  • 6. Re: Checking if a checkbox is checked
    Joel_C Pro
    Currently Being Moderated
    xarg-Xrc wrote:
    Joel_C wrote:
    2. Your checkbox doesn't contain valid array index - it looks like a key of some sort (req_line_id ?)
    It uniquely identifies in item in the tabular form (not null). Stands for requisition line id.


    I have tried looping over f03 (the checkbox array) but no luck.

    I have (kind of) reproduced the problem in apex.oracle

    http://apex.oracle.com/pls/apex/f?p=36987:1

    workspace: aiworskpace
    username:test
    password:test
    App: 36987

    As you can see, if I check TURNER or KING (one or 2 items) it will just pick the top most. Where as if I check everything It works fine.

    All I want is to get the ID (or empno in my case ) of every checked items.

    As for tabular form, well the use case is people do shopping and in the end they view their shopping in a list (line items, price etc).
    And then they need tick box which after pressing will flick an email. So tabular form (much as hate it) is needed unfortunately as users want it. Basically it was a report and when you add check box , date field, quantity (number field) it became a manual tabular form.

    Many thanks for your help & suggestion.

    Edited by: xarg-Xrc on Mar 5, 2013 3:36 AM
    Hello,

    Could you doublecheck the login credentials? Looks like a typo in the workspace name (although I tried it both ways).
  • 7. Re: Checking if a checkbox is checked
    xarg-Xrc Explorer
    Currently Being Moderated
    Hi sorry here it is:
    Workspace: aiworskspace
    Username: test
    Pass: test

    Thanks
  • 8. Re: Checking if a checkbox is checked
    xarg-Xrc Explorer
    Currently Being Moderated
    In mean time I have created a little workaround using a little javascript.

    Step 1: Create a page item to hold a colon delimited list of checked items


    Step 2: Put the following code in page header:
    function createdCheckedList()
    {
         var chkbox = document.getElementsByName('f05');
    
         var checkedValues = "";
    
         for (var i=0;i<chkbox.length;i++)
         {
    
                                  console.log('first time is '+chkbox.value);


              // check if checkbox or hidden
              if(chkbox[i].getAttribute("type")=="checkbox")
              {
                   // checked?
                   if(chkbox[i].checked)
                   {
                             // first checked item?
                             if(checkedValues == "")
                             {
                                  checkedValues = chkbox[i].value;
                             }
                             else
                                  checkedValues = checkedValues + ':' + chkbox[i].value;
                   }
              }
         }

         $x('P3_CHECKED_ITEMS').value = checkedValues;
         console.log($v('P3_CHECKED_ITEMS'));
         
         doSubmit('SUBMIT');
    }



    Step 3: In page process, instead of looping throught apex_application.g_f arrays, run the loop as below:
    DECLARE
    
         l_vc_arr2     APEX_APPLICATION_GLOBAL.VC_ARR2;
    
         x_ename varchar2(500);
    
    BEGIN
         l_vc_arr2 := APEX_UTIL.STRING_TO_TABLE(:P3_CHECKED_ITEMS);
         
         
              
         FOR i IN 1..l_vc_arr2.COUNT LOOP
              select ename into x_ename
              where          
              empno = CAST(l_vc_arr2(i) AS NUMBER)
                                    -- rest of your processing 
         
         END LOOP;
    
         :P3_CHECKED_ITEMS:= NULL;
    END;
  • 9. Re: Checking if a checkbox is checked
    Joel_C Pro
    Currently Being Moderated
    Hello,

    Sorry for the delay.

    I've created a trivial example (Page 2) partly based on your test - all it does it enter the dates (i.e. the contents of F01(i)) for rows where you check the boxes.

    If you switch on debug, check some boxes, then review the debug log, you should see the checked items.

    Regards,
  • 10. Re: Checking if a checkbox is checked
    xarg-Xrc Explorer
    Currently Being Moderated
    Thanks Joel, all making sense now.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points