[Lift] Re: how to construct a table with a set number of columns

2009-08-18 Thread Naftoli Gugenheim

You mean a word wrap / FlowLayout type of arrangement?
One idea is to have two snippets like yours, one for tr's and one for td's; and 
make you class a StatefulSnippet and initialize a 2D array there (or the 
RequestVar equivalent); and prepare the row for the td snippet in the tr 
snippet.
Another option is to use NodeSeq functions (FuncBindParam) inside one snippet 
instead of multiple snippets. So in your snippet arrange a 2D array, then bind 
say foreach:row to a function that iterates (flatMap) over the rows, binding 
foreach:col to each column of the current row (flatMap) with a NodeSeq function 
that does the bind of the cell contents.


-
harryhhar...@gmail.com wrote:


I have a List[Foo] and I want to construct a table with 10 columns and
however many rows necessary to contain all the Foos. I feel like I
should do something like so, but it's not quite right yet:

table
  lift:MyPage.func
tdfoo:name//td
 /lift:MyPage.func
/table

def func(xhtml: NodeSeq): NodeSeq = {
  val cells: List[NodeSeq] = Foo.findAll(...).flatMap(foo = {
bind(foo, xhtml, name - foo.name)
  })

  // then reduce the list of cells putting in trs in appropriate
places, but not sure how.
}



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Lift group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: how to construct a table with a set number of columns

2009-08-18 Thread Naftoli Gugenheim

With the second option you might be able to do a foldLeft instead of a flatMap 
to List.take(10) one row at a time.

-
harryhhar...@gmail.com wrote:


I have a List[Foo] and I want to construct a table with 10 columns and
however many rows necessary to contain all the Foos. I feel like I
should do something like so, but it's not quite right yet:

table
  lift:MyPage.func
tdfoo:name//td
 /lift:MyPage.func
/table

def func(xhtml: NodeSeq): NodeSeq = {
  val cells: List[NodeSeq] = Foo.findAll(...).flatMap(foo = {
bind(foo, xhtml, name - foo.name)
  })

  // then reduce the list of cells putting in trs in appropriate
places, but not sure how.
}



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Lift group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: how to construct a table with a set number of columns

2009-08-18 Thread Ryan Donahue

You could do something like this.

Snippet:

  def func(xhtml: NodeSeq): NodeSeq = {
val foos = Foo.findAll(...)
val rowSize = (S.attr(rowSize) openOr 10).toInt
val rowNum = (foos.size + rowSize - 1) / rowSize
List.range(0, rowNum).foldLeft(Util.emptyNodeSeq)((ns, i) = {
  val start = i*rowSize
  val end = start+rowSize
  ns ++ bind(list, xhtml, row - row(foos.slice(start, end))
_)
})
  }

  private def row(foos:List[String])(xhtml: NodeSeq) : NodeSeq =
foos.foldLeft(Util.emptyNodeSeq)((ns, foo) = ns ++ bind(foo,
xhtml, name - foo.name))


Then your template can use table/tr/td or it could just use floating
divs that wrap over when necessary:

table
  lift:MyPage.func rowSize=10
tr
  list:row
tdfoo:name//td
  /list:row
/tr
  /lift:MyPage.func
/table

OR

lift:MyPage.func
  list:row
div class=cellfoo:name//div
  /list:row
/lift:MyPage.func

For an empty NodeSeq that works in the foldLeft I've had to create the
following.  Anybody know a better way?
object Util {
  val emptyNodeSeq : NodeSeq = NodeSeq.fromSeq(Seq(xml:group/
xml:group))
}

On Aug 18, 2:58 pm, harryh har...@gmail.com wrote:
 I have a List[Foo] and I want to construct a table with 10 columns and
 however many rows necessary to contain all the Foos. I feel like I
 should do something like so, but it's not quite right yet:

 table
   lift:MyPage.func
     tdfoo:name//td
  /lift:MyPage.func
 /table

 def func(xhtml: NodeSeq): NodeSeq = {
   val cells: List[NodeSeq] = Foo.findAll(...).flatMap(foo = {
     bind(foo, xhtml, name - foo.name)
   })

   // then reduce the list of cells putting in trs in appropriate
 places, but not sure how.

 }

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Lift group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: how to construct a table with a set number of columns

2009-08-18 Thread Ryan Donahue

Ah yes, flatMap, of course.  That will clean up some of my code,
thanks.

On Aug 18, 4:16 pm, harryh har...@gmail.com wrote:
 Ah, perfect!  Thx!

 Incidentally, I simplified in two places by instead of doing this:

 List.range(0, rowNum).foldLeft(Util.emptyNodeSeq)((ns, i) = {

 })

 doing this:

 List.range(0, rowNum).flatMap(i = {

 })

 (simlar change in the row function)

 -harryh

 On Aug 18, 3:52 pm, Ryan Donahue donahu...@gmail.com wrote:

  You could do something like this.

  Snippet:

    def func(xhtml: NodeSeq): NodeSeq = {
      val foos = Foo.findAll(...)
      val rowSize = (S.attr(rowSize) openOr 10).toInt
      val rowNum = (foos.size + rowSize - 1) / rowSize
      List.range(0, rowNum).foldLeft(Util.emptyNodeSeq)((ns, i) = {
        val start = i*rowSize
        val end = start+rowSize
        ns ++ bind(list, xhtml, row - row(foos.slice(start, end))
  _)
      })
    }

    private def row(foos:List[String])(xhtml: NodeSeq) : NodeSeq =
      foos.foldLeft(Util.emptyNodeSeq)((ns, foo) = ns ++ bind(foo,
  xhtml, name - foo.name))

  Then your template can use table/tr/td or it could just use floating
  divs that wrap over when necessary:

  table
    lift:MyPage.func rowSize=10
      tr
        list:row
          tdfoo:name//td
        /list:row
      /tr
    /lift:MyPage.func
  /table

  OR

  lift:MyPage.func
    list:row
      div class=cellfoo:name//div
    /list:row
  /lift:MyPage.func

  For an empty NodeSeq that works in the foldLeft I've had to create the
  following.  Anybody know a better way?
  object Util {
    val emptyNodeSeq : NodeSeq = NodeSeq.fromSeq(Seq(xml:group/
  xml:group))

  }

  On Aug 18, 2:58 pm, harryh har...@gmail.com wrote:

   I have a List[Foo] and I want to construct a table with 10 columns and
   however many rows necessary to contain all the Foos. I feel like I
   should do something like so, but it's not quite right yet:

   table
     lift:MyPage.func
       tdfoo:name//td
    /lift:MyPage.func
   /table

   def func(xhtml: NodeSeq): NodeSeq = {
     val cells: List[NodeSeq] = Foo.findAll(...).flatMap(foo = {
       bind(foo, xhtml, name - foo.name)
     })

     // then reduce the list of cells putting in trs in appropriate
   places, but not sure how.

   }

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Lift group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---



[Lift] Re: how to construct a table with a set number of columns

2009-08-18 Thread harryh

Ah, perfect!  Thx!

Incidentally, I simplified in two places by instead of doing this:

List.range(0, rowNum).foldLeft(Util.emptyNodeSeq)((ns, i) = {
})

doing this:

List.range(0, rowNum).flatMap(i = {
})

(simlar change in the row function)

-harryh

On Aug 18, 3:52 pm, Ryan Donahue donahu...@gmail.com wrote:
 You could do something like this.

 Snippet:

   def func(xhtml: NodeSeq): NodeSeq = {
     val foos = Foo.findAll(...)
     val rowSize = (S.attr(rowSize) openOr 10).toInt
     val rowNum = (foos.size + rowSize - 1) / rowSize
     List.range(0, rowNum).foldLeft(Util.emptyNodeSeq)((ns, i) = {
       val start = i*rowSize
       val end = start+rowSize
       ns ++ bind(list, xhtml, row - row(foos.slice(start, end))
 _)
     })
   }

   private def row(foos:List[String])(xhtml: NodeSeq) : NodeSeq =
     foos.foldLeft(Util.emptyNodeSeq)((ns, foo) = ns ++ bind(foo,
 xhtml, name - foo.name))

 Then your template can use table/tr/td or it could just use floating
 divs that wrap over when necessary:

 table
   lift:MyPage.func rowSize=10
     tr
       list:row
         tdfoo:name//td
       /list:row
     /tr
   /lift:MyPage.func
 /table

 OR

 lift:MyPage.func
   list:row
     div class=cellfoo:name//div
   /list:row
 /lift:MyPage.func

 For an empty NodeSeq that works in the foldLeft I've had to create the
 following.  Anybody know a better way?
 object Util {
   val emptyNodeSeq : NodeSeq = NodeSeq.fromSeq(Seq(xml:group/
 xml:group))

 }

 On Aug 18, 2:58 pm, harryh har...@gmail.com wrote:

  I have a List[Foo] and I want to construct a table with 10 columns and
  however many rows necessary to contain all the Foos. I feel like I
  should do something like so, but it's not quite right yet:

  table
    lift:MyPage.func
      tdfoo:name//td
   /lift:MyPage.func
  /table

  def func(xhtml: NodeSeq): NodeSeq = {
    val cells: List[NodeSeq] = Foo.findAll(...).flatMap(foo = {
      bind(foo, xhtml, name - foo.name)
    })

    // then reduce the list of cells putting in trs in appropriate
  places, but not sure how.

  }

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Lift group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~--~~~~--~~--~--~---