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?

DorkRawk
  • 692
  • 1
  • 6
  • 20

1 Answers1

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.

Community
  • 1
  • 1
jcern
  • 7,499
  • 4
  • 35
  • 46
  • That seems to change/add ids to elements inside the tr element. I need thing the id to be added to each , but not the s or anything else inside. – DorkRawk Sep 12 '13 at 22:42
  • @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 Sep 13 '13 at 01:40
  • @jcern No, that will only affect tr elements inside the .row (which is a tr). I need to append an id to the tr. – DorkRawk Sep 13 '13 at 18:37
  • @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 Sep 13 '13 at 18:38
  • @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. – jcern Sep 13 '13 at 20:40
  • @jcern You're right! That was my issue. I needed to work with the whole tr, not just the contents. Thanks. – DorkRawk Sep 13 '13 at 21:32
  • @DorkRawk I suppose you already managed to do this. I just added a minor note that you can use `.row [id]` just like other selectors `.cell1 *` – VasiliNovikov Sep 14 '13 at 13:45