2.0.6 • Published 10 days ago

easytable.js v2.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
10 days ago

EasyTable.js

EasyTable.js is a simple VanillaJS solution for adding pagination, sorting and filtering to HTML tables whether the data is loaded on page render or via AJAX.

Npm package version Npm package total downloads GitHub license JavaScript

Installation

npm install easytable.js

Usage

This example shows how to use EasyTable with AJAX, but you can also load your data with the page turn that table into an EasyTable.

<div>
    <label for="limit">Per Page</label>
    <select id="limit">
        <option value="25">25</option>
        <option value="50">50</option>
        <option value="100">100</option>
        <option value="">All</option>
    </select>
</div>
<div>
    <table id="dataTable">
        <thead class="cm_blue">
            <tr>
                <th class="et-sortable" data-column="name">Name</th>
                <th class="et-sortable" data-column="phone_number">Phone Number</th>
                <th class="et-sortable" data-column="datetime">Date</th>
                <th>Time</th>
            </tr>
        </thead>
        <tbody></tbody>
        <tfoot>
            <tr>
                <td>
                    <div>
                        <p id="page_view_count"></p>
                    </div>
                    <div id="pagination_container"></div>
                </td>
            </tr>
        </tfoot>
    </table>
</div>
window.EasyTable = require('./easytable.js');

let easyTable = new EasyTable('#dataTable', {
    ajax: {
        url: 'url',
        type: 'POST',
        data: {},
        emptyText: 'No Data Found',
        columns: [
            //...
        ],
        handlers: {
            error: function(xhr, status, error) {
                //...
            },
            success: function(result) {
                //...
            },
        },
    },
    countText: 'Showing {PF} - {PL} of {TR} items',
    countTextContainer: '#page_view_count',
    dataType: 'ajax',
    pageSize: $('#limit').val(),
    pageSizeSelector: '#limit',
    pagination: true,
    paginationContainer: '#pagination_container',
    paginationSideLinks: 3,
    sortingIcons: {
        none: 'fas fa-sort',
        asc: 'fas fa-sort-down',
        desc: 'fas fa-sort-up',
    },
    stickyHeader: true,
    stickyHeaderTop: function(element) {
        //
    },
    sorting: true,
});

Options

NameTypeDefaultDescription
ajax.columnsarray[]list of data properties in the order it will be displayed in the table
ajax.dataobject{}AJAX request data
ajax.emptyTextstring"No Records Found"message shown when no data in table
ajax.handlers.errorfunctionfunction(xhr, error, status)AJAX error handler
ajax.handlers.successfunctionfunction(result)AJAX success handler
ajax.sorting.columnstring""column to sort on; from ajax.columns
ajax.sorting.descbooleanfalsesorting direction
ajax.typestring"GET"type of request; supports GET and POST
ajax.urlstring""request URL
countTextstring"Showing {PF} - {PL} of {TR} rows"pagination count text
countTextContainerstring""identifier for count text container
dataTypestring"html"data source ("html", "ajax")
filteringbooleanfalseenables filtering
filteringInputstring""<input/> element for filtering table data
filteringTriggerstring""element that triggers filtering, only supports buttons, defaults to input's input event
pageActiveinteger1which page to start on
pageSizeinteger10page size, set to null to display all data on 1 page
pageSizeSelectorstring""identifier of element that triggers changing the page size, can be any element that triggers a change event, a <select> element is suggested
paginationbooleanfalseenables pagination
paginationContainerstring""identifier for pagination container
paginationNext.arrowstring/function"»"next page button icon
paginationPrevious.arrowstring/function"«"previous page button icon
paginationSideLinksintegernullnumber of buttons to show on each side of active page
stickyHeaderbooleanfalsestick header to top of page on table scroll
stickyHeaderTopstring/function""distance from the top of the page the header is allowed or a function to modify the <th>s
sortingbooleanfalseenables sorting
sortingIcons.nonestring/function""icon for no sorting
sortingIcons.ascstring/function""icon for ASC sorting
sortingIcons.descstring/function""icon for DESC sorting

Methods

EasyTable.getCurrentPage(): returns the current page

EasyTable.setCurrentPage(page = 1): sets the current page (defaults to 1), call EasyTable.refresh() after calling this

EasyTable.getProperties(): gets the current properties of the current instance of EasyTable

EasyTable.loadPage(page = null, reset_sort = false): loads a page via AJAX (page = null will load page 1)

EasyTable.refresh(): redraws EasyTable

Miscellaneous

countText Placeholders

PlaceholderValue
{CP}current page number
{TP}total pages
{PF}position first record on current page
{PL}position last record on current page
{TR}total records

Notes

add the class .et-sortable to <th>s to make a column sortable

add the class .et-filter to <th>s to allow filtering on a column

add data-column attribute with value of column to <th>s when sorting via AJAX

2.0.6

10 days ago

2.0.5

13 days ago

2.0.3

8 months ago

2.0.2

8 months ago

2.0.4

8 months ago

2.0.1

8 months ago

2.0.0

8 months ago

1.1.4

11 months ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago

1.0.0

1 year ago