简体   繁体   中英

Adding columns to a Web2py table in a form

In my web2py application, in the controller I read from an external DB the names of students I want to take a register for. I loop through the resulting list adding the list elements to a new list.

for student in pupils_query:
    attendance_list.insert(counter, [student[0], student[1], student[2], student[3]])
    counter += 1
counter = 0

Then for each student I read their attendance codes for the day so far from another table, and append them to attendance_list:

for attendance_code in attendance_result:
        attendance_list[counter].append(attendance_code)

Now, I'm going to want to make a form from all this, using a table which will show each students' attendance code in a text input (so they can be updated if wrong), then have a dropdown for input of the current lesson code.

I'm using a FORM and TABLE helper to create the table in the form:

form=FORM(TABLE(*[TR(*rows) for rows in attendance_list]))

but can't seem to be able to add a new 'row' form item with something like:

            select = "SELECT("+ main_reg_list +")"
            attendance_list[counter].append(select)

where main_reg_list is dictionary of acceptable attendance codes (or of course, any other form input element).

In summary, I'm stuck adding new TDs to a table made with a TABLE helper from a list of lists. I bet I'm not the first person to overcome this problem.

I am still not clear about what you want. I think you want table of student information and in one column you want dropdown. Something similat to following image

在此处输入图片说明

Above form is created from following code. I hope following code will help you:

# controller/default.py

def index():
    # Dummy attendance list, list after appending attendance code
    attendance_list = [['stud_id_1', 'first_name_1', 'last_name_1', 'attendance_code_1'],
                       ['stud_id_2', 'first_name_2', 'last_name_2', 'attendance_code_2'],
                       ['stud_id_3', 'first_name_3', 'last_name_3', 'attendance_code_5'],
                       ['stud_id_4', 'first_name_4', 'last_name_4', 'attendance_code_4']]

    possible_att_code = ['attendance_code_1', 'attendance_code_2', 'attendance_code_3', 'attendance_code_4', 'attendance_code_5']

    # initialise form_rows with Table heading
    form_rows = [THEAD(TR(TH('ID'), TH('First Name'), TH('Last Name'), TH('Attendence Code')))]

    for attendance in attendance_list:
        attendance_code_dropdown = _get_dropdown(attendance[0], attendance[3], possible_att_code)

        td_list = [TD(attendance[0]), TD(attendance[1]), TD(attendance[2]),
                   TD(attendance_code_dropdown)]
        table_row = TR(td_list, _id='row_' + attendance[0])
        form_rows.append(table_row)

    # Form submit button
    form_rows.append(TR(INPUT(_type='submit')))
    form = FORM(TABLE(*form_rows), _name='student_attendance',
                _id='student_attendance')

    if form.accepts(request, session):
        # Write code to update record
        pass
    return dict(form=form)


def _get_dropdown(stud_id, att_code, possible_att_code):

    option_list = []
    for pac in possible_att_code:
        if pac == att_code:
            option_list.append(OPTION(pac, _value=pac, _selected='selected'))
        else:
            option_list.append(OPTION(pac, _value=pac))

    return SELECT(*option_list, _name=stud_id)

<!-- views/default/index.html -->
{{extend 'layout.html'}}

{{=form}}

Are my assumptions correct? or you want any thing else? Comment if didn't understood code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM