Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Edit multiple records at once

59 views
Skip to first unread message

fja

unread,
Dec 19, 2008, 11:05:31 AM12/19/08
to web2py Web Framework
Hello, is there a way to edit multiple records at once in a form?

Say you have a table

StudentName (string)
Term1 (string)
Term2 (string)

I would like to have a form similar to this.

Student Name Grade Term 1 Grade Term2
Roger A __
Cindy B __

[Submit]

I would like to be able to enter the grades for term 2 in one go (like
an Excel Sheet) and press submit to have it stored in the db.

Thank you

mdipierro

unread,
Dec 19, 2008, 12:30:53 PM12/19/08
to web2py Web Framework
You have to create the form using HTML and process the request.vars
manually.

Massimo

Iceberg

unread,
Dec 19, 2008, 12:49:29 PM12/19/08
to web2py Web Framework
I do not know an elegant way to do that.
But I just want to say, whatever trick you choose, do not try to use
more than one form in one page. I tried but did not work, probably due
to some builtin form id checking function to avoid multi-submit.

mdipierro

unread,
Dec 19, 2008, 1:00:54 PM12/19/08
to web2py Web Framework
You can have mutliple forms in a page but you need to give names to
forms

form1=FORM(...)
form2=FORM(...)
if form1.accepts(request.vars,session,formname='form1'): pass
if form2.accepts(request.vars,session,formname='form2'): pass

Massimo

Timothy Farrell

unread,
Dec 19, 2008, 1:42:03 PM12/19/08
to web...@googlegroups.com
If you're open to using a different Javascript framework...take a look at this:

http://www.satyam.com.ar/yui/2.6.0/assortedControls.html

It's a YUI datatable that allows for editing and server-side validation and saving.  I use this technique on two of my pages.  It's really cool.

-tim
-- 
Timothy Farrell <tfar...@swgen.com>
Computer Guy
Statewide General Insurance Agency (www.swgen.com)

Yarko Tymciurak

unread,
Dec 19, 2008, 4:21:02 PM12/19/08
to web...@googlegroups.com
That is cool.....

And (without looking into it too much) I've read yui grids are among the best layout options out there too...

Timothy Farrell

unread,
Dec 19, 2008, 4:27:20 PM12/19/08
to web...@googlegroups.com
I'm a fan!

Are you confusing YUI Grids with YUI Datatable (I'm not sure which one you're referring to here)?

YUI Grids:
http://developer.yahoo.com/yui/grids/

YUI Datatable:
http://developer.yahoo.com/yui/datatable/

Yarko Tymciurak

unread,
Dec 19, 2008, 4:40:15 PM12/19/08
to web...@googlegroups.com
no, not confusing so much... as noting one "source", one licencse type....

fja

unread,
Dec 19, 2008, 5:13:15 PM12/19/08
to web2py Web Framework
Thanks to everyone for their responses.

I am a VB and C# programmer for MS Windows Desktops, so I find myself
at a very steep learning curve here.

I have been using web2py for less than a day and am new to programming
with web frameworks. I like what I have seen so far with web2py and
would like to use the framework for future development.

The YUI components do look interesting, however I wouldn't know where
to begin using plugins at this stage.

I have included the very simple code I have done so far in hopes that
someone can point me in the right direction towards accomplishing
this. Articles and/or sample code would be greately appreciated.

Following the suggestion from Massimo, I have attempted a solution via
HTML, but I am not sure where to process the request.vars.

This is what I have so far....


MODEL - db.py

db.define_table('mytest', SQLField('studentname', 'string'), SQLField
('gradeterm1','string'), SQLField('gradeterm2','string'))


CONTROLLER - mytest.py

def index():
myform=SQLFORM(db.mytest,fields=['studentname'])
if myform.accepts(request.vars,session):
response.flash='record added'
myrecords=db().select(db.mytest.ALL)
return dict(form=myform, records=myrecords)


VIEW - mytest/index.html

{{extend 'layout.html'}}
<h1>mytest entry form</h1>
{{=form}}
<h2>Current Records</h2>
<table>
<tr>
<td>Student Name</td>
<td>Grade - Term1</td>
<td>Grade - Term2</td>
</tr>
{{for myrecords in records:}}
<tr>
<td>{{=A(myrecords.studentname)}}</td>
<td><INPUT type="text", name="grade1", value=
{{=myrecords.gradeterm1}}></td>
<td><INPUT type="text", name="grade2", value=
{{=myrecords.gradeterm2}}></td>
</tr>
{{pass}}
</table>
<INPUT type="submit" value="Save"/>



This code gives me a nice form where I can add new students to the db,
and presents me with a list of all students in the db with input boxes
for the 2 term grades for each student.

When I press the Save button after filling in the grades, nothing
happens.

My questions are

1. Upon pressing Save, who is receiving the data entered for the
grades?
2. How can I associate the grade entered in the input boxes with the
student in the row, so I can call the update functions to update the
grades for each student respectively.

Thank you for help and patience.

mdipierro

unread,
Dec 20, 2008, 2:26:20 AM12/20/08
to web2py Web Framework
In your code.

<td><INPUT type="text", name="grade1", value=
{{=myrecords.gradeterm1}}></td>
<td><INPUT type="text", name="grade2", value=
{{=myrecords.gradeterm2}}></td>

The two inputs are outside any form therefore nobody receives the
input. This is an HTML issue, not a web2py issue. You can try:

def index():
myform=SQLFORM(db.mytest,fields=['studentname'])
if myform.accepts(request.vars,session):
response.flash='record added'
myrecords=db().select(db.mytest.ALL)
myforms=[SQLFORM(db.mytest,r) for r in myrecords,fields=
['gradeterm1','gradeterm2'])
for f in myforms: f.accepts(request.vars,formname=str
(f.records.id))
return dict(form=myform, forms=myforms)

and

{{extend 'layout.html'}}
<h1>mytest entry form</h1>
{{=form}}
<h2>Current Records</h2>
<table>
<tr>
<td>Student Name</td>
<td>Grade - Term1</td>
<td>Grade - Term2</td>
<td></td>
</tr>
{{for f in forms:}}
<form>
<tr>
<td>{{=A(f.record.studentname)}}</td>
<td><INPUT type="text", name="gradeterm1", value=
{{=f.latest.gradeterm1}} /></td>
<td><INPUT type="text", name="gradeterm2", value=
{{=f.latest.gradeterm2}} /></td><td><input type="submit" /></td>
</tr>
{{=f.hidden_fields()}}
</form>
{{pass}}
</table>

mdipierro

unread,
Dec 20, 2008, 8:01:30 AM12/20/08
to web2py Web Framework
or better

def index():
myform=SQLFORM(db.mytest,fields=['studentname'])
if myform.accepts(request.vars,session):
response.flash='record added'
myrecords=db().select(db.mytest.ALL)
if request.vars.multiple_update:
for r in myrecords:
if request.vars.has_key('id%i_grade1' % r.id):
r.update_record(gradeterm1=request.vars['id%i_grade1'
% r.id],
gradeterm2=request.vars['id
%i_grade2' % r.id])
myrecords=db().select(db.mytest.ALL)
return dict(form=myform, records=myrecords)

and

{{extend 'layout.html'}}
<h1>mytest entry form</h1>
{{=form}}
<h2>Current Records</h2>
<form>
<input type='hidden' name='multiple_update' value="true" />
<table>
<tr>
<td>Student Name</td>
<td>Grade - Term1</td>
<td>Grade - Term2</td>
</tr>
{{for record in records:}}
<tr>
<td>{{=A(record.studentname)}}</td>
<td><INPUT type="text", name="id{{=record.id}}_grade1", value=
{{=record.gradeterm1}} /></td>
<td><INPUT type="text", name="id{{=record.id}}_grade2", value=
{{=record.gradeterm2}} /></td>
</tr>
{{pass}}
</table>
<input type="submit" />
</form>

fja

unread,
Dec 20, 2008, 4:18:31 PM12/20/08
to web2py Web Framework
Massimo, thank you so much. This solution worked perfectly!

Keep up the great work with web2py.
Reply all
Reply to author
Forward
0 new messages