How to change the location of the checkbox labels in SQLform

76 Views Asked by At

Is there an easy way to reposition the labels for checkboxes? I would like to show them on the left side in the same way as the other controls.

Form with checkbox labels

2

There are 2 best solutions below

0
On BEST ANSWER

Thank you Tim for your answer. That told me everything I needed to know. Here is what I had to do:

  1. Find the relevant function in the gluon\sqlhtml.py. For me it was formstyle_bootstrap4_inline_factory

  2. Copy the _inner function which is in the formstyle_boostrap4_inline_factory function into my application and rename it formstyle_myapp (putting it in a model file is probably the easiest)

  3. Copy the add_class function from gluon.sqlhtml and add it into formstyle_myapp function. This makes it visible to formstyle_myapp only.

  4. Change the default formstyle setting in db.py to point to the new function: response.formstyle = formstyle_myapp

  5. Finally, adjust the checkbox part of the formstyle_myapp to position the label on the left under all the other labels and the checkbox to the right under all the the other controls:

     elif controls['_type'] == 'checkbox' or controls['_type'] == 'radio':
         _controls = DIV(controls, _class="%s" % col_class)
    
0
On

You just need to make your own formstyle (copying formstyle_bootstrap3_inline_factory, which I'm guessing you're using)

Then cut out the rule treating checkboxes differently:

elif controls['_type'] == 'checkbox':
                label['_for'] = None
                label.insert(0, controls)
                label.insert(1, ' ')
                _controls = DIV(DIV(label, _help, _class="checkbox"),
                                _class="%s %s" % (offset_class, col_class))
                label = ''