Bootstrap Table Cell Dynamic Background Width
by Greg Esguerra
HTML
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.18.0/bootstrap-table.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.18.0/bootstrap-table.min.js"></script>
<table id="tblDemo" class="table-condensed table-striped">
<thead>
<tr>
<th data-field="id">ID</th>
<th data-field="name">Name</th>
<th data-field="value">Value</th>
<th data-cell-style="PercentBarCellStyle">Percent</th>
</tr>
</thead>
</table>
JavaScript
$(function () {
var $table = $('#tblDemo');
var mydata =
[
{
"id": 1,
"name": "test1",
"value": "15"
},
{
"id": 2,
"name": "test2",
"value": "3"
},
{
"id": 3,
"name": "test3",
"value": "2"
},
{
"id": 4,
"name": "test4",
"value": "6"
},
{
"id": 5,
"name": "test5",
"value": "5"
},
{
"id": 6,
"name": "test6",
"value": "5"
},
{
"id": 7,
"name": "test7",
"value": "22"
},
{
"id": 8,
"name": "test8",
"value": "32"
},
{
"id": 9,
"name": "test9",
"value": "3"
},
{
"id": 10,
"name": "test10",
"value": "4"
},
{
"id": 11,
"name": "test11",
"value": "100"
}
];
$('#tblDemo').bootstrapTable({
data: mydata
});
});
function PercentBarCellStyle(value, row, index) {
var classes = ['active', 'success', 'info', 'warning', 'danger'];
var size = row.value.toString() + "% auto";
return {
css: {
"color": "transparent",
"background-image": "url('https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/Flag_of_Libya_%281977%E2%80%932011%29.svg/1920px-Flag_of_Libya_%281977%E2%80%932011%29.svg.png')",
"background-repeat": "repeat-y",
"background-size": size
}
};
}