How to sort a smartgrid with your own custom sorted id list

48 views
Skip to first unread message

rāma

unread,
Jun 18, 2020, 11:38:39 AM6/18/20
to web...@googlegroups.com
Hi all,

How to sort a smartgrid with your own custom sorted id list?

Say I have a id list after executing the query and I have a sorting functions that returns a sorted id list, how would I make smartgrid to commit to the ordering instead of sorting by ids by default?

Thanks,
rama

AGRogers

unread,
Jun 22, 2020, 6:48:54 PM6/22/20
to web...@googlegroups.com
The only thing that comes to mind is to put your sort data into the database so you have something to sort on. If it is a single user app then it's easy - add a new column called sortid and put your custom sort order their.

Multi user needs a second table to join on which might mess with smart grid. 

If you are not trying to display a lot of data you could make a copy of the table in memory using your order and give that to smart grid.

You can also modify (for that one call) the actual table. I am not sure how that works for adding fields though. Perhaps you could add a field and put your data in. It might magically disappear after the function closes. Or you could drop the field manually if it didn't. 

On Fri, 19 Jun 2020, 1:38 am rāma, <ranje...@gmail.com> wrote:
Hi all,

How to sort a smartgrid with your own custom sorted id list?

Say I have a id list of the query and I have a sorting functions that returns a sorted id list, how would I make smartgrid to commit to the ordering instead of sorting by ids by default?

Thanks,
rama

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/a078772b-0d41-4993-8df7-b9e5c223e9d0o%40googlegroups.com.

Serge Bourgeois

unread,
Jun 24, 2020, 6:43:43 AM6/24/20
to web2py-users
Have you tried the orderby option of the smartgrid?
Example of conctent:
orderby = dict(t_prj_doc_item= [ db.t_prj_doc_item.f_sequence, db.t_prj_doc_item.id]),

Alex Beskopilny

unread,
Jul 3, 2020, 10:11:52 AM7/3/20
to web2py-users
 Hi!
some working solution

1 build a sqlite-table in memory, as required
2 insert data in the table
3 show table in smartgird

def pro_report1():

    tbl = 'proverka'
    l = [e for e in db[tbl].fields if e in f_short_proverka ]
    memf = [ Field('oid', 'integer', label='П id', default= 0) ] + \
           [ Field(db[tbl][e].name, db[tbl][e].type, label = db[tbl][e].label,
             length= db[tbl][e].length, represent= db[tbl][e].represent  )
             for e in l ]

    dbmem = DAL('sqlite:memory')
    dbmem.define_table('mem_table',*memf)

    records = db(db[tbl].id>0).select().as_list()
    for r in records:
        r['oid'] = r['id']
        dbmem.mem_table.insert(**dbmem.mem_table._filter_fields(r))
    flds= [ db[tbl][e] for e in f_short_proverka ]
    rows=db(db[tbl].id>0 ).select( *flds  )

    exportclasses=dict(
            csv_with_hidden_cols=False,
            csv=False,
            xml=False,
            json=False,
            tsv_with_hidden_cols=False,
            tsv= False,
            )

    dbmem.mem_table.id.readable = False
    grid = SQLFORM.grid(dbmem.mem_table, create=False, editable=False, exportclasses= exportclasses,
            deletable=False, buttons_placement = 'left', showbuttontext=False)
    return dict(rows=rows, l=l, records=records, grid=grid)


четверг, 18 июня 2020 г., 18:38:39 UTC+3 пользователь rāma написал:

Alex Beskopilny

unread,
Jul 3, 2020, 10:22:36 AM7/3/20
to web2py-users
smartgrid sort with three one to many tables

def uvidomlen_all():
    table=db.operacia

    gqu = lambda tnm : (tnm.id>0) if auth.user.is_admin else (tnm.created_by == me)
    oqu = lambda tnm : ~db[tnm].id

    constraints = {'operacia':gqu(db.operacia), 'os1doc':gqu(db.os1doc), 'os2doc':gqu(db.os2doc), 'os3doc':gqu(db.os3doc)}
    orderby = {'operacia':oqu('operacia'), 'os1doc':oqu('os1doc'), 'os2doc':oqu('os2doc'), 'os3doc':oqu('os3doc')}

    return dict(grid=SQLFORM.smartgrid(db.operacia,
           deletable = False, #constraints = dict(povidomlen=query),
           editable = auth.user.is_admin,
           orderby= orderby,
           constraints= constraints,
           linked_tables= ['os1doc','os2doc','os3doc'] ,
           buttons_placement = 'left', user_signature=False,searchable=True, create=False, csv=False,showbuttontext=False,   ))



четверг, 18 июня 2020 г., 18:38:39 UTC+3 пользователь rāma написал:
Hi all,
Reply all
Reply to author
Forward
0 new messages