|
11 | 11 | import j2html.tags.UnescapedText;
|
12 | 12 | import j2html.tags.specialized.*;
|
13 | 13 |
|
14 |
| -import java.util.Collection; |
15 |
| -import java.util.List; |
16 |
| -import java.util.Map; |
| 14 | +import java.util.*; |
17 | 15 | import java.util.Map.Entry;
|
18 |
| -import java.util.Objects; |
19 |
| -import java.util.Optional; |
20 | 16 | import java.util.function.BiFunction;
|
21 | 17 | import java.util.function.Function;
|
22 | 18 | import java.util.function.Predicate;
|
@@ -122,6 +118,24 @@ public static <T> DomContent each(Collection<T> collection, Function<? super T,
|
122 | 118 | return tag(null).with(collection.stream().map(mapper));
|
123 | 119 | }
|
124 | 120 |
|
| 121 | + /** |
| 122 | + * Creates a DomContent object containing HTML using a mapping BiFunction on a collection |
| 123 | + * Intended usage: {@literal each(names, (index, name) -> li(index + " " + name))} |
| 124 | + * |
| 125 | + * @param <T> The derived generic parameter type |
| 126 | + * @param collection the collection to iterate over, ex: a list of values [ "Tom", "Dick", "Harry" ] |
| 127 | + * @param mapper the mapping BiFunction, ex: {@literal "(index, name) -> li(index + " " + name)"} |
| 128 | + * @return DomContent containing mapped data {@literal (ex. docs: [li(0 Tom), li(1 Dick), li(2 Harry)])} |
| 129 | + */ |
| 130 | + public static <T> DomContent each(Collection<T> collection, BiFunction<Integer, ? super T, DomContent> mapper) { |
| 131 | + ContainerTag<?> dom = tag(null); |
| 132 | + int i = 0; |
| 133 | + for(T t : collection){ |
| 134 | + dom.with(mapper.apply(i++, t)); |
| 135 | + } |
| 136 | + return dom; |
| 137 | + } |
| 138 | + |
125 | 139 | public static <I, T> DomContent each(final Map<I, T> map, final Function<Entry<I, T>, DomContent> mapper) {
|
126 | 140 | return each(map.entrySet().stream().map(mapper));
|
127 | 141 | }
|
|
0 commit comments