捕捉:内部/外部循环与抢劫 [英] Snap: inner/outer loops with heist

查看:111
本文介绍了捕捉:内部/外部循环与抢劫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否正确地在标题中表达了这个问题,但这是情况。
当表字段(模式)仅在运行时可用时,我需要创建一个带有heist的动态表。
所以通常情况下,当模式在compile时已知,我会这样做:

 < table> 
< row-splice>
< tr>
< td> < field1的/> < / TD>
< td> < FIELD2 /> < / TD>
< td> <字段3 /> < / TD>
< / tr>
< / row-splice>
< / table>

这就是当我在编译时知道字段数目和它们的名字。
我理解如何处理与'runChildrenWith','mapSplices'等处理程序中的所有内容...



现在我处于一种情况字段和模式的数量仅在运行时可用。
因此,据我所知,heist模板看起来像这样:

 < table> 
< row-splice>
< tr>
< field-splice>
< td> <场强/> < / TD>
< / field-splice>
< / tr>
< / row-splice>
< / table>

我很在意如何在处理程序中实现它。我假设我需要做两次'mapSplices' - 一个在另一个里面,对吗?因此,从经验上讲,这将是一个内部和外部循环/地图,对吗?

我如何在处理程序中执行此操作?



谢谢。

更新:

忘记提及架构可以在运行时间从数据库和可用:

  Table {tableName :: Text 
,tableFields :: [Text]

$ / code>

但它并不是真的需要,因为数据来自无模式mongodb并被转换到Map:

  fromList [(FieldName1,Value1),(FieldName2,Value2),( FieldName3,Value3)] 

Update2:

我尝试了一些没有运气的示例,我只是将所有数据都放在一个列中。
我只需要一个简单的内部和外部循环来动态地生成字段和行。
这不能简单:

 <%@ rows.each do | row | %GT; 
< tr>
<%row.each do | field | %GT;
< td> <%= field%> < / TD>
<%end%>
< / tr>
<%end%>

Update3:



我终于破解了它经过长时间的周末休息...
这里是例子。它是特定的mongodb,我简直就是复制并粘贴它。
但是如果有人被内部/外部循环卡住了,我想这会有所帮助。


  showTableH :: AppHandler()
showTableH = do
表< - liftIO $ fetchTable tname
docs< - liftIO $ getColList tname
let rowSplice doc = mapSplices(\ f - > fieldSplice $ T.pack $ at f doc)(tableFields table)
where fieldSplice field = runChildrenWithText [(field,field)]
let listRowsSplice = mapSplices(\d-> runChildrenWith [(fields,rowSplice d)])docs
heistLocal(bindSplices [(rows, listRowsSplice)])$ rendershow-table


解决方案

我认为这里的关键是使用mapSplices和runChildrenWith。我认为它看起来像这样:

  rowSplice = do 
rows< - lift getRowsFromDB
mapSplices(\row - > runChildrenWith [(field-splice,fieldSplice row)])rows
fieldSplice rowId = do
fields< - lift $ getFieldsFromDB rowId
mapSplices \\ f - > runChildrenWith [(field,fieldToText f)])fields


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:

<table>
  <row-splice>
    <tr>
      <td> <field1/> </td>
      <td> <field2/> </td>
      <td> <field3/> </td>
    </tr>
  </row-splice>
</table>

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 ...

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:

<table>
  <row-splice>
    <tr>
      <field-splice>
      <td> <field/> </td>
      </field-splice>
    </tr>
  </row-splice>
</table>

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?

How would I do this within handler??

Thanks.

Update:

Forgot to mention that schema can be retrieved at run time from the DB and available as:

Table { tableName   :: Text
      , tableFields :: [Text]
      }

But it is not really needed as data comes from schema-less mongodb and converted to Map:

fromList [("FieldName1","Value1"),("FieldName2","Value2"),("FieldName3","Value3")]

Update2:

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:

<% @rows.each do |row| %>
  <tr>
  <% row.each do |field| %>
    <td> <%= field %> </td> 
  <% end %>
  </tr>
<% end %>

Update3:

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.

showTableH :: AppHandler ()
showTableH = do
  table <- liftIO $ fetchTable tname
  docs <- liftIO $ getColList tname
  let rowSplice doc = mapSplices (\f -> fieldSplice $ T.pack $ at f doc) (tableFields table)
    where fieldSplice field = runChildrenWithText [("field", field)]
  let listRowsSplice = mapSplices (\d -> runChildrenWith [("fields", rowSplice d)]) docs
  heistLocal (bindSplices [("rows", listRowsSplice)]) $ render "show-table"

解决方案

I think the key here is using mapSplices along with runChildrenWith. I think it would look something like this:

rowSplice = do
    rows <- lift getRowsFromDB
    mapSplices (\row -> runChildrenWith [("field-splice", fieldSplice row)]) rows
fieldSplice rowId = do
    fields <- lift $ getFieldsFromDB rowId
    mapSplices (\f -> runChildrenWith [("field", fieldToText f)]) fields

这篇关于捕捉:内部/外部循环与抢劫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆