Skip to content

Commit f87ff88

Browse files
committed
Fix object answer. Improve sample
Signed-off-by: Pierre Adam <p.adam@merim-groupe.com>
1 parent c706a71 commit f87ff88

10 files changed

Lines changed: 263 additions & 19 deletions

File tree

core/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3+
<!--
4+
~ The MIT License (MIT)
5+
~
6+
~ Copyright (c) 2025 Pierre Adam
7+
~
8+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
9+
~ of this software and associated documentation files (the "Software"), to deal
10+
~ in the Software without restriction, including without limitation the rights
11+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
~ copies of the Software, and to permit persons to whom the Software is
13+
~ furnished to do so, subject to the following conditions:
14+
~
15+
~ The above copyright notice and this permission notice shall be included in all
16+
~ copies or substantial portions of the Software.
17+
~
18+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
~ SOFTWARE.
25+
-->
26+
327
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
428
xmlns="http://maven.apache.org/POM/4.0.0"
529
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

core/src/main/java/com/github/PierreAdam/javadatatables/core/implementations/SimpleDataTables.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,26 @@ public JsonNode getAjaxResult(final Parameters parameters, final C context) {
202202
return this.objectMapper.valueToTree(result);
203203
}
204204

205+
/**
206+
* Use object answer boolean.
207+
*
208+
* @param parameters the parameters
209+
* @return the boolean
210+
*/
205211
private boolean useObjectAnswer(final Parameters parameters) {
206212
for (final Column column : parameters.getColumns()) {
207213
if (column.getData() != null) {
208214
try {
209215
Long.parseLong(column.getData());
210-
return false;
211216
} catch (final NumberFormatException ignore) {
217+
return true;
212218
// Not a number, continue checking
213219
}
214220
}
215221
}
216222

217-
return true;
223+
// All the column are using numbers. We can use non-object answer.
224+
return false;
218225
}
219226

220227
/**
@@ -315,7 +322,7 @@ protected ArrayNode objectToArrayNode(final E entity, final Parameters parameter
315322
final ArrayNode data = this.objectMapper.createArrayNode();
316323

317324
parameters.getOrderedColumns().forEach(column -> {
318-
if (column == null) {
325+
if (column == null || column.getSafeName() == null) {
319326
data.addNull();
320327
} else {
321328
final Optional<BiFunction<E, C, String>> optionalDisplaySupplier = this.field(column.getSafeName()).getDisplaySupplier();
@@ -346,10 +353,12 @@ protected ObjectNode objectToObjectNode(final E entity, final Parameters paramet
346353
final String columnName = column.getSafeName();
347354
final Optional<BiFunction<E, C, String>> optionalDisplaySupplier = this.field(columnName).getDisplaySupplier();
348355

349-
if (optionalDisplaySupplier.isPresent()) {
350-
data.put(columnName, optionalDisplaySupplier.get().apply(entity, context));
351-
} else {
352-
data.set(columnName, this.resolveColumn(column, entity, context));
356+
if (columnName != null) {
357+
if (optionalDisplaySupplier.isPresent()) {
358+
data.put(columnName, optionalDisplaySupplier.get().apply(entity, context));
359+
} else {
360+
data.set(columnName, this.resolveColumn(column, entity, context));
361+
}
353362
}
354363
});
355364

core/src/test/resources/logback.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ The MIT License (MIT)
4+
~
5+
~ Copyright (c) 2025 Pierre Adam
6+
~
7+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
8+
~ of this software and associated documentation files (the "Software"), to deal
9+
~ in the Software without restriction, including without limitation the rights
10+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
~ copies of the Software, and to permit persons to whom the Software is
12+
~ furnished to do so, subject to the following conditions:
13+
~
14+
~ The above copyright notice and this permission notice shall be included in all
15+
~ copies or substantial portions of the Software.
16+
~
17+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
~ SOFTWARE.
24+
-->
25+
226
<configuration>
327
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
428
<layout class="ch.qos.logback.classic.PatternLayout">

jooq/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3+
<!--
4+
~ The MIT License (MIT)
5+
~
6+
~ Copyright (c) 2025 Pierre Adam
7+
~
8+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
9+
~ of this software and associated documentation files (the "Software"), to deal
10+
~ in the Software without restriction, including without limitation the rights
11+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
~ copies of the Software, and to permit persons to whom the Software is
13+
~ furnished to do so, subject to the following conditions:
14+
~
15+
~ The above copyright notice and this permission notice shall be included in all
16+
~ copies or substantial portions of the Software.
17+
~
18+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
~ SOFTWARE.
25+
-->
26+
327
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
428
xmlns="http://maven.apache.org/POM/4.0.0"
529
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3+
<!--
4+
~ The MIT License (MIT)
5+
~
6+
~ Copyright (c) 2025 Pierre Adam
7+
~
8+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
9+
~ of this software and associated documentation files (the "Software"), to deal
10+
~ in the Software without restriction, including without limitation the rights
11+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
~ copies of the Software, and to permit persons to whom the Software is
13+
~ furnished to do so, subject to the following conditions:
14+
~
15+
~ The above copyright notice and this permission notice shall be included in all
16+
~ copies or substantial portions of the Software.
17+
~
18+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
~ SOFTWARE.
25+
-->
26+
327
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
428
xmlns="http://maven.apache.org/POM/4.0.0"
529
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

sample/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3+
<!--
4+
~ The MIT License (MIT)
5+
~
6+
~ Copyright (c) 2025 Pierre Adam
7+
~
8+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
9+
~ of this software and associated documentation files (the "Software"), to deal
10+
~ in the Software without restriction, including without limitation the rights
11+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
~ copies of the Software, and to permit persons to whom the Software is
13+
~ furnished to do so, subject to the following conditions:
14+
~
15+
~ The above copyright notice and this permission notice shall be included in all
16+
~ copies or substantial portions of the Software.
17+
~
18+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
~ SOFTWARE.
25+
-->
26+
327
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
428
xmlns="http://maven.apache.org/POM/4.0.0"
529
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

sample/src/main/java/com/github/PierreAdam/javadatatables/sample/JooqPersonDataTable.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.jooq.impl.DSL;
3232

3333
import java.time.OffsetDateTime;
34+
import java.util.HashMap;
3435
import java.util.UUID;
3536

3637
/**
@@ -75,13 +76,28 @@ public JooqPersonDataTable(final ObjectMapper objectMapper, final DSLContext dsl
7576
)
7677
)
7778
)
78-
.setRowExtraData(extraData -> {
79-
}//extraData
80-
// .setRowId(personEntity -> personEntity.getUid().toString())
81-
// .setRowData(personEntity -> new HashMap<String, String>() {{
82-
// this.put("uid", personEntity.getUid().toString());
83-
// this.put("fullName", String.format("%s %s", personEntity.getFirstName(), personEntity.getLastName()));
84-
// }})
79+
.setRowExtraData(extraData -> extraData
80+
.setRowId(personEntity -> personEntity.getUid().toString())
81+
.setRowClass(personEntity -> personEntity.getActive() ? "active" : "inactive")
82+
.setRowAttr(personEntity -> new HashMap<String, String>() {{
83+
this.put("createdAt", personEntity.getCreatedAt().toString());
84+
}})
85+
.setRowData(personEntity -> new HashMap<String, String>() {{
86+
this.put("uid", personEntity.getUid().toString());
87+
this.put("fullName", String.format("%s %s", personEntity.getFirstName(), personEntity.getLastName()));
88+
}})
89+
)
90+
.field("firstName", field -> field
91+
.setSearchHandler((jooqProvider, s) -> jooqProvider.addCondition(DSL.field("firstName").containsIgnoreCase(s)))
92+
.setOrderHandler((jooqProvider, orderEnum) -> jooqProvider.addSort(DSL.field("firstName"), orderEnum))
93+
)
94+
.field("lastName", field -> field
95+
.setSearchHandler((jooqProvider, s) -> jooqProvider.addCondition(DSL.field("lastName").containsIgnoreCase(s)))
96+
.setOrderHandler((jooqProvider, orderEnum) -> jooqProvider.addSort(DSL.field("lastName"), orderEnum))
97+
)
98+
.field("title", field -> field
99+
.setSearchHandler((jooqProvider, s) -> jooqProvider.addCondition(DSL.field("title").containsIgnoreCase(s)))
100+
.setOrderHandler((jooqProvider, orderEnum) -> jooqProvider.addSort(DSL.field("title"), orderEnum))
85101
)
86102
.field("fullName", field -> field
87103
.setDisplaySupplier((entity, context) ->

sample/src/main/java/com/github/PierreAdam/javadatatables/sample/MainClass.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import io.undertow.util.Headers;
3434
import lombok.extern.slf4j.Slf4j;
3535

36-
import java.io.InputStream;
36+
import java.io.File;
3737
import java.nio.charset.StandardCharsets;
3838
import java.util.Scanner;
3939

@@ -56,6 +56,9 @@ public class MainClass {
5656
*/
5757
private final Undertow server;
5858

59+
/**
60+
* The Object mapper.
61+
*/
5962
private final ObjectMapper objectMapper;
6063

6164
/**
@@ -119,15 +122,25 @@ public void run() throws InterruptedException {
119122
*/
120123
public void index(final HttpServerExchange exchange) {
121124
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
122-
try (final InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("index.html");
123-
final Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8)) {
125+
Scanner scanner = null;
126+
127+
try {
128+
if (true) {
129+
scanner = new Scanner(new File("sample/src/main/resources/index.html"), StandardCharsets.UTF_8);
130+
} else {
131+
scanner = new Scanner(this.getClass().getClassLoader().getResourceAsStream("index.html"), StandardCharsets.UTF_8);
132+
}
133+
124134
final String content = scanner.useDelimiter("\\A").next();
125135
exchange.getResponseSender().send(content);
126136
} catch (final Exception e) {
127137
MainClass.logger.error("Failed to load index.html", e);
128138
exchange.setStatusCode(500);
129139
exchange.getResponseSender().send("Internal Server Error");
130140
} finally {
141+
if (scanner != null) {
142+
scanner.close();
143+
}
131144
exchange.endExchange();
132145
}
133146
}

sample/src/main/resources/index.html

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
<!--
2+
~ The MIT License (MIT)
3+
~
4+
~ Copyright (c) 2025 Pierre Adam
5+
~
6+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
7+
~ of this software and associated documentation files (the "Software"), to deal
8+
~ in the Software without restriction, including without limitation the rights
9+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
~ copies of the Software, and to permit persons to whom the Software is
11+
~ furnished to do so, subject to the following conditions:
12+
~
13+
~ The above copyright notice and this permission notice shall be included in all
14+
~ copies or substantial portions of the Software.
15+
~
16+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
~ SOFTWARE.
23+
-->
24+
125
<!DOCTYPE html>
226
<html lang="en">
327
<head>
@@ -12,7 +36,7 @@
1236
<script src="https://cdn.datatables.net/1.13.5/js/jquery.dataTables.min.js"></script>
1337
<script>
1438
$(document).ready(function () {
15-
$('#example').DataTable({
39+
const table = $('#example').DataTable({
1640
serverSide: true,
1741
processing: true,
1842
searching: true,
@@ -25,12 +49,49 @@
2549
}
2650
},
2751
columns: [
52+
{
53+
className: 'dt-control',
54+
orderable: false,
55+
data: null,
56+
defaultContent: ''
57+
},
2858
{data: "firstName", orderable: true, searchable: true},
2959
{data: "lastName", orderable: true, searchable: true},
3060
{data: "title", orderable: true, searchable: true},
3161
{data: "bloodGroup", orderable: false, searchable: true},
3262
{data: "active", orderable: false, searchable: false}
33-
]
63+
],
64+
order: [[1, 'asc']]
65+
});
66+
67+
function format(d) {
68+
// `d` is the original data object for the row
69+
return (
70+
'<dl>' +
71+
'<dt>Full name:</dt>' +
72+
'<dd>' +
73+
d.fullName +
74+
'</dd>' +
75+
'<dt>UUID:</dt>' +
76+
'<dd>' +
77+
d.uid +
78+
'</dd>'
79+
);
80+
}
81+
82+
table.on('click', 'td.dt-control', function (e) {
83+
let tr = e.target.closest('tr');
84+
let jqData = $(tr).data();
85+
let row = table.row(tr);
86+
let fullDtData = row.data();
87+
88+
if (row.child.isShown()) {
89+
// This row is already open - close it
90+
row.child.hide();
91+
} else {
92+
// Open this row
93+
row.child(format(jqData)).show();
94+
}
3495
});
3596
});
3697
</script>
@@ -41,6 +102,7 @@ <h1>DataTables Example</h1>
41102
<table class="display" id="example" style="width:100%">
42103
<thead>
43104
<tr>
105+
<th style="width: 30px"></th>
44106
<th>First Name</th>
45107
<th>Last Name</th>
46108
<th>Title</th>

0 commit comments

Comments
 (0)