1

I have some Lift code that creates table rows from a List:

".row *" #> myList.map(x => {
  val rowId = x.id

  ".cell1" #> x.data &
  ".cell2" #> x.moreData 
})

Based on a template like this:

<table>
  <tr class="row">
    <td class="cell1"></td>
    <td class="cell2"></td>
  <tr>
<table>

I want output like this:

<table>
  <tr class="row" id="123">
    <td class="cell1">stuff</td>
    <td class="cell2">junk</td>
  <tr>
  <tr class="row" id="456">
    <td class="cell1">more stuff</td>
    <td class="cell2">more junk</td>
  <tr>
<table>

How do I set that id attribute to be rowId for each tr based on my List elements?

1 Answer 1

2

This should work for you:

".row" #> myList.map(x => {
  val rowId = x.id

  "tr [id]" #> rowId &
  ".cell1 *" #> x.data &
  ".cell2 *" #> x.moreData 
})

To set an attribute, you usually just need to specify the name of the attribute inside of []. So, in addition to ID, if you wanted to add a class, it would be [class]. There is also a special modifier, + which will append to the current value. So [class+] will add whatever you specify to the current values.

It is also worth noting that some drafts of the HTML spec require at least one letter in the ID, see this question for an explanation of why.

Sign up to request clarification or add additional context in comments.

7 Comments

That seems to change/add ids to elements inside the tr element. I need thing the id to be added to each <tr class="row"></tr>, but not the <td>s or anything else inside.
@DorkRawk I updated the code to anchor only on the TR for the id. That will not add the ID attribute the children TD elements.
@jcern No, that will only affect tr elements inside the .row (which is a tr). I need to append an id to the tr.
@VasyaNovikov Yes, I know I can do that, but what is the best way to make that work with pulling data from a List?
@DorkRawk, notice I dropped the ".row *" which you had in favor of ".row". That will pass the TR into the transform, not just the children.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.