0

need a hint on web2py built-in autocomplete widget.

in controller: db.otc_products.counterparty.widget = SQLFORM.widgets.autocomplete(request, db.counterparty.Long_Name, limitby=(0,10), min_length=2)

in model:

db.define_table('otc_products', Field('counterparty','reference counterparty', widget = SQLFORM.widgets.autocomplete(request, db.counterparty.Long_Name, id_field=db.counterparty.id, limitby=(0,20), min_length=2 )), ...

db.define_table('counterparty', Field('Long_Name'), ...

The resulted sqlform.grid returns a grid which features the counterparty field to be a regular table field instead of a widget, let alone autocompelete widget.

What is the configuration that needs to modify here to get a autocomplete widget shown and work.

thanks in advance! check link above for a snapshot of tge grid.

  • You say the grid contains "a regular table field instead of a widget". But the grid is supposed to show the data stored in the database records. Widgets are for forms, not the grid. The autocomplete widget is for filling in an empty form field with a single value taken from an entire column of a database table. A cell in the grid, on the other hand, is for displaying the existing value of a particular field in a particular record. Are you saying you want to be able to edit records in-line in the grid? If so, note that the grid does not include such functionality. – Anthony Jan 28 '16 at 18:13
  • @Anthony, so the autocomplete widget is not avail to sqlform.grid? is this certain. on the widget level, most widget do support grid however, as in my snapshot, you can see other columns except counterparty are in-line edits, dela_id is a StringWidget, the second column is a OptionWidget.those were defined in my controller like below and they really work for grid: db.otc_products.DealID.represent = lambda value,row: string_widget(db.otc_products.DealID,value, **{'_name':'DealID_row_%s' % row.id}) – Steven Zhou Jan 29 '16 at 01:50

2 Answers2

0

The .widget attribute of a Field object produces the HTML form input element used for the field in forms created via SQLFORM. The grid is for displaying values stored in the database, not for editing those values. The display of a given field in the grid is therefore determined by the field's .represent attribute, not by the .widget attribute.

As you have mentioned in your comment above, you can use a form widget within the .represent attribute, and the grid will then display the widget. However, in both your model and controller code above, you have set the .widget attribute of the counterparty field rather than the .represent attribute.

In any case, there is really no point to using form widgets within the grid itself, because the grid provides no mechanism for making inline updates to records via those widgets (i.e., you will be able to select/input values in each widget, but you will have no way to submit the changes so they will be persisted in the database).

In theory, you could build your own inline editing functionality on top of the grid (via Javascript that reads the values from the inputs and posts the updated records to the server via Ajax). But this functionality is not built into the grid.

The grid does provide a way to edit individual records. If you configure the grid to be editable, each row will include an "Edit" button, and when you click it, you will be taken to an edit form on a separate page. That edit form will make use of any widgets you have defined, including the autocomplete widget you have configured in your code.

Anthony
  • 25,466
  • 3
  • 28
  • 57
  • what you stated is correct, it helps me to understand the widgets, after a thurough readin of sqlhtml.py file. what I am trying to code is a system containing a complicated structure in bookkeeping as well as CRM, so the user is asking for an in-line autocomplete. I will have to resort to javascript/css to render this on the grid, in fact the grid is what they love to see and work on. – Steven Zhou Jan 31 '16 at 15:02
0

For web2py and stackoverflow communities and the sake of completeness, developer who is leveraging this framework and streamlining the web development:

the sqlform.grid can be secondarily developed to extend its capacity in some ways, just need to be very familiar with the underlying .py files which is open source and modifiable to meet your needs.

folks have posted solutions like below, with this one can in-line edit a gird, which provides convenient editing as well as comparing between sql records, the sql tables can be managed thru the grid with greater transparencies without jumping back and forth for an edit:

http://www.web2pyslices.com/slice/show/1928/basic-inline-editing-in-sqlformgrid-no-plugin-no-javascript

p.s.: I am utilizing web2py for faster development for a financial firm, so there is really no time for me to extend this framework to unclock additional functionalities, like the one above. when I do have some free time, if there is actual development need, I will extend web2py for particular requirement, and post the code on the web.