user1043466
user1043466

Reputation: 353

Loop over a list of objects and display it in lift

How do you look over an object and display the results, without having markup in your scala code?

I have the following code:

class User(id: Long, name: String)

class DisplayIt {
  def display = {
    val users = List(new User(0,"John"), new User(1, "James"))
    "#name *" #> users.map(_.name) &
    "#id *" #> users.map(_.id.toString)
  }
}


//In the html:
<div class="lift:DisplayIt.display">
  <div class="one-user">
    User <span id="name"> has the id <span id="id">
  </div>
</div>

What happens now is that I end with "User John James has the id 0 1", all within one div class="one-user".

How do I loop over it so I have one div class="one-user" for each user?

I know I can write the html/xml in scala code and do it that way, but is there a straightforward way to do it without any xml in the scala code?

Upvotes: 0

Views: 512

Answers (2)

fmpwizard
fmpwizard

Reputation: 2768

Try

def list = { ".one-user *" #> users.map( n => { 
  "#name *" #> n.name) & 
  "#id *" #> n.id.toString) 
  }
) }

Upvotes: 1

Debilski
Debilski

Reputation: 67828

Try

def display = {
  val users = List(new User(0, "John"), new User(1, "James"))
  ".one-user *" #> users.map { u =>
    "#name *" #> u.name &
    "#id *" #> u.id.toString
  }
}

Basically, you have to match a surrounding element first and apply a list of transformations to that.

Upvotes: 1

Related Questions