Note that there are some explanatory texts on larger screens.

plurals
  1. POSnap: inner/outer loops with heist
    primarykey
    data
    text
    <p>I am not sure if I phrased the question in the title correctly but here is the situation.. I need to create a dynamic table with heist when table fields (schema) are only available at run time. So usually, when the schema is known at compile, I would do something like this:</p> <pre><code>&lt;table&gt; &lt;row-splice&gt; &lt;tr&gt; &lt;td&gt; &lt;field1/&gt; &lt;/td&gt; &lt;td&gt; &lt;field2/&gt; &lt;/td&gt; &lt;td&gt; &lt;field3/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/row-splice&gt; &lt;/table&gt; </code></pre> <p>That is when I know the number of fields and their names at compile time. I understand how to process all that in the handler with 'runChildrenWith', 'mapSplices' and so on ...</p> <p>Now I am in a situation when the number of fields and schema are available only at run time. So the heist template, as I understand it, would look like this:</p> <pre><code>&lt;table&gt; &lt;row-splice&gt; &lt;tr&gt; &lt;field-splice&gt; &lt;td&gt; &lt;field/&gt; &lt;/td&gt; &lt;/field-splice&gt; &lt;/tr&gt; &lt;/row-splice&gt; &lt;/table&gt; </code></pre> <p>I am really stuck with how to implement it within the handler. I assume I would need to do 'mapSplices' twice - one inside the other, correct? So, empirically speaking, that would be an inner and outer loop/map, right?</p> <p>How would I do this within handler??</p> <p>Thanks.</p> <p>Update:</p> <p>Forgot to mention that schema can be retrieved at run time from the DB and available as:</p> <pre><code>Table { tableName :: Text , tableFields :: [Text] } </code></pre> <p>But it is not really needed as data comes from schema-less mongodb and converted to Map:</p> <pre><code>fromList [("FieldName1","Value1"),("FieldName2","Value2"),("FieldName3","Value3")] </code></pre> <p>Update2:</p> <p>I tried suggested examples with no luck I just get all my data in a single column. All I need is a simple inner and outer loop to generate fields and rows dynamically. It cannot be simpler then this:</p> <pre><code>&lt;% @rows.each do |row| %&gt; &lt;tr&gt; &lt;% row.each do |field| %&gt; &lt;td&gt; &lt;%= field %&gt; &lt;/td&gt; &lt;% end %&gt; &lt;/tr&gt; &lt;% end %&gt; </code></pre> <p>Update3:</p> <p>I finally cracked it after a long weekend rest ... Here is the example. it is mongodb specific and I literally just copied and pasted it. But if someone gets stuck with inner/outer loops it would be helpful, I suppose. </p> <pre><code>showTableH :: AppHandler () showTableH = do table &lt;- liftIO $ fetchTable tname docs &lt;- liftIO $ getColList tname let rowSplice doc = mapSplices (\f -&gt; fieldSplice $ T.pack $ at f doc) (tableFields table) where fieldSplice field = runChildrenWithText [("field", field)] let listRowsSplice = mapSplices (\d -&gt; runChildrenWith [("fields", rowSplice d)]) docs heistLocal (bindSplices [("rows", listRowsSplice)]) $ render "show-table" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload