Click here to Skip to main content
15,881,841 members
Articles / Web Development / HTML
Tip/Trick

Bootstrap Table With Sorting, Searching and Paging using dataTable.js (Responsive)

Rate me:
Please Sign up or sign in to vote.
4.86/5 (33 votes)
26 Sep 2014CPOL1 min read 643.5K   20K   37   51
This tip presents an example of datatable in responsive using bootstrap and dataTable.js

Introduction

This tip presents an example of DataTable in Responsive using bootstrap. The DataTable.js automatically provides column sorting, searching and paging. DataTable.js is just like a .js file. It's open source. In many of the applications, we need to display data as a table format so in this scenario, if we will use this library then it reduces our work and it increases the efficiency.

Background

DataTable.JS

DataTable.js is just like a JavaScript library we can use in any web related projects. It automatically provides column sorting, searching and paging functionalities.

Bootstrap

Bootstrap is the most popular for HTML, CSS and JS framework for developing responsive applications.

Responsive

Responsive means the single application will target any device like mobile, tablet, small PC and large PC.

Using the Code

Here is the code to display dataTable in responsive mode.

The JQuery is the top of all the .js libraries.

First, we need to include the required libraries.

HTML
//
// These are required files we must include these libraries 
//

<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">   
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<link rel="stylesheet" 
href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css"></style>
<script type="text/javascript" 
src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" 
src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

After including all required libraries, put your HTML table either static or dynamic.

Here I am showing static HTML table. If your table is dynamic, it is also the same.

HTML
//
// Table with data
//
<table id="myTable">  
        <thead>  
          <tr>  
            <th>ENO</th>  
            <th>EMPName</th>  
            <th>Country</th>  
            <th>Salary</th>  
          </tr>  
        </thead>  
        <tbody>  
          <tr>  
            <td>001</td>  
            <td>Anusha</td>  
            <td>India</td>  
            <td>10000</td>  
          </tr>  
          <tr>  
            <td>002</td>  
            <td>Charles</td>  
            <td>United Kingdom</td>  
            <td>28000</td>  
          </tr>  
          <tr>  
            <td>003</td>  
            <td>Sravani</td>  
            <td>Australia</td>  
            <td>7000</td>  
          </tr>  
           <tr>  
            <td>004</td>  
            <td>Amar</td>  
            <td>India</td>  
            <td>18000</td>  
          </tr>  
          <tr>  
            <td>005</td>  
            <td>Lakshmi</td>  
            <td>India</td>  
            <td>12000</td>  
          </tr>  
          <tr>  
            <td>006</td>  
            <td>James</td>  
            <td>Canada</td>  
            <td>50000</td>  
          </tr>  
          
           <tr>  
            <td>007</td>  
            <td>Ronald</td>  
            <td>US</td>  
            <td>75000</td>  
          </tr>  
          <tr>  
            <td>008</td>  
            <td>Mike</td>  
            <td>Belgium</td>  
            <td>100000</td>  
          </tr>  
          <tr>  
            <td>009</td>  
            <td>Andrew</td>  
            <td>Argentina</td>  
            <td>45000</td>  
          </tr>  
          
            <tr>  
            <td>010</td>  
            <td>Stephen</td>  
            <td>Austria</td>  
            <td>30000</td>  
          </tr>  
          <tr>  
            <td>011</td>  
            <td>Sara</td>  
            <td>China</td>  
            <td>750000</td>  
          </tr>  
          <tr>  
            <td>012</td>  
            <td>JonRoot</td>  
            <td>Argentina</td>  
            <td>65000</td>  
          </tr>  
        </tbody>  
      </table>  

Finally, after creating table, we need to add some jquery code for dataTable functionalites to our table id.

HTML
//
// This only line code describes to bind datatable functionalities like searching, sorting and paging to our table.
// Here 'myTable' is the ID of our table
//
 
<script>
$(document).ready(function(){
    $('#myTable').dataTable();
});
</script>

Now our dataTable is ready with sorting, searching and paging.

Now we need to make the table as responsive. For that, we need to include bootstrap CSS class 'table table-striped'.

HTML
<table id="myTable" class="table table-striped" >  

or we have another method to do the same thing in responsive:

HTML
<div class="table-responsive">
<table id="myTable" class="display table" width="100%" >

//The table data come here...

</table>
</div>

That's it! The output looks like this in PC.

Image 1

The output in Smart phone is like this:

sampleoutput in mobile

Points of Interest

I love this very much. It's much simpler and it saves more time.

History

  • 26-09-2014

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 5 Pin
Prashant Verma 202429-Mar-24 6:04
Prashant Verma 202429-Mar-24 6:04 
QuestionEdit gridview by keeping the pagination Pin
Member 1011261112-Aug-19 23:50
Member 1011261112-Aug-19 23:50 
QuestionRegarding different behavior of chrome w.r.t script Pin
Member 1401967714-Oct-18 22:31
Member 1401967714-Oct-18 22:31 
Bugpagination not working how can i solve it Pin
Member 1353719423-Nov-17 1:16
Member 1353719423-Nov-17 1:16 
QuestionNot working Pin
Member 1353719423-Nov-17 0:08
Member 1353719423-Nov-17 0:08 
PraiseArticle Pin
Gautam Chaudhari26-Sep-17 20:02
Gautam Chaudhari26-Sep-17 20:02 
QuestionSQL data binding to grid Pin
Member 80776445-Aug-17 21:02
Member 80776445-Aug-17 21:02 
QuestionBootstrap Table With Sorting, Searching and Paging Pin
Darrell Ulm20-Jun-17 9:37
Darrell Ulm20-Jun-17 9:37 
AnswerRe: Bootstrap Table With Sorting, Searching and Paging Pin
Member 1176957813-Mar-18 6:22
Member 1176957813-Mar-18 6:22 
QuestionBootstrap Table With Sorting, Searching and Paging using dataTable.js Pin
Member 1304128326-Mar-17 11:59
Member 1304128326-Mar-17 11:59 
In trying to use dataTable.js I followed your example and it worked at first but when I used Ajax to collect data it does not work.

I initiate a call to the db by clicking the ERF button in the main menu. This opens a page with the tabs. First tab is the ERFS table and the other is the Map of the ERFs displayed in the table.

The table does show but the dataTable work. Sigh | :sigh:

Where can I post my code so you can see and assist please?
QuestionNot working in Chrome - Bootstrap Table With Sorting, Searching and Paging using dataTable.js (Responsive) Pin
Member 1173388422-Feb-17 3:13
Member 1173388422-Feb-17 3:13 
AnswerRe: Not working in Chrome - Bootstrap Table With Sorting, Searching and Paging using dataTable.js (Responsive) Pin
Member 1304128326-Mar-17 12:01
Member 1304128326-Mar-17 12:01 
Questiondisabling sorting for one column Pin
Member 97662734-Oct-16 21:06
Member 97662734-Oct-16 21:06 
Questionrelated to table data Pin
Member 1274267115-Sep-16 16:49
Member 1274267115-Sep-16 16:49 
GeneralMy vote of 5 Pin
srfox22-Aug-16 17:17
professionalsrfox22-Aug-16 17:17 
QuestionDynamic Data will not refresh Pin
Member 1249152728-Apr-16 8:57
Member 1249152728-Apr-16 8:57 
QuestionTable With Sorting, Searching and Paging Pin
renjithstephen10-Mar-16 23:06
renjithstephen10-Mar-16 23:06 
QuestionHTML Table bind through Json. Pin
Abhisek Munshi16-Dec-15 1:42
Abhisek Munshi16-Dec-15 1:42 
Questionnot working in .cshtml Pin
Baraneedharan(Baranee)15-Dec-15 5:26
Baraneedharan(Baranee)15-Dec-15 5:26 
PraiseGreat article Pin
Rajdeep Debnath15-Dec-15 1:30
Rajdeep Debnath15-Dec-15 1:30 
Questionhow to refresh table after updating rows by ajax? Pin
Richard_Michael_RM14-Dec-15 23:14
Richard_Michael_RM14-Dec-15 23:14 
QuestionVerrry Useful Post !! Helped me a lot !! Pin
Member 1215166919-Nov-15 0:40
Member 1215166919-Nov-15 0:40 
QuestionLosing results if dynamically loaded Pin
floydus2714-Nov-15 11:31
floydus2714-Nov-15 11:31 
QuestionHow to make it work in "Grayscale" bootstrap template? Pin
Jacek Miłaszewski28-Sep-15 0:07
Jacek Miłaszewski28-Sep-15 0:07 
QuestionWhere to add? Pin
Member 1179634110-Sep-15 20:48
Member 1179634110-Sep-15 20:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.