Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Bootstrap table custom sort for one column only

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!