Skip to content

Commit ed3a207

Browse files
authored
Merge pull request #263 from fo-code/data-tables-select
Select option for data tables
2 parents 2b1be1a + c8446c6 commit ed3a207

File tree

8 files changed

+151
-0
lines changed

8 files changed

+151
-0
lines changed

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"datatables.net-buttons-bs5": "^2.2.2",
1111
"datatables.net-colreorder-bs5": "^1.5.5",
1212
"datatables.net-responsive-bs5": "^2.2.9",
13+
"datatables.net-select-bs5": "^1.3.4",
1314
"datatables.mark.js": "^2.1.0",
1415
"mark.js": "^8.11.1",
1516
"remark-cli": "^10.0.1",

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@
156156
<directory>${project.basedir}/node_modules/datatables.net-responsive-bs5/js</directory>
157157
<filtering>false</filtering>
158158
</resource>
159+
<resource>
160+
<directory>${project.basedir}/node_modules/datatables.net-select/js</directory>
161+
<filtering>false</filtering>
162+
</resource>
163+
<resource>
164+
<directory>${project.basedir}/node_modules/datatables.net-select-bs5/js</directory>
165+
<filtering>false</filtering>
166+
</resource>
159167
<resource>
160168
<directory>${project.basedir}/node_modules/datatables.mark.js/dist</directory>
161169
<filtering>false</filtering>
@@ -198,6 +206,10 @@
198206
<directory>${project.basedir}/node_modules/datatables.net-responsive-bs5/css</directory>
199207
<filtering>false</filtering>
200208
</resource>
209+
<resource>
210+
<directory>${project.basedir}/node_modules/datatables.net-select-bs5/css</directory>
211+
<filtering>false</filtering>
212+
</resource>
201213
</resources>
202214
</configuration>
203215
</execution>

src/main/java/io/jenkins/plugins/datatables/TableConfiguration.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.core.JsonProcessingException;
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
9+
import io.jenkins.plugins.datatables.options.SelectStyle;
910

1011
/**
1112
* Provides a configuration for the whole DataTable. This is merged with a default configuration in table.js.
@@ -18,6 +19,7 @@ public class TableConfiguration {
1819
private boolean useResponsive = false;
1920
private boolean useColReorder = false;
2021
private boolean useButtons = false;
22+
private boolean useSelect = false;
2123

2224
/**
2325
* Make the table responsive, i.e. the columns wrap over to a child column.
@@ -101,6 +103,31 @@ public boolean isUseButtons() {
101103
return useButtons;
102104
}
103105

106+
/**
107+
* Enable selection.
108+
*
109+
* @param selectStyle
110+
* The {@link SelectStyle selection style}
111+
*
112+
* @return this {@link TableConfiguration} for chaining methods
113+
*
114+
* @see <a href="https://datatables.net/reference/option/select">https://datatables.net/reference/option/select</a>
115+
*/
116+
public TableConfiguration select(final SelectStyle selectStyle) {
117+
configuration.put("select", selectStyle.getStyle());
118+
useSelect = true;
119+
return this;
120+
}
121+
122+
/**
123+
* Returns whether select is configured to be used.
124+
*
125+
* @return true, if select should be used, false otherwise
126+
*/
127+
public boolean isUseSelect() {
128+
return useSelect;
129+
}
130+
104131
/**
105132
* Enable saving of the table state.
106133
*
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.jenkins.plugins.datatables.options;
2+
3+
/**
4+
* The possible values of the <a href="https://datatables.net/reference/option/select.style">select.style</a> option,
5+
* which is used for enabling the selection for datatables and setting its style (the default is {@link #OS}.
6+
*
7+
* @author Florian Orendi
8+
*/
9+
public enum SelectStyle {
10+
11+
/**
12+
* Selection can only be performed via the API.
13+
*/
14+
API("api"),
15+
16+
/**
17+
* Only a single item can be selected, any other selected items will be automatically
18+
* deselected when a new item is selected.
19+
*/
20+
SINGLE("single"),
21+
22+
/**
23+
* Multiple items can be selected. Selection is performed by simply clicking on the items to be selected.
24+
*/
25+
MULTI("multi"),
26+
27+
/**
28+
* Operating System (OS) style selection.
29+
* This is the most comprehensive option and provides complex behaviours such as ctrl/cmd clicking
30+
* to select / deselect individual items, shift clicking to select ranges
31+
* and an unmodified click to select a single item.
32+
*/
33+
OS("os"),
34+
35+
/**
36+
* A hybrid between the {@link #OS} style and {@link #MULTI},
37+
* allowing easy multi-row selection without immediate de-selection when clicking on a row.
38+
*/
39+
MULTI_SHIFT("multi+shift");
40+
41+
private final String style;
42+
43+
SelectStyle(final String style) {
44+
this.style = style;
45+
}
46+
47+
public String getStyle() {
48+
return style;
49+
}
50+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?jelly escape-by-default='true'?>
2+
<!--
3+
Use it like <st:adjunct includes="io.jenkins.plugins.data-tables-select"/>
4+
-->
5+
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler">
6+
7+
<j:new var="h" className="hudson.Functions"/>
8+
${h.initPageVariables(context)}
9+
10+
<st:adjunct includes="io.jenkins.plugins.jquery3"/>
11+
<st:adjunct includes="io.jenkins.plugins.bootstrap5"/>
12+
<st:adjunct includes="io.jenkins.plugins.data-tables"/>
13+
14+
<link type="text/css" rel="stylesheet" href="${resURL}/plugin/data-tables-api/css/select.bootstrap5.min.css"/>
15+
16+
<script type="text/javascript" src="${resURL}/plugin/data-tables-api/js/dataTables.select.min.js"/>
17+
<script type="text/javascript" src="${resURL}/plugin/data-tables-api/js/select.bootstrap5.min.js"/>
18+
19+
</j:jelly>

src/main/webapp/css/jenkins-style.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,14 @@ div.details-control {
8181
.form-select-sm, .form-control-sm {
8282
margin: 0.2em;
8383
}
84+
85+
/* ------------------------------------------------------------------------------------------------------------------- */
86+
/* DataTables select */
87+
/* ------------------------------------------------------------------------------------------------------------------- */
88+
table.dataTable tbody > tr.selected, table.dataTable tbody > tr > .selected {
89+
background-color: #b4b4b4 !important;
90+
}
91+
92+
table.dataTable tbody tr.selected, table.dataTable tbody th.selected, table.dataTable tbody td.selected {
93+
color: black !important;
94+
}

src/test/java/io/jenkins/plugins/datatables/TableConfigurationTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.jenkins.plugins.datatables;
22

3+
import io.jenkins.plugins.datatables.options.SelectStyle;
34
import org.junit.jupiter.api.Test;
45

56
import static io.jenkins.plugins.datatables.TableConfigurationAssert.*;
@@ -32,6 +33,17 @@ void shouldCreateResponsiveConfiguration() {
3233
assertThat(configuration).isUseResponsive();
3334
}
3435

36+
@Test
37+
void shouldCreateSelectConfiguration() {
38+
TableConfiguration configuration = new TableConfiguration().select(SelectStyle.SINGLE);
39+
40+
assertThat(configuration).hasConfiguration("{\"select\":\"single\"}");
41+
assertThat(configuration).isNotUseButtons();
42+
assertThat(configuration).isNotUseColReorder();
43+
assertThat(configuration).isNotUseResponsive();
44+
assertThat(configuration).isUseSelect();
45+
}
46+
3547
@Test
3648
void shouldCreateColReorderConfiguration() {
3749
TableConfiguration configuration = new TableConfiguration()

0 commit comments

Comments
 (0)