Skip to content

Commit f9c2dce

Browse files
committed
Add column type percentage.
1 parent e4eea94 commit f9c2dce

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ public enum ColumnCss {
161161
* ago", rather than an absolute time. A tooltip is available that shows the absolute time.
162162
*/
163163
DATE("date"),
164+
/**
165+
* Percentages (values in the interval [0,1]) will be rendered correctly as a percentage using the native
166+
* JS locale sensitive rendering.
167+
*/
168+
PERCENTAGE("percentage"),
164169
/**
165170
* Numbers will be shown right aligned, so they can be compared more easily.
166171
*/

src/main/webapp/js/table.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ jQuery3(document).ready(function () {
4141
}
4242
}
4343
},
44+
{
45+
targets: 'percentage', // All columns with the '.percentage' class in the <th>
46+
render: function (data, type, _row, _meta) {
47+
if (isNaN(data)) {
48+
return data;
49+
}
50+
return Number(data).toLocaleString(undefined,
51+
{style: 'percent', minimumFractionDigits: 2});
52+
}
53+
},
4454
{
4555
targets: 'hidden', // All columns with the '.hidden' class in the <th>
4656
visible: false,

0 commit comments

Comments
 (0)