Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does data-sort-name in Bootstrap table Js work?

I am using folowing library : http://bootstrap-table.wenzhixin.net.cn/documentation/

I load json objects into this table which works fine, but now here comes the problem. I want to be able to sort columns.

My Json layout as folows :

[{"Total": 12345.56, "Name": "Monkey1", "TotalFormatted": "$ 12.345,56"},{"Total": 13345.56, "Name": "Monkey3", "TotalFormatted": "$ 13.345,56"},{"Total": 11345.56, "Name": "Monkey2", "TotalFormatted": "$ 11.345,56"}]

  <table id="test" data-page-size="10" data-pagination="true" data-unique-id="true" data-show-footer="false">
                <thead>
                    <tr>
                        <th data-field="Name">Name</th>
                        <th data-field="TotalFormatted" data-sort-name="Total" data-sortable="true" data-align="right">TotalFormatted</th>
                    </tr>
                </thead>
            </table>

I want to show TotalFormatted data, but i want to sort this column with Total property, since TotalFormatted cant be used for that. In the documentation i saw the following :

data-sort-name : Provide a customizable sort-name, not the default sort-name in the header, or the field name of the column. For example, a column might display the value of fieldName of "html" such as "abc", but a fieldName to sort is "content" with the value of "abc".

but how ever data is not sorted correctly, has anyone exprienced this or have i misunderstood something?

like image 343
Timsen Avatar asked Jul 20 '15 13:07

Timsen


People also ask

How do I sort data in bootstrap table?

The sort table needed a bootstrap table with a JavaScript method. There is two ways to sort table data which is below. The first way helps to sort table data automatically using the DataTable() method. The second way helps the user to sort table data as per requirement.

How do you sort data in a table in HTML?

Adding the "sortable" class to a <table> element provides support for sorting by column value. Clicking the column headers will sort the table rows by that column's value. Tables must use <thead> and <th> tags for sortable functionality to work. The <th> tag defines a header cell in an HTML table.


1 Answers

Actually data-sort-name doesn't work that way. the main intention of data-sort-name option is to control the default sorting of the table data.

for the data-sort-name option to work with default sorting it needs to point to one of the data-field attribute of the column in the table.

note : In short data-field is like a an id added to each column, which the data-sort-name option refers to sort the table on load.

To understand this better, here is an example with code from Bootstrap site

  • try changing the data-sort-name to one of the columns data-field value and execute the code, you will understand what I just explained above.

HTML Code:

<table data-toggle="table"
   data-url="https://api.github.com/users/wenzhixin/repos?type=owner&sort=full_name&direction=asc&per_page=100&page=1"
   data-sort-name="stargazers_count"
   data-sort-order="desc">
<thead>
<tr>
    <th data-field="name" 
        data-sortable="true">
            Name
    </th>
    <th data-field="stargazers_count" 
        data-sortable="true">
            Stars
    </th>
    <th data-field="forks_count" 
        data-sortable="true">
            Forks
    </th>
    <th data-field="description" 
        data-sortable="true">
            Description
    </th>
</tr>
</thead>

Live demo @ JSFIDDLE: http://jsfiddle.net/dreamweiver/ptxj8Lao/

like image 138
dreamweiver Avatar answered Sep 28 '22 07:09

dreamweiver