python - Web2py databases: How do I select a subset of rows from selected rows? And how do I pass it to JavaScript? -


i have table , want web2py action pass contents view. view selects subset , manifests them 1 @ time in iteration.

here's sample table in db.py:

db.define_table('block',     field('location'),     field('propertya'),     field('propertyb') ) 

a sample action in controller default.py:

def demo():     return dict(blocks=db(db.block).select()) 

so far good. compiles, doesn't crash , after running few tests did wanted.

but view. in example, want select "propertya" is, say, 5. , want run loop, prints them in table, exists. table has 100 cells , id's 1-100. want print values of propertyb table cells, id matches blocks' location.

a sample view default/demo.html:

{{extend 'layout.html'}}  <style>     table#map, td {         border: 1px solid black;         border-collapse: collapse;     }     td {         background-color: gray;         width: 50px;         height:50px;         text-align: center;         color: white;     } </style>  <!--this creates 10*10 table running id 1 100--> <table id="map">     <caption>map</caption>     {{for y in range(10):}}     <tr>         {{for x in range(10):}}         {{=td('', _id=10*y+x)}}         {{pass}}     </tr>     {{pass}} </table>  <!--then want select subset blocks, propertya 5 these lines crash if uncommented.--> {{#query = (propertya == 5)}} {{#subset = blocks(query).select()}}  <!--and run loop which, iterates subset, , in each iteration, writes value of propertyb, if cell's id , block's location match. made place holder function, because don't know how pass python variables/objects javascript--> <script>     //var subset = "subset python";     function myfunction() {         var i;         (i = 0; < 100; i++) {             //var cell = document.getelementbyid(i);             //if(subset(location===cell.id).select() === true) {                 //var value = subset(location===cell.id).propertyb;                 //cell.innerhtml = value;             //} else {                 //cell.innerhtml = '';             //}         }     } </script> 

so have no idea how should done. , web2py tutorial book quite stingy on info this. or having wrong approach this? because think done ajax calls, don't feel making database server query 100 times in row right thing do.

.select method of dal set object , returns rows object -- cannot apply .select method again rows object. instead, rows object has .find method, returns new filtered rows object:

blocks.find(lambda row: row.propertya == 5) 

if want use subset of propertya values in javascript, need write javascript code in template. first, have extract individual values each row list, , convert json in order use javascript variable:

{{from gluon.serializers import json}}  <script>   var subset = {{=json([r.propertya r in blocks if r.propertya == 5])}} </script> 

here use template delimiters ({{ }}) write results of python code directly javascript code in order define value of subset variable.

also, notice because list comprehension used generate list of propertya values, possible use if clause filter records rather using .find method.

it best minimize amount of python logic in views (harder read, debug, , test), might better create of json arrays in controller , pass them view.

also, if number of records in blocks large, filtering in python might slow, end being faster separate database queries each subset needed, rather doing single query , building subsets purely in python (especially if each subset requires different fields database table -- way can limit each query fields required, further improve performance). maybe not worth worrying right now, if performance becomes issue, can profiling determine best approach.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -