2017-12-11 8 views
1

j2htmlsを使って各メソッドを使ってコレクションの要素を追加するにはどうすればよいですか?どのようにj2htmlを各メソッドで使うのですか

彼らはhttps://j2html.com/examples.html

// each() lets you iterate through a collection and returns the generated HTML 
// as a DomContent object, meaning you can add siblings, which is not possible 
// using the stream api in the previous example 
body(
    div(attrs("#employees"), 
     p("Some sibling element"), 
     each(employees, employee -> 
      div(attrs(".employee"), 
       h2(employee.getName()), 
       img().withSrc(employee.getImgPath()), 
       p(employee.getTitle()) 
      ) 
     ) 
    ) 
) 

に例を与えるしかし、彼らは実際にどのような従業員または従業員を定義していません。

私の場合は、div(各ラベル付き)に一連のCounter要素を追加したいが、それを行う方法がわからないので、今は唯一j2htmlをそれぞれ使用しているし、ハードコードされたタグ。

sb.append("<div>\n"); 
for(Map.Entry<Integer, Counter> next:fsc.getCounters().entrySet()) 
{ 
    sb.append(label(next.getValue().getText()).attr("for","pb"+next.getKey().intValue())); 
    sb.append(render(progress() 
      .withId("pb"+next.getKey().intValue()) 
      .attr("value", next.getValue().getCounter().intValue()) 
      .attr("max", "100"))); 
    sb.append(rendern(br())); 
} 
sb.append("</div>\n"); 

答えて

1

わかりましたので、私は一例では把握していなかったものを従業員がコレクション変数であると従業員がarbitaryありました、それは単にループに割り当てられたローカル変数であり、あなたが望む何もすることができます。

これで機能しました。

sb.append(rendern(table(each(fsc.getCounters().entrySet(), next -> 
     tr(
       td(
         label(next.getValue().getText()) 
           .attr(FOR,PB_PREFIX+next.getKey().intValue())), 
       td(
         iffElse(next.getValue().getBar().isIndeterminate(), 
           progress() 
             .withId(PB_PREFIX+next.getKey().intValue()), 
           progress() 
             .withId(PB_PREFIX+next.getKey().intValue()) 
             .attr(VALUE, next.getValue().getCounter().intValue()) 
             .attr(MAX, next.getValue().getBar().getMaximum()) 
         ) 
       ) 
     ) 
    ) 
)));  
関連する問題