--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/net/tz/lift/snippet/Table.scala Sun Apr 03 15:55:02 2011 +0200
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.tz.lift.snippet
+
+import net.liftweb.common._
+import net.liftweb.util.Helpers._
+import scala.xml.{Elem, NodeSeq, Text}
+
+object Column {
+ def apply[T](name: String, f: T => NodeSeq): Column[T] =
+ new Column[T](Text(name), f, { _ => Empty})
+ def apply[T](name: String, f: T => NodeSeq, tdCss: String): Column[T] =
+ new Column[T](Text(name), f, { _ => Full(tdCss)})
+}
+
+class Column[T](name: => NodeSeq, f: T => NodeSeq, tdCss: T => Box[String])
+ extends Function1[T, NodeSeq] {
+
+ def th: Elem = <th>{name}</th>
+ def td(in: T): Elem = {
+ val r = <td>{apply(in)}</td>
+ tdCss(in) map { css => r % ("class" -> css) } openOr r
+ }
+ def apply(in: T): NodeSeq = f(in)
+}
+
+object Table {
+ def apply[T](cols: Iterable[Column[T]], rows: Iterable[T]) = new Table(Empty,
+ cols, rows, Empty)
+ def apply[T](css: String, cols: Iterable[Column[T]], rows: Iterable[T],
+ foot: NodeSeq) = new Table(Full(css), cols, rows, Full(foot))
+}
+
+class Table[T](css: Box[String], cols: Iterable[Column[T]],
+ rows: => Iterable[T], foot: Box[NodeSeq]) extends
+ Function1[NodeSeq, NodeSeq] {
+
+ def oddEven(i: Int) = (i % 2) match {
+ case 1 => "even"
+ case 0 => "odd"
+ }
+
+ def thead: Elem = <thead><tr>{ cols.map(c => c.th) }</tr></thead>
+ def tbody: Elem = <tbody>{
+ rows.zipWithIndex.map( r => tr(r._1, r._2))
+ }</tbody>
+ def tr(in: T, idx: Int): Elem = <tr class={oddEven(idx)}>{
+ cols.map(_.td(in))
+ }</tr>
+
+ def tfoot: Elem = <tfoot>{
+ foot map { f => <tr><td colspan={cols.size.toString}>{f}</td></tr>
+ } openOr NodeSeq.Empty
+ }</tfoot>
+
+ def apply(ns: NodeSeq): NodeSeq = {
+ val t = <table>{
+ List(thead, tfoot, tbody)
+ }</table>
+ css map { cl => t % ("class" -> cl) } openOr t
+ }
+
+}
+
+// vim: set ts=2 sw=2 et: