Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Radio & Checkbox Inputs #117

Open
heme opened this issue Oct 21, 2013 · 7 comments
Open

Radio & Checkbox Inputs #117

heme opened this issue Oct 21, 2013 · 7 comments

Comments

@heme
Copy link

heme commented Oct 21, 2013

Is there documentation on HTML form inputs somewhere? Specifically Checkboxes and Radio buttons. The code below is not working as expected for me. Thank you!

<html>
<head>
<meta charset="utf-8"/>
<title>Radio Button Tests</title>

    <script src="jquery.js"></script>
    <script src="transparency.js"></script>
    <script>
        var data = {
            radio: 'TWO',
            checkbox: ['THREE', 'FOUR'],
        };

        $(function () {
            $('#theForm').render(data);
        })
    </script>

</head>
<body>

<form id="theForm">
    <h3>Radio Buttons</h3>
    <input data-bind="radio" type="radio" name="radio" value="ONE" />ONE
    <br>
    <input data-bind="radio" type="radio" name="radio" value="TWO">TWO
    <br>
    <input data-bind="radio" type="radio" name="radio" value="THREE">THREE
    <br><br>

    <h3>Check Boxes</h3>
    <table cellpadding="10">
    <tbody><tr>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="ONE">ONE</td>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="TWO">TWO</td>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="THREE">THREE</td>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="FOUR">FOUR</td>
    </tr></tbody></table>

</form>

</body>
</html>
@grimaldodev
Copy link

Can you put the desired html please?

@heme
Copy link
Author

heme commented Oct 28, 2013

I would expect output HTML below by default. Again, if anyone knows of docs, examples, or special directives I would appreciate it.

JavaScript:

        var data = {
            radio: 'TWO',
            checkbox: ['THREE', 'FOUR'],
        };

        $(function () {
            $('#theForm').render(data);
        })

Input:

<form id="theForm">
    <h3>Radio Buttons</h3>
    <input data-bind="radio" type="radio" name="rdo" value="ONE" />ONE
    <br>
    <input data-bind="radio" type="radio" name="rdo" value="TWO" />TWO
    <br>
    <input data-bind="radio" type="radio" name="rdo" value="THREE" />THREE
    <br><br>

    <h3>Check Boxes</h3>
    <table cellpadding="10">
    <tbody><tr>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="ONE" />ONE</td>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="TWO" />TWO</td>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="THREE" />THREE</td>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="FOUR" />FOUR</td>
    </tr></tbody></table>
</form>

Output:

<form id="theForm">
    <h3>Radio Buttons</h3>
    <input data-bind="radio" type="radio" name="radio" value="ONE" />ONE
    <br>
    <input data-bind="radio" type="radio" name="radio" value="TWO" checked="checked" />TWO
    <br>
    <input data-bind="radio" type="radio" name="radio" value="THREE" / >THREE
    <br><br>

    <h3>Check Boxes</h3>
    <table cellpadding="10">
    <tbody><tr>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="ONE" />ONE</td>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="TWO" />TWO</td>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="THREE" checked="checked" />THREE</td>
    <td><input data-bind="checkbox" type="checkbox" name="chkbox" value="FOUR" checked="checked" />FOUR</td>
    </tr></tbody></table>

</form>

Thanks for any help you can provide!

@grimaldodev
Copy link

you are trying to use the tag name as id to insert the text and paramaters, si not posible, look the next example:

javascript for the form

var inputs = {
    'label1' : 'this is a text',
    'label2' : 'other one',
    'checked1': 'checked'
}

var directives = {
  'checked1': {
     'checked': function(){
        return this.checked1;
      }
   }
}

$('form').render(inputs, directives)

html markup

<form>
    <input type="checkbox" id="checked1"><label for="checked1" id="label1"></label>
    <input type="checkbox" id="checked2"><label for="checked2" id="label2"></label>
</form>

html output

<form>
    <input type="checkbox" id="checked1" checked="checked"><label for="checked1" id="label1">this is a text</label>
    <input type="checkbox" id="checked2"><label for="checked2" id="label2">other one</label>
</form>

@heme
Copy link
Author

heme commented Oct 28, 2013

Thanks @grimaldo! But...

  • I am trying to use the data-bind attribute set the checked status of radio/checkboxes.
  • I do not care about the label or its text.

I would expect the checkboxes, radio buttons to check themselves based on the data passed in. In my case the templates will have the labels hard coded in the HTML. I am not dynamically building the form, but I am dynamically "filling it in".

I have revamped my inputs/outputs:

JavaScript:

    <script>
        var data = {
            quantity: 3,
            colors: ['GREEN', 'YELLOW'],
        };
        $(function () {
            $('#theForm').render(data);
        })
    </script>

Input HTML

<form id="theForm">
    <h3>Quantity: Radio Buttons</h3>
    <input data-bind="quantity" type="radio" name="rdos" value="2" />2<br>
    <input data-bind="quantity" type="radio" name="rdos" value="3" />3<br>
    <input data-bind="quantity" type="radio" name="rdos" value="5" />5<br>
    <br>
    <h3>Colors: Check Boxes</h3>
    <input data-bind="colors" type="checkbox" name="chkbxs" value="RED" />RED<br>
    <input data-bind="colors" type="checkbox" name="chkbxs" value="GREEN" />GREEN<br>
    <input data-bind="colors" type="checkbox" name="chkbxs" value="BLUE" />BLUE<br>
    <input data-bind="colors" type="checkbox" name="chkbxs" value="YELLOW" />YELLOW<br>
    <br>
</form>

Expected Output HTML

<form id="theForm">
    <h3>Quantity: Radio Buttons</h3>
    <input data-bind="quantity" type="radio" name="rdos" value="2" />2<br>
    <input data-bind="quantity" type="radio" name="rdos" value="3" checked="checked" />3<br>
    <input data-bind="quantity" type="radio" name="rdos" value="5" />5<br>
    <br>
    <h3>Colors: Check Boxes</h3>
    <input data-bind="colors" type="checkbox" name="chkbxs" value="RED" />RED<br>
    <input data-bind="colors" type="checkbox" name="chkbxs" value="GREEN" checked="checked" />GREEN<br>
    <input data-bind="colors" type="checkbox" name="chkbxs" value="BLUE" />BLUE<br>
    <input data-bind="colors" type="checkbox" name="chkbxs" value="YELLOW" checked="checked" />YELLOW<br>
    <br>
</form>

@cfaheyCurveJumper
Copy link

Hi @heme ,

Did you ever find a solution for this? It works for me for checkboxes, but not for radios.

Thanks!

@cfaheyCurveJumper
Copy link

In case anyone else has this same struggle, adding a directive worked for me:

<script>
    var data = {
        quantity: 3,
        colors: ['GREEN', 'YELLOW'],
    };

    // THE DIRECTIVE
    var directive = {
        quantity: {
            checked: function() {

                // the element exists as the first argument
                var thisElem = arguments[0].element;

                // return true if the value of this element equals your quantity value
                return ( thisElem.value == this.quantity );
            }
        },
        colors: {
            checked: function() {

                var thisElem = arguments[0].element;

                // return true if the value of this element is in your array of colors
                return ( this.colors.indexOf( thisElem.value ) >= 0 );
            }
        }
    }

    $(function () {
        $('#theForm').render( data, directive ); // <=== USE THE DIRECTIVE
    })
</script>

@heme
Copy link
Author

heme commented Jun 16, 2015

It has been a while since I first posted this, and I'm no longer actively using transparency, but....

I don't think I wanted to write a directive for every radio/checkbox group in my app. I was hoping this could get baked into the framework in some way. I do realize checkboxes are used as a single input and as a group; I'm sure that presents a challenge.

Good on ya for jumping in here. I really enjoyed working with this lib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants