scala:将属性(奇数行和偶数行)添加到xml表

时间:2010-06-05 16:46:01

标签: xml scala scala-2.8 lift

在Lift应用程序中,我想添加一个特殊标记,该标记占用下一个表的<tbody>部分,并为每个表添加oddeven个类(例如) <tr>标记。当然,交替。虽然我找到了为所有<tr>标记添加其他属性的方法,但仍然存在一些问题(请参阅下面的代码)。

首先,它不起作用。 cycle.next经常被调用,所以最后一切都是odd行。其他问题是代码不排除内部表(因此嵌套的<tr>也会被转换)并且它也会影响表的<thead>部分。

使这段代码有效的想法? (当然,如果已经有一个基于电梯的解决方案 - 没有jQuery - 为此,我将非常感激。)

// Helper class for looping
class Loop(val strs: String*) {
    val stream_iter = Stream.continually(strs.toStream).flatten.iterator
    def next = stream_iter.next
}

val cycle = new Loop("even", "odd")

val rr = new RewriteRule {
  override def transform(n: Node): Seq[Node] = n match {
    // match any Elem
    case elem : Elem => elem match {
        // If it’s a <tr> do some further processing
        case Elem(_, "tr", att @ _, _, _*) => 
            elem % Attribute(None, "class", Text(
                // add the attribute and concatenate with others
                List(att.get("class").getOrElse("").toString, cycle.next).reduceLeft(_+" "+_).trim
                ), Null) toSeq
        case other => other
    }
    case other => other
  }
}

val rt = new RuleTransformer(rr)

val code = <table>
  <thead><tr><td>Don’t</td><td>transform this</td></tr></thead>
  <tbody>
    <tr class="otherclass">
      <td>r1c1</td><td>r1c2</td>
    </tr>
    <tr>
      <td>r2c1</td><td>r2c2</td>
    </tr>
    <tr>
      <td>r3c1</td><td>r3c2</td>
    </tr>
  </tbody>
</table>

println(rt(code))

2 个答案:

答案 0 :(得分:3)

我有两种不同的方法来完成备用表行类:

1 - 使用jQuery colorize plugin

2 - 在循环数据时使用zipWithIndex来创建表格:

doQuery.zipWithIndex.map{ case (log, i) => {  
    <tr class={if (i % 2 == 0) "even" else "odd"}>  
        <td>Data</td>  
    </tr>  
}  

答案 1 :(得分:1)

RewriteRule的问题似乎是他们陷入了太深的困境。也就是说,一旦启动了向<tr>添加属性的规则,就无法阻止它。 (至少,它对我不起作用。)但是,我找到了一个适合我的递归解决方案。此外,只要内部有 一个<tbody>,递归就会提前停止。如果没有,我们可能会遇到问题......

abstract class Loop {
    val stream_iter = Stream.continually(elems.toStream).flatten.iterator
    def next = stream_iter.next
    def elems: Seq[String]
}
class Cycle extends Loop { override def elems = List("odd", "even") }

// Call this when in <tbody>
def transformChildren(sn: Seq[Node]): Seq[Node] = {
    // Start a new cycle
    val cycle = new Cycle
    sn.map{ node => node match {
        case Elem(prefix, "tr", att, scope, ch @ _*) => 
            Elem(prefix, "tr", att, scope, ch:_*) % Attribute(None, "class", Text(
                List(att.get("class").getOrElse("").toString, cycle.next).reduceLeft(_+" "+_).trim
                ), Null)
        case other => other
        }
    }
}

// Look for first <tbody>, transform child tr elements and stop recursion
// If no <tbody> found, recurse
def recurse(sn: NodeSeq): NodeSeq = sn.map{ node =>
    node match {
        case Elem(prefix, "tbody", att, scope, ch @ _*)
            => Elem(prefix, "tbody", att, scope, transformChildren(ch):_*)
        case Elem(prefix, label, att, scope, ch @ _*)
            => Elem(prefix, label, att, scope, recurse(ch):_*)
        case other => other
    }
}

val code = <table>
  <thead><tr><td>Don’t</td><td>transform this</td></tr></thead>
  <tbody>
    <tr class="otherclass">
      <td>r1c1</td><td>r1c2</td>
    </tr>
    <tr>
      <td><table><tbody><tr><td>Neither this.</td></tr></tbody></table></td><td>r2c2</td>
    </tr>
    <tr>
      <td>r3c1</td><td>r3c2</td>
    </tr>
  </tbody>
</table>

println(recurse(code))

给出:

<table>
  <thead><tr><td>Don’t</td><td>transform this</td></tr></thead>
  <tbody>
    <tr class="otherclass odd">
      <td>r1c1</td><td>r1c2</td>
    </tr>
    <tr class="even">
      <td><table><tbody><tr><td>Neither this</td></tr></tbody></table></td><td>r2c2</td>
    </tr>
    <tr class="odd">
      <td>r3c1</td><td>r3c2</td>
    </tr>
  </tbody>
</table>