Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show Bootstrap table with sort icon

Am new to Bootstrap, i have a requirement to show a table with sort up and down arrow near to title of the table. This is my table structure

<table class="table table-bordered table-striped">
        <thead>
        <tr>
            <th><b>#</b>  <i class='icon-arrow-up'></i><i class='icon-arrow-down'></th> **// tried** 
            <th ><b>Name</b></th>
            <th><b>Email</b></th>
            <th><b>Team</b></th>
            <th ><b>Role</b></th>
            <th ><b>Timezone</b></th>
            <th><b>Connections</b></th>
            <th><b># Posts available</b></th>
            <th><b>Last Login</b></th>
            <th><b>Posts</b></th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

I want to display sort up/down arrows similar to the following image. enter image description here

Please help me to solve this issue. Your help is much appreciated. Thank you.

like image 456
Dibish Avatar asked Aug 05 '14 04:08

Dibish


People also ask

How do you sort a table in Javascript?

Sort Table by Clicking the Headers. Click the headers to sort the table. Click "Name" to sort by names, and "Country" to sort by country. The first time you click, the sorting direction is ascending (A to Z).

How do I sort a table column in bootstrap?

For sorting the records in a table, you may use data attributes at the table or <th> tags. Specify the default column that you want to sort as web page loads at table tag: e.g. Similarly, add the data attribute data-sortable=”true” to the columns where you want to enable sorting in that HTML table.

How do you add a sort to a table?

How to Make Sortable Tables. 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.

How can we sort the icons?

To arrange icons by name, type, date, or size, right-click a blank area on the desktop, and then click Arrange Icons. Click the command that indicates how you want to arrange the icons (by Name, by Type, and so on). If you want the icons to be automatically arranged, click Auto Arrange.


2 Answers

You could try using FontAwesome. It contains a sort-icon (http://fontawesome.io/icon/sort/).

To do so, you would

  1. need to include fontawesome:

    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
    
  2. and then simply use the fontawesome-icon instead of the default-bootstrap-icons in your th's:

    <th><b>#</b> <i class="fa fa-fw fa-sort"></i></th>
    

Hope that helps.

like image 121
nozzleman Avatar answered Oct 18 '22 02:10

nozzleman


Use this icons with bootstrap (glyphicon):

<span class="glyphicon glyphicon-triangle-bottom"></span>
<span class="glyphicon glyphicon-triangle-top"></span>

http://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_glyph_triangle-bottom&stacked=h

http://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_glyph_triangle-bottom&stacked=h

like image 17
Dudi Avatar answered Oct 18 '22 02:10

Dudi