Update Row
Updates a row in bootstrap table
by jimkennelly
HTML
<link rel="stylesheet" href="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.css">
<script src="https://rawgit.com/wenzhixin/bootstrap-table/master/src/bootstrap-table.js"></script>
<table id="bsTable" data-toggle="table" data-unique-id="forks_count" data-url="/gh/get/response.json/wenzhixin/bootstrap-table/tree/master/docs/data/data1/">
<thead>
<tr>
<th data-field="name">Name</th>
<th data-field="stargazers_count" data-formatter="formatStars">Stars</th>
<th data-field="forks_count">Forks</th>
<th data-field="description">Description</th>
</tr>
</thead>
</table>
JavaScript
$table = $("#bsTable");
//define an odd/even function
function isOdd(num) {
return num % 2;
}
function myTest() {
var id = $(this).data("id");
var dataRow = $table.bootstrapTable('getRowByUniqueId', id);
dataRow.stargazers_count = parseInt(dataRow.stargazers_count) + 1;
$table.bootstrapTable('updateByUniqueId', id, dataRow);
// alert(dataRow.name);
}
// for any dynamic content with a btnSave class - run the myTest function
$table.on('click', '.btnSave', myTest)
//format the my stars function (odd -> green or even -> orange)
function formatStars(value, row, index) {
return '<button class="btn ' + (isOdd(value) ? 'btn-success' : 'btn-warning') + ' btnSave" data-id="' + row.forks_count + '">' + value + '</button>';
}