I have a bootstrap table with the following code:
<table data-toggle=\"table\" data-url=\"admdata/data2.json\" data-show-refresh=\"true\" data-show-toggle=\"true\" data-show-columns=\"true\" data-search=\"true\" data-pagination=\"true\" data-sort-name=\"id\" data-sort-order=\"desc\" data-custom-sort=\"customSort\">
<thead>
<tr>
<th data-field=\"id\" data-sortable=\"true\">ID</th>
<th data-field=\"name\" data-sortable=\"true\">Имя</th>
<th data-field=\"fname\" data-sortable=\"true\">Фамилия</th>
<th data-field=\"phone\" data-sortable=\"true\">Телефон</th>
<th data-field=\"email\" data-sortable=\"true\">Почта</th>
<th data-field=\"position\" data-sortable=\"true\">Позиция</th>
<th data-field=\"role\" data-sortable=\"true\">Роль</th>
</tr>
</thead>
</table>
<script>
function customSort(sortName, sortOrder, data) {
var order = sortOrder === 'desc' ? -1 : 1
data.sort(function (a, b) {
var aa = +((a[sortName] + '').replace(/[^\d]/g, ''))
var bb = +((b[sortName] + '').replace(/[^\d]/g, ''))
if (aa < bb) {
return order * -1
}
if (aa > bb) {
return order
}
return 0
})
}
</script>
This is a custom solution from the Bootstrap developers. I have one problem, if I include
data-custom-sort="customSort"
in the table tag I get correct sort on numeric ID values but all other columns sorting stops working. I tried putting the data-custom-sort inside th tag but it didn't work. I want ID column to be sorted by custom sort and other columns as usual, any ideas on how to do that?