var $table = $('#table')
var $remove = $('#remove')
var selections = []
document.getElementById("Add").onclick = function () {
location.href = "new.php";
};
function getIdSelections() {
return $.map($table.bootstrapTable('getSelections'), function (row) {
return row.id
})
}
function responseHandler(res) {
$.each (res.rows, function (i, row) {
row.state = $.inArray(row.id, selections) !== -1
})
return res
}
function detailFormatter(index, row) {
var html = []
$.each(row, function (key, value) {
html.push('<p><b>' + key + ':</b> ' + value + '</p>')
})
return html.join('')
}
function operateFormatter(value, row, index) {
return [
'<a class="remove" onclick="return confirm(\'Are you sure you want to delete this vendor?\')" href="deleteVendor.php?vendor_id=' + row.id + '" title="Remove">',
'<i class="fa fa-trash"></i>',
'</a>', '<a class="view" href="viewVendor.php?vendor_id=' + row.id + '" title="View">',
'<i class="fa fa-eye"></i>', '</a>' ].join(''); }
window.operateEvents = {
'click .view': function (e, value, row, index) {
sessionStorage.setItem("id", row);
},
}
function totalTextFormatter(data) {
return 'Total'
}
function totalNameFormatter(data) {
return data.length
}
function totalPriceFormatter(data) {
var field = this.field
return '$' + data.map(function (row) {
return +row[field].substring(1)
}).reduce(function (sum, i) {
return sum + i
}, 0)
}
function initTable() {
$table.bootstrapTable('destroy').bootstrapTable({
height: 700,
locale: $('#locale').val(),
columns: [
[{
field: 'state',
checkbox: true,
rowspan: 2,
align: 'center',
valign: 'middle'
}, {
title: 'Vendor',
colspan: 1,
align: 'center'
}, {
title: 'Vendor Details',
colspan: 8,
align: 'center'
}],
[{
field: 'name',
title: 'Vendor',
sortable: true,
footerFormatter: totalNameFormatter,
align: 'center',
},{
field: 'account_company',
title: 'Account Company',
sortable: true,
footerFormatter: totalNameFormatter,
align: 'center',
}, {
field: 'id',
title: 'Vendor ID',
sortable: true,
footerFormatter: totalNameFormatter,
align: 'center',
}, {
field: 'residual',
title: 'Residual Risk',
sortable: true,
footerFormatter: totalNameFormatter,
align: 'center',
},
{
field: 'company',
title: 'Company',
sortable: true,
align: 'center',
footerFormatter: totalPriceFormatter
}, {
field: 'type',
title: 'Type',
sortable: true,
align: 'center',
footerFormatter: totalPriceFormatter
},{
field: 'status',
title: 'Status',
sortable: true,
align: 'center',
footerFormatter: totalPriceFormatter
},
{
field: 'owner',
title: 'Owner',
sortable: true,
align: 'center',
footerFormatter: totalPriceFormatter
},
{
field: 'operate',
title: 'Operations',
align: 'center',
clickToSelect: false,
events: window.operateEvents,
formatter: operateFormatter
},
]
]
})
$table.on('check.bs.table uncheck.bs.table ' +
'check-all.bs.table uncheck-all.bs.table',
function () {
$remove.prop('disabled', !$table.bootstrapTable('getSelections').length)
// save your data, here just save the current page
selections = getIdSelections()
// push or splice the selections if you want to save all data selections
})
$table.on('all.bs.table', function (e, name, args) {
console.log(name, args)
})
$remove.click(function () {
var ids = getIdSelections()
$table.bootstrapTable('remove', {
field: 'id',
values: ids
})
$remove.prop('disabled', true)
})
}
$(function() {
initTable()
$('#locale').change(initTable)
})
table {
width: 100%;
}
td {
padding: 1em;
border: 1px solid #ccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<table
id="table"
data-toolbar="#toolbar"
data-search="true"
data-show-refresh="true"
data-show-toggle="true"
data-show-fullscreen="true"
data-show-columns="true"
data-show-columns-toggle-all="true"
data-detail-view="true"
data-show-export="true"
data-click-to-select="true"
data-detail-formatter="detailFormatter"
data-minimum-count-columns="2"
data-show-pagination-switch="true"
data-pagination="true"
data-id-field="id"
data-page-list="[10, 25, 50, 100, all]"
data-side-pagination="client"
data-url="ajax.php"
data-response-handler="responseHandler" >
<thead>
<tr>
<td data-field="name">Vendor</td>
<td data-field="account_company">Account Company</td>
<td data-field="id">Vendor ID</td>
<td data-field="residual">Residual Risk</td>
<td data-field="company">Company</td>
<td data-field="type">Type</td>
<td data-field="status">Status</td>
<td data-field="owner">Owner</td>
</tr>
</thead>
<tbody>
<tr>
<td>Some data</td>
<td>Some data</td>
<td>Some data</td>
<td>Some data</td>
<td>Some data</td>
<td>Some data</td>
<td>Some data</td>
<td>Some data</td>
</tr>
</tbody>
</table>