문제

I'd like to generate a form from multiple tables using SQLFORM.factory, and I'd like the rows in the form to be labeled with classes corresponding to the tables from which they were generated. For example:

SQLFORM.factory(db.table1, db.table2)

and the accompanying code to generate classes, should generate something like this:

...
<tbody>
    <tr id = 'no_table_field1__row' class = 'table1_field'></tr>
    <tr id = 'no_table_field2__row' class = 'table1_field'></tr>
    <tr id = 'no_table_field1__row' class = 'table2_field'></tr>
    <tr id = 'no_table_field2__row' class = 'table2_field'></tr>
</tbody>
...

By default, it seems that the ids generated by SQLFORM.factory start with "no_table_".

Is there a quick and easy way to generate classes, or at least to have the auto-generated names of the ids correspond to the table names? Thank you.

도움이 되었습니까?

해결책

There isn't really an easy way to do what you want with SQLFORM.factory. You can change "no_table" to something else via the table_name argument, but that will affect all the form input fields. If you want to use SQLFORM.factory (as opposed to generating a custom form), then you'll have to use the server-side DOM to manipulate the form object after it has been created. Something like:

form = SQLFORM.factory(db.table1, db.table2)
for f in [f for f in db.table1] + [f for f in db.table2]:
    if f.type != 'id' and f.readable and f.writable:
        form.element('tr[id=no_table_%s__row]' % f.name).add_class(
            '%s_%s' % (f.tablename, f.name))

The above iterates through all the Field objects in the two tables, and for each one, it finds the associated TR element in the form based on the id and then adds a class to the TR.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top