com.wa9nnn.wa9nnnutil.tableui.Table.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wa9nnnutil_3 Show documentation
Show all versions of wa9nnnutil_3 Show documentation
Utils for scala3 and playframework 3.x.
The newest version!
/*
* Copyright (C) @year Dick Lieber, WA9NNN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.wa9nnn.wa9nnnutil.tableui
/**
* Something that can be rendered as an html using the play Whirl template /app/views/renderTable.scala.html
*
* @param headers zero or more rows of column headers [[Cell]]s that makeup the
* @param rows in the
* @param cssClass class of table element.
*/
case class Table(headers: Seq[Seq[Cell]], rows: Seq[Row], id: Option[String] = None, cssClass: Seq[String] = Seq("headeredTable")) {
lazy val cols: Int = headers
.lastOption // are there headers
.map(_.length) // how long is the last one.
.getOrElse(0) // 0 if not header
val columnHeaders: Seq[Seq[Cell]] = headers.map(hw => hw.map(Cell(_)))
def withId(id: String): Table = copy(id = Option(id))
def withCssClass(css: String): Table = copy(cssClass = cssClass.appended(css))
def append(newRows: Seq[Row]): Table =
copy(rows =
rows.appendedAll(newRows)
)
def append(tableSection: TableSection): Table =
copy(rows =
rows.appendedAll(tableSection.rows)
)
// /**
// * Add rows to this table with a header
// * This may not correctly set the colspan of the section headers corredtly it the lat row o the header '
// * doesn't match the length of [[Cell]]s in the new [[Rows]]s.
// *
// * @param sectionName full with
*/
def apply(columnHeaders: List[Any], rows: Row*): Table = {
Table(Seq(columnHeaders.map(Cell(_))), rows)
}
/**
* A table with one column.
*
* @param singleColHeader column header
* @param rowValues values for row.
* @return
*/
def apply(singleColHeader: String, rowValues: Seq[Any]): Table = {
val rows: Seq[Row] = rowValues.map { v =>
Row(Seq(Cell(v)))
}
Table(Header(singleColHeader), rows)
}
/**
* A multi line header
*
* {{
* Table(
* Header("fullspan", "colA", "colB", "colC"),
* Row("a", "b", "c")),
* Row("aa", "bb", "cc"))
* }}
*
* @param header a [[Header]]
* @param rows
*/
def apply(header: Header, rows: Seq[RowSectionOrKV]): Table = {
val rowBuilder = Seq.newBuilder[Row]
rows.foreach {
case r: Row =>
rowBuilder += r
case (name: String, value: Any) =>
rowBuilder += Row(Seq(Cell(name), Cell(value)))
case t: TableSection =>
rowBuilder ++= t.rows
}
Table(header.rows, rowBuilder.result())
}
type RowSectionOrKV = Row | TableSection | (String, Any)
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy