jQWidgets Forums

jQuery UI Widgets Forums Editors CheckBox, RadioButton How to enumerate radiobuttons in Radiogroup ?

This topic contains 9 replies, has 3 voices, and was last updated by  dima72 10 years ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author

  • dima72
    Participant

    Dear All,

    I want to dynamically enumerate radiobuttons in radiogroup in order to get selected option.
    I’m doing something like:

    `function GetRadiogroupOption( groupname )
    {
    //var Radiogroup = document.getElementById(groupname);
    var children = $( “#”+groupname).children();
    for(var i=0;i<children.length;i++)
    {
    var checkbox = children[i];
    if( checkbox.jqxRadioButton( “checked”) )
    return “checked”;
    }
    return “none”;
    }`

    but it does not really work in fact. Please suggest correct way to get children of Radiogroup.


    ivailo
    Participant

    Hi dima72,

    I made a bit different demo to solve your problem.
    In this example you can just add your dynamically create radio buttons in array, and use it to detect on checked event.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title id='Description'></title>
        <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
        <script type="text/javascript" src="../../scripts/jquery-1.11.1.min.js"></script>
        <script type="text/javascript" src="../../scripts/demos.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxradiobutton.js"></script>
    
        <style>
            .jqx-group-button-pressed.jqx-success {
               background-color: #ff0000 !important;
                color: #aaaaaa !important;
                background-image: none !important;
            }
        </style>
    
    </head>
    <body class='default'>
        <div id='content'>
            <script type="text/javascript">
                $(document).ready(function () {
    
                    $("#jqxRadioButton1").jqxRadioButton({
                        width: 120,
                        height: 25,
                        theme: 'energyblue',
                        groupName: "Group"
                    });
                    $("#jqxRadioButton2").jqxRadioButton({
                        width: 120,
                        height: 25,
                        theme: 'energyblue',
                        groupName: "Group"
                    });
                    $("#jqxRadioButton3").jqxRadioButton({
                        width: 120,
                        height: 25,
                        theme: 'energyblue',
                        groupName: "Group"
                    });
                    $("#jqxRadioButton4").jqxRadioButton({
                        width: 120,
                        height: 25,
                        theme: 'energyblue',
                        groupName: "Group"
                    });
    
                    //You must create dynamically array with the actual radio buttons
                    var checkBoxesArray = ["#jqxRadioButton1", "#jqxRadioButton2", "#jqxRadioButton3", "#jqxRadioButton4"];
                    var checkBoxes = checkBoxesArray.toString();
    
                    // Detect the checked button from the group
                    $(document).on('checked', checkBoxes, function () {
                        //Your code here
                        var nowIsChecked = $(this).attr("id");
                        alert("You checked the button" + nowIsChecked);
                    });
                   
    
                });
    
            </script>
            <div>
                <div id='jqxRadioButton1'>Radio 1</div>
                <div id='jqxRadioButton2'>Radio 2</div>
                <div id='jqxRadioButton3'>Radio 3</div>
                <div id='jqxRadioButton4'>Radio 4</div>
            </div>
        </div>
    </body>
    </html>

    Best Regards,
    Ivailo Ivanov

    jQWidgets Team
    http://www.jqwidgets.com


    dima72
    Participant

    having not found solution for my problem for a while I stick to this:

               
                //returning name of selected radiobutton  
                function GetCheckedRadiobuttonName( radiobutton1, radiobutton2, radiobutton3, radiobutton4, radiobutton5, radiobutton6 )
                {
                   if( radiobutton1 !== null && radiobutton1.jqxRadioButton( 'checked')  )
                     return radiobutton1.attr("id"); 
                   else if( radiobutton2 !== null && radiobutton2.jqxRadioButton( 'checked') )
                     return radiobutton2.attr("id"); 
                   else if( radiobutton3 !== null && radiobutton3.jqxRadioButton( 'checked') )
                     return radiobutton3.attr("id"); 
                   else if( radiobutton4 !== null && radiobutton4.jqxRadioButton( 'checked') )
                     return radiobutton4.attr("id"); 
                   else if( radiobutton5 !== null && radiobutton5.jqxRadioButton( 'checked') )
                     return radiobutton5.attr("id"); 
                   else if( radiobutton6 !== null && radiobutton6.jqxRadioButton( 'checked') )
                     return radiobutton6.attr("id"); 
                }
                //passing name of selected radiobutton - "checked_id"
                function SetRadioGroupChecked( checked_id, radiobutton1, radiobutton2, radiobutton3, radiobutton4, radiobutton5, radiobutton6 )
                {               
                   if( ( radiobutton1 !== null ) && ( radiobutton1.attr("id") == checked_id ) )
                     radiobutton1.jqxRadioButton('check');
                   else if( ( radiobutton2 !== null ) && ( radiobutton2.attr("id") == checked_id ) )
                     radiobutton2.jqxRadioButton('check');
                   else if( ( radiobutton3 !== null ) && ( radiobutton3.attr("id") == checked_id ) )
                     radiobutton3.jqxRadioButton('check');
                   else if( ( radiobutton4 !== null ) && ( radiobutton4.attr("id") == checked_id ) )
                     radiobutton4.jqxRadioButton('check');
                   else if( ( radiobutton5 !== null ) && ( radiobutton5.attr("id") == checked_id ) )
                     radiobutton5.jqxRadioButton('check');
                  else if( ( radiobutton6 !== null ) && ( radiobutton6.attr("id") == checked_id ) )
                     radiobutton6.jqxRadioButton('check');
    
                }

    it allows to get and set option in radiogroup by radiobutton name. up to 6 radiobuttons supported, but that is enough for most cases.


    dima72
    Participant

    Dear Ivailo,

    thank you for support and excellent components.
    in brief my problem is to $.post from modal form many radio-options with Ajax.

    for that i need to obviously mention every radiogroup and write particular code to get option from it.

    to lesser amount of my code i’m going to do like this:

    $.post( "post_profilesettings.php", 
                        { Run_Every_Day_Option : GetCheckedRadiobuttonName( $("#Run_Every_Day_Radio_Mode"), $("#Repeat_after_Radio_Mode"),       $("#Repeat_monthly_Radio_Mode"), $("#Run_only_Once_Radio_Mode"), null, null )...... 
    

    this approach takes atleast one line of code for every radiogroup.
    I’m not sure if it possible in Javascript atall, but i would rather use a method to post (without refresh of page) all controls values in single line of code. Unfortunately standard HTML Form method does not allow to work with modal jqxWindow.

    thanks for support,


    ivailo
    Participant

    Hi dima72,

    With $.post you can insert an object with multiple key/value pairs. So you can fill that object with the values of all radio groups. You don’t need to send values about not selected buttons. Just create an object contains one value for every radio group. You don’t need to send multiple lines of post requests.

    You can see how to do that from the official page:
    http://api.jquery.com/jQuery.post/

    Best Regards,
    Ivailo Ivanov

    jQWidgets Team
    http://www.jqwidgets.com


    dima72
    Participant

    Dear Ivailo,

    >You don’t need to send values about not selected buttons.
    GetCheckedRadiobuttonName returns only name of selected radiobutton.

    Can you please suggest if jqWidgets has some technology like DataBinding in other languages.
    What i want is to associate controls with XML attributes once in code initialization and have DataBinding do all the rest job for mapping and posting.

    for now i have to mention attribute name 3 times. 1) when mapping from Source 2) when reading value to control 3) when posting value

    thanks,


    ivailo
    Participant

    Hi dima72,

    You can try to use our widget jqxDataAdapter. It’s the option about DataBinding right now.
    Here is it’s documentation:
    http://www.jqwidgets.com/jquery-widgets-documentation/documentation/jqxdataadapter/jquery-data-adapter.htm

    Best Regards,
    Ivailo Ivanov

    jQWidgets Team
    http://www.jqwidgets.com


    dima72
    Participant

    jqxDataAdapter is good, but it can be bind to jqxGrid only.
    what I would looking for is how to bind other visual controls to jqxDataAdapter


    Peter Stoev
    Keymaster

    Hey dima72,

    jqxDataAdapter is used for binding in jqxTree, jqxMenu, jqxInput, jqxTreeGrid, jqxGrid, jqxDataTable, jqxListBox, jqxDropDownList, jqxComboBox, jqxChart, jqxTreeMap. So I would suggest you to look at its documentation page which is pointed out by my colleague. When data is loaded, it’s stored in the “records” member of jqxDataAdapter.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com/


    dima72
    Participant

    Yes, I can see source param in jqxInput
    $(“#jqxInput”).jqxInput({ source: dataAdapter

    but that facility IMHO only for purpose to display popup datalist in order to simplify data input.

    But I can not find simple example of displaying value of current record in Dataset for jqxInput.
    most of my problem ( and probably everybody ) is to display on a form values from current record in jqxGrid.

    thanks for understanding,

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.