Question

I have a web2py form created via SQLFORM. There is a section of the form that is optional and therefore I want it to be hidden from display by default and only shown when the user checks a checkbox on the form.

What I am doing now is setting the various input form rows to be display none in the controller:

form.element('#mytable_myfield__row')['_style'] = 'display: none'
...

and then in my veiw:

{{extend 'layout.html'}}
<script>
jQuery(document).ready(function($){

    $('#mytable_is_something').change(function() {
        $('#mytable_myfield__row').toggle();
            ...
    });       

});
</script>
<h1>Create Something</h1>
{{=form}}

(where #mytable_is_something is the id of my checkbox)

  1. Is this the best way to do this?
  2. Since I have several fields whose visibility I want to toggle, is there a way to group them into a DIV and then just toggle the visibility of that div?
Was it helpful?

Solution

  1. Is this the best way to do this?

Seems reasonable, though might be cleaner if you move the CSS to a block in the view (or a separate stylesheet) rather than inline.

  1. Since I have several fields whose visibility I want to toggle, is there a way to group them into a DIV and then just toggle the visibility of that div?

Probably the best way would be to create a custom form layout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top