Is there any jQuery plugin that applies paging, allows users to select the page size, and manages displaying user's current position in the results (e.g. 'Showing results: 1-5 of 230')? I want to load all the list items into the 'ul' on initial page load, and apply the paging afterword.
DataTables appears to do all of this (and more), but it doesn't work with a 'ul'.
Here is a wireframe screenshot for what I am trying to accomplish:
Any suggestions?
DataTables is a powerful jQuery plugin for creating table listings and adding interactions to them. It provides searching, sorting and pagination without any configuration.
DataTables 1.12. 1 is the current stable release of DataTables.
DataTables has an extensive API which can be used to access the data contained in a table and otherwise manipulate the table after the table initialisation has completed. The DataTables API is designed to reflect the structure of the data in the table and how you will typically interact with the table through the API.
DataTables is free, open source software that you can download and use for whatever purpose you wish, on any and as many sites you want. It is free for you to use!
Another option is to change your UL
/LI
s to a table
on the fly and then attach dataTables()
. This example adds a jQuery extension method called dataList
that converts any target ULs
to a TABLE
(but leaves existing tables alone):
JSFiddle: http://jsfiddle.net/TrueBlueAussie/r5xf61zp/5/
$.fn.dataList = function (options) {
this.each(function () {
var $table = $(this);
if ($table.is('ul')) {
var $ul = $table;
$table = $ul.wrap('<table><tbody/></table').closest('table');
$ul.find('li').wrap('<tr><td/></tr>').contents().unwrap();
$ul.contents().unwrap()
$table.prepend('<thead><tr><th>Heading</th></tr></thead>');
}
$table.dataTable(options);
});
}
And use just like .dataTable()
:
$('.example').dataList({
"sPaginationType": "full_numbers"
});
The only outstanding issue is what to display for the heading (currently Heading
in the example :)
Just revisiting this one and decided the best way to provide a heading would be via a data-heading
attribute on the UL
.
Example in HTML:
<ul class="example" data-heading="My new heading">
Code:
$.fn.dataList = function (options) {
this.each(function () {
var $table = $(this);
if ($table.is('ul')) {
var $ul = $table;
$table = $ul.wrap('<table><tbody/></table').closest('table');
$ul.find('li').wrap('<tr><td/></tr>').contents().unwrap();
$ul.contents().unwrap()
$table.prepend('<thead><tr><th>' + ($ul.data('heading') || '') + '</th></tr></thead>');
}
$table.dataTable(options);
});
}
JSFiddle: http://jsfiddle.net/TrueBlueAussie/r5xf61zp/8/
After further research I have not found an equivalent to DataTables that works with <ul>
s.
Because the website is internal website and DataTables is so robust, I went with DataTables. The page renders a single-column table, and the styling is tweaked to hide the table header and footer. In the end, it looked exactly like the wireframe above. It's just a shame that the markup has to be polluted with the ugly <table>
tags.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With