Use auto complete without SQL

75 views
Skip to first unread message

Seraaj Muneer

unread,
Jan 17, 2016, 6:33:56 PM1/17/16
to web...@googlegroups.com
Hello everyone from Accra Ghana.

I've a small issue I'm not sure how to go about.

I've a number of companies in the database. What I want to do is create a form with just a select field containing the companies in the database, and naturally a submit button to post the form to a controller. How do I use the Autocomplete widget outside of SQLForm? Is it even possible? If not, how do I represent the list of companies in HTML in a way that the user will not have to scroll endlessly to select a company as would be the case with a traditional HTML select? 

Also every company entity in the database has a boolean field, 'is_default', now how do I make sure only one company has 'is_default' set to True? So all others have it as False? Also if upon inserting or updating a company, the new or updated record has 'is_default' set to True, then I want to set that of all other companies as False. 




Thanks


DenesL

unread,
Jan 18, 2016, 11:51:44 AM1/18/16
to web2py-users
Hello Seraaj

1) You can still use SQLFORM if you want, just preset the widget and use the fields parameter of SQLFORM to filter:

def someaction():
  db
.table.fieldX.widget = SQLFORM.widgets.autocomplete(request, ...)
  form
= SQLFORM(db.table, fields=['fieldX',], ...)
 
if form.validate():
   
# do something with the form's data


or you can use SQLFORM.factory:

 form = SQLFORM.factory( db.table.fieldX.clone(...) )

2) Set the default for the is_default field to False
and check if there is a record in the db with is_default==True and a different id that the current insert/update in the accepted portion of your form processing, and set it to False.

Denes.

Seraaj Muneer

unread,
Jan 18, 2016, 1:38:28 PM1/18/16
to web...@googlegroups.com
Thanks. I decided against the first issue because as per the application I am porting from Java EE to web2py, there'd be some overheads with that route.

With regards to the second issue, I added a function to the insert and update hooks to update any company set that is set as default if the new record to be inserted has is_default set to true.

def set_all_default_company_to_false(fields):
if fields.get('is_default'):
rows = db(query).select(db.company.id)
for row in rows:
db(db.company.id == row.id).update(is_default=False)

Reply all
Reply to author
Forward
0 new messages