If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!


  • 0
    cfhowes  14 years ago
    I'd like to add some things to dynamic_search(): limitby - improves performance on large tables, and orderby so that i can sort. Also, i added id and class to the input so i get the date-picker widget automagically for dates. There were a couple of changes i made since i'm using it on Google App Engine as well.
    def dynamic_search(table):
        """
        Build the input for for selection criteria, and runs a query
        based on the current criteria
        """
        tbl = TABLE()
        selected = []
        ops = ['equals','not equal','greater than','less than',
               'starts with','ends with','contains']
        if request.env.web2py_runtime_gae:
            ops = ['equals','not equal','greater than','less than']
    
        query = table.id > 0    
        for field in table.fields:
            chkval = request.vars.get('chk'+field,None)
            txtval = request.vars.get('txt'+field,None)
            opval = request.vars.get('op'+field,None)
            row = TR(TD(INPUT(_type="checkbox",_name="chk"+field,
                              value=chkval=='on')),
                     TD(field),TD(SELECT(ops,_name="op"+field,
                                         value=opval)),
                     TD(INPUT(_type="text",_name="txt"+field,_id="txt"+field,
                              _value=txtval, _class=table[field].type)))
            tbl.append(row)
            if chkval:
                if txtval:
                    query &= build_query(table[field], 
                                    opval,txtval)
                selected.append(table[field])           
    
        #limit the results
        row = TR(TD(), TD('Limit to'), TD(),
                 TD(INPUT(_type="text",_name="limitby", _id="limitby",
                          _value=request.vars.get('limitby', "100"),
                          _class="string")))
        tbl.append(row)
    
        #order the requelts
        opts = table.fields[:]
        opts.remove("id")
        opts = [""] + opts
        row = TR(TD(), TD('Order by'), TD(),
                 TD(SELECT(opts,_name="orderby",
                           value=request.vars.get('orderby', None))))
        tbl.append(row)
    
    
        form = FORM(tbl,INPUT(_type="submit"))
    
    
        #for GAE the orderby must already be a table field, not a string, so convert
        orderby=None
        if request.vars.orderby:
            field = request.vars.orderby
            orderby=db[table][field]
    
        results=db(query).select(limitby=(0,int(request.vars.get('limitby','100'))), 
                                            orderby=orderby, *selected)
    
        return form, results
    
    replies (1)
    • nipun-bhardwaj-10101 9 years ago

      Can you send me the full code of dynamic search with models and views.

Commented on:

This creates a form that allows the user to dynamically search and select fields in a table

Hosting graciously provided by:
Python Anywhere