Skip to content

Commit d9d87e1

Browse files
authored
Merge pull request #201 from jenkinsci/percentage
Add a percentage column type
2 parents 8995acb + 62d0649 commit d9d87e1

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ jQuery3(document).ready(function () {
4141
}
4242
}
4343
},
44+
{
45+
targets: 'percentage', // All columns with the '.percentage' class in the <th>
46+
className: 'text-end',
47+
render: function (data, type, _row, _meta) {
48+
if (isNaN(data)) {
49+
return data;
50+
}
51+
return Number(data).toLocaleString(undefined,
52+
{style: 'percent', minimumFractionDigits: 2});
53+
}
54+
},
4455
{
4556
targets: 'hidden', // All columns with the '.hidden' class in the <th>
4657
visible: false,

0 commit comments

Comments
 (0)