English 中文(简体)
Write table on the fly
原标题:
  • 时间:2009-12-22 19:01:45
  •  标签:
  • scala
  • lift

I have the following function, and I would like to write to the page instead of the println. How can I do that? I need a table with that information in my page, But I did t find any information about that, I saw how to write collections to the page, but I would rather prefer write to the page on the fly.

Thanks in advance and I hope for your response.

def tablaAmortizacion(xhtml:NodeSeq,monto:Double,amort:Double,start:java.util.Calendar) {
    var formatter = new java.text.SimpleDateFormat("dd/MM/yyyy")
    var end = new java.util.GregorianCalendar()
    end.setTime(start.getTime)
    end.add(java.util.Calendar.MONTH,1)
    var difference = Math.abs(start.getTimeInMillis - end.getTimeInMillis)
    var days = difference / (1000 * 60 * 60 * 24)

    println("Monto sal: " + monto + "   Amortizacion: " + amort + "   Start: " + formatter.format(start.getTime)  + "   End: " + formatter.format(end.getTime) + "   Days: " + days)

    if (monto > amort) {
        tablaAmortizacion(xhtml,monto-amort,amort,end)
    }

}

Fernando Avalos.

问题回答

Or you can do something like this where you generate the table in the method.

def list = <table>
<thead>
<tr>
    <th>monto-amort</th>
    <th>amort</th>
    <th>end</th>
    <th/>
</tr>
</thead>
<tbody>
  {generateTableBody()}
</tbody>

def generateTableBody = {
//calculate values here.
<tr><td>{monto-amort}</td><td>{amort}</td><td>{end}</td></tr>
}

I m not sure what you mean write to the page. Do you mean you want to dynamically add your table to the page after it s rendered? If you mean in an ajax kind of way, you should look at the comet chat app.

Or do you mean you want some sort of expression language like jsp/jsf pages do? If you mean like jsp/jsf pages, the answer is you can t, by design. If you need to dynamically generate html, you do that in your snippet, not in the xhtml.

here s the answer: In your xhtml file you can have something like:

<table>
    <thead>
    <tr>
        <th>First Name</th>
        <th>Middle Name</th>
        <th>Last Name</th>
        <th/>
    </tr>
    </thead>
    <tbody>
    <lift:PersonSnippets.list>
        <tr>
            <td>
                <party:firstName/>
            </td>
            <td>
                <party:middleName/>
            </td>
            <td>
                <party:lastName/>
            </td>
            <td>
                <party:edit/>
                <party:delete/>
            </td>
        </tr>
    </lift:PersonSnippets.list>
    </tbody>
</table>

Then your snippet looks like:

def list(xhtml: NodeSeq): NodeSeq = {

val people = Model.createNamedQuery[Person]("findAllPeople").getResultList()

people.flatMap(person =>
        bind("party", xhtml,
          "firstName" -> Text(person.getFirstName()),
          "middleName" -> Text(person.getMiddleName()),
          "lastName" -> Text(person.getLastName()),
          "edit" -> link("/contact/person/edit", () => personVar(person), Text(?("Edit"))),
          "delete" -> link("/contact/person/delete", () => personVar(person), Text(?("Delete")))
          ))

}





相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签