Download
Demo
- Overview
- Documents
User Rating: 0/5 ( 0 votes)
Your Rating:
Datatable is a jQuery plugin for dynamic datatables with pagination, filtering and ajax loading.
Source: github.com
1. INCLUDE CSS AND JS FILES
<link rel="stylesheet" type="text/css" media="screen" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css"> <link rel="stylesheet" type="text/css" media="screen" href="stylesheets/datatable-bootstrap.min.css"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript" src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script> <script type="text/javascript" src="javascripts/datatable.min.js"></script>
2. HTML
<div class="paging"></div> <table> <thead> <tr> <th>Firstname</th> <th>Lastname</th> <th></th> </tr> </thead> <tbody> <tr> <td>Linus</td> <td>Torvalds</td> <td>Computer Science</td> </tr> <tr> <td>Brian</td> <td>Kernighan</td> <td>Computer Science</td> </tr> <tr> <td>Blaise</td> <td>Pascal</td> <td>Mathematics</td> </tr> <tr> <td>Larry</td> <td>Page</td> <td>Computer Science</td> </tr> <tr> <td>Richard</td> <td>Hamming</td> <td>Mathematics</td> </tr> <tr> <td>Grace</td> <td>Hopper</td> <td>Computer Science</td> </tr> <tr> <td>Pierre</td> <td>Bezier</td> <td>Mathematics</td> </tr> <tr> <td>Shigeru</td> <td>Miyamoto</td> <td>Computer Science</td> </tr> <tr> <td>Leslie</td> <td>Lamport</td> <td>Computer Science</td> </tr> <tr> <td>Rasmus</td> <td>Lerdorf</td> <td>Computer Science</td> </tr> </tbody> </table> <div class="paging"></div>
3. JAVASCRIPT
$('#example-table').datatable({ pageSize: 5, sort: [true, true, false], filters: [true, false, 'select'], filterText: 'Type to filter... ' }) ;
4. OPTIONS
The current available options that can be passed to datatable are shown in the following table:
Option | Default value | Explanation | |
---|---|---|---|
data | See the data part below for more information. | ||
tableClass | 'datatable' | The HTML class for the table. | |
pagingDivSelector | '.paging' | The jQuery selector for paging divs. | |
pagingDivClass | 'pagination pagination-centered' | The HTML class which will be added to the paging div. | |
pagingListClass | '' | The HTML class which will be added to the <ul> tag inside the paging div. | |
counterDivSelector | '.loading' | The jQuery selector for counter divs. | |
counterText | See below. | ||
loadingDivSelector | '.loading' | The jQuery selector for the ajax loading div. | |
sort | false | See the sort part below for more information. | |
sortKey | undefined | The default sort key (if sort enable). | |
sortDir | 'asc' | The default sort direction ('asc' or 'desc'). | |
nbColumns | -1 | The number of columns. If not specified will be find according to the number of <th> tags in the head of the table. | |
pageSize | 20 | The number of elements that will be shown on each page. | |
identify | false | The key to use for check if 2 element are equals. Can also be a function. | |
pagingNumberOfPages | 9 | The number of pages that will be shown on the pagination list. | |
filters | See the filter part below for more information. | ||
filterText | Search... | The placeholder for the filter input. | |
filterInputClass | '' | The HTML class for search input. | |
filterSelectClass | '' | The HTML class for search select. | |
onChange | function (old, new) {} | A callback call before page is changed. | |
beforeRefresh | function () {} | A callback call before each refresh. | |
afterRefresh | function () {} | A callback call after each refresh. | |
lineFormat | See below. | ||
firstPage | << | First page link text (set to false to do not show the link). | |
prevPage | < | Previous page link text (set to false to do not show the link). | |
nextPage | > | Next page link text (set to false to do not show the link). | |
lastPage | >> |
Last page link text (set to false to do not show the link). |
5. COMMANDS
Some commands are available with datatable plugin, you can call a command like this way:
$('#MyTable').datatable(command, args) ;
Available commands are:
Command | Args | Info |
destroy | Remove the datatable. | |
option | Option name, option value | Set a option value (the data options can not be set this way). |
page | Page number | If a argument is specified, load the specified page, otherwize return current page number. |
select | ID | Retrieve the first element found with the specified ID. |
insert | Element | Add the specified element to the data array. |
update | ID, Element | Update the first element found with the specified ID with the new specified data. |
delete | ID | Remove the first element found with the specified ID. |
Some options required an ID, how element are compared is specified by identify option. Ifidentify is a string, then element are compared using this key. If identify is a function, 2 elements are equals if identify returns true. Examples:
/* EXAMPLE 1 */ /* Two elements E1 and E2 are equals if E1.id === E2.id. */ $('#MyTable').datatable({identify: 'id'}) ; /* Delete the first element E found such as E.id === 4. */ $('#MyTable').datatable('delete', 4) ; /* EXAMPLE 2 */ $('#MyTable').datatable({identify: function (id, E) { return id.toUpperCase() === (E.firstname.toUpperCase() + ' ' + E.lastname.toUpperCase() ; }}) ; /* Update ISAAC NEWTON. The update function will update all the field contains in the second parameter. */ $('#MyTable').datatable('update', 'ISAAC NEWTON', {field: "Mathematics"}) ;