Skip to content

Bug: Sorting Groups with Radio Buttons (With Solution) #152

Closed

Description

There is a bug when sorting Groups that have radio button input fields.
They way you sort is by swapping the values of the input fields between the two groups. You're attempting to treat radio buttons the same as checkbox by doing this in your cmb.shiftRows function:

            else if ( 'checkbox' === $element.attr('type') || 'radio' === $element.attr('type') ) {
                inputVals[ index ]['$'].prop( 'checked', $element.is(':checked') );
                $element.prop( 'checked', inputVals[ index ]['val'] );
            }

Because you're iterating over each input element, you can't swap the checked property one-for-one with radio buttons, because when you check a radio button from a group, the dom/browser unchecks the others within the radio button group. Example:

Group 1    [ ] A  [X] B   [ ] C
Group 2    [X] A  [ ] B   [ ] C

On the first iteration, the checked property for A is set in the first group but that unchecks B:

Group 1    [X] A   [ ] B   [ ] C
Group 2    [ ] A   [ ] B   [ ] C

Now on the second iteration, B is unchecked in Group 1, so B will never get checked for Group 2.

To solve this, I saved the checked states in a temporary attribute and then set the checked property at the end:

        // And swap them all
        $goto.find( cmb.repeatEls ).each( function( index ) {

            ...

            // handle checkbox swapping
            else if ( 'checkbox' === $element.attr('type') ) {
                inputVals[ index ]['$'].prop( 'checked', $element.is(':checked') );
                $element.prop( 'checked', inputVals[ index ]['val'] );
            }
            // handle radio swapping
            else if ( 'radio' === $element.attr( 'type' )  ) {
                if ($element.is(':checked')) {
                    inputVals[index]['$'].attr('data-checked', 'true');
                }
                if (inputVals[index]['$'].is(':checked')) {
                    $element.attr('data-checked', 'true');  
                }
            }

            ...

        });

        $parent.find("input[data-checked=true]").prop('checked', true).removeAttr('data-checked');
        $goto.find("input[data-checked=true]").prop('checked', true).removeAttr('data-checked');

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions