tags:

views:

137

answers:

2

Hi,

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.

A: 

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")))
          ))

}

Jim Barrows
I need to dynamically generate html, or map my code to tags in the template. But, I don't want to use a collection to map, I want to be writing each time mi recursive function is called.
Favalos
Is that posible?
Favalos
+2  A: 

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>
}
Jim Barrows