Click here to Skip to main content
15,914,419 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 647.4K   20.1K   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

 
Question¿How can I edit the html that appears when I implement the javascript? Pin
Hilary Madrigal Valverde4-Jul-15 13:09
Hilary Madrigal Valverde4-Jul-15 13:09 
Hello,

Im relatively new at web programming.
My project is in codeigniter.

I implemented this table functionality (paging, search and responsive table) just as you show and it works, perfectly! What I did was, I made a new view, copied and pasted the code that you have here, implemented the js and css that you posted and added the script as well.

The thing is, I want to edit the style of the labels "Show entries" and "search", problem is I dont know where they are.

For example when I "inspect the elemnt" using firefox web browser, I can see that there is a lot of html that doesn't appear in the html that I copied and paste on my view. How can I access that in order to add a class to the label and then edit it in the css.

This is how appears when I "inspect the element":


Show

entries


What I want to do is asign a class to the label like: Same stuff inside

Where Can I edit that Html that doesn appear on my view... where is it?

Thanks for your help
QuestionReduce entries lesser than 10 Pin
Member 116652875-May-15 23:29
Member 116652875-May-15 23:29 
AnswerRe: Reduce entries lesser than 10 Pin
semanticindia9-Aug-15 22:30
semanticindia9-Aug-15 22:30 
BugCode gives me error Pin
priyanka naidu9-Apr-15 23:43
priyanka naidu9-Apr-15 23:43 
GeneralRe: Code gives me error Pin
Member 1164781227-Apr-15 21:55
Member 1164781227-Apr-15 21:55 
QuestionHow to implement it in codeigniter Pin
dauruk051228-Jan-15 18:07
dauruk051228-Jan-15 18:07 
AnswerRe: How to implement it in codeigniter Pin
Hilary Madrigal Valverde4-Jul-15 13:12
Hilary Madrigal Valverde4-Jul-15 13:12 
QuestionImages Pin
Member 113562686-Jan-15 5:10
Member 113562686-Jan-15 5:10 
AnswerRe: Images Pin
Rapuru Amarendra7-Jan-15 3:22
Rapuru Amarendra7-Jan-15 3:22 
GeneralNeed server side code for datatable Pin
Amit Kaushal25-Nov-14 0:40
Amit Kaushal25-Nov-14 0:40 
GeneralRe: Need server side code for datatable Pin
Rapuru Amarendra25-Nov-14 1:11
Rapuru Amarendra25-Nov-14 1:11 
QuestionAbout using this Pin
Member 989755416-Nov-14 6:19
Member 989755416-Nov-14 6:19 
AnswerRe: About using this Pin
Rapuru Amarendra16-Nov-14 6:26
Rapuru Amarendra16-Nov-14 6:26 
GeneralRe: About using this Pin
Member 989755416-Nov-14 6:43
Member 989755416-Nov-14 6:43 
GeneralRe: About using this Pin
Rapuru Amarendra16-Nov-14 6:50
Rapuru Amarendra16-Nov-14 6:50 
GeneralRe: About using this Pin
Member 989755416-Nov-14 7:15
Member 989755416-Nov-14 7:15 
GeneralRe: About using this Pin
Rapuru Amarendra16-Nov-14 7:22
Rapuru Amarendra16-Nov-14 7:22 
GeneralRe: About using this Pin
Member 989755416-Nov-14 7:40
Member 989755416-Nov-14 7:40 
QuestionTableTools and Editor Pin
Koushik Saha16-Nov-14 3:57
Koushik Saha16-Nov-14 3:57 
Questionhow can i add dynamic data to this table Pin
Member 1118579027-Oct-14 23:01
Member 1118579027-Oct-14 23:01 
AnswerRe: how can i add dynamic data to this table Pin
Rapuru Amarendra28-Oct-14 3:59
Rapuru Amarendra28-Oct-14 3:59 
GeneralRe: how can i add dynamic data to this table Pin
Member 1127098828-Nov-14 0:19
Member 1127098828-Nov-14 0:19 
GeneralMy vote of 4 Pin
Gun Gun Febrianza23-Oct-14 23:03
Gun Gun Febrianza23-Oct-14 23:03 
QuestionBootstrap pagination with First and last option. Pin
Praveen Raj S17-Oct-14 1:45
Praveen Raj S17-Oct-14 1:45 
QuestionHow does this behave when we have multiple columns Pin
Sreekanth Mothukuru26-Sep-14 22:54
Sreekanth Mothukuru26-Sep-14 22:54 

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.