title: Web2py databases: How do I select a subset of rows from selected rows? And how do I pass it to JavaScript?
body: <p>I have a table and want the web2py action to pass all its contents for the view. The view then selects a subset from it and manifests them one at a time in iteration.</p>
  
  <p>Here's a sample table in db.py:</p>
  
  <pre><code>db.define_table('block',
      Field('location'),
      Field('propertyA'),
      Field('propertyB')
  )
  </code></pre>
  
  <p>A sample action in controller default.py:</p>
  
  <pre><code>def demo():
      return dict(blocks=db(db.block).select())
  </code></pre>
  
  <p>So far so good. It compiles, doesn't crash and after running a few tests it did what I wanted.</p>
  
  <p>But now for the view. In this example, I would want to select all whose "propertyA" is, say, 5. And then I want to run a loop, which prints them in to a table, which already exists. The table has 100 cells and id's are 1-100. I want to print the values of propertyB to the table cells, whose id matches the blocks' location.</p>
  
  <p>A sample view default/demo.html:</p>
  
  <pre><code>{{extend 'layout.html'}}
  
  &lt;style&gt;
      table#map, td {
          border: 1px solid black;
          border-collapse: collapse;
      }
      td {
          background-color: gray;
          width: 50px;
          height:50px;
          text-align: center;
          color: white;
      }
  &lt;/style&gt;
  
  &lt;!--This creates a 10*10 table with running id from 1 to 100--&gt;
  &lt;table id="map"&gt;
      &lt;caption&gt;Map&lt;/caption&gt;
      {{for y in range(10):}}
      &lt;tr&gt;
          {{for x in range(10):}}
          {{=TD('', _id=10*y+x)}}
          {{pass}}
      &lt;/tr&gt;
      {{pass}}
  &lt;/table&gt;
  
  &lt;!--then I want to select a subset from blocks, whose propertyA is 5
  These lines crash if uncommented.--&gt;
  {{#query = (propertyA == 5)}}
  {{#subset = blocks(query).select()}}
  
  &lt;!--and run a loop which, which iterates the subset, and in each
  iteration, writes the value of propertyB, if cell's id and block's location
  match. I just made a place holder function, because I don't know how to
  pass python variables/objects to javascript--&gt;
  &lt;script&gt;
      //var subset = "subset from python";
      function myFunction() {
          var i;
          for (i = 0; i &lt; 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 = '';
              //}
          }
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>So I have no idea how this should be done. And the web2py tutorial book is quite stingy on info about this. Or am I having a completely wrong approach to this? Because I think it could also be done with ajax calls, but I don't feel like making a database server query 100 times in a row is the right thing to do.</p>