0

在 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))
4

2 回答 2

3

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

1 - 使用 jQuery colorize 插件

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

doQuery.zipWithIndex.map{ case (log, i) => {  
    <tr class={if (i % 2 == 0) "even" else "odd"}>  
        <td>Data</td>  
    </tr>  
}  
于 2010-06-07T15:35:11.700 回答
1

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

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>
于 2010-06-07T15:50:32.507 回答