Get new jQuery plugins just once a week

×

KingTable

FavoriteLoadingAdd to favorites

November 16, 2015

KingTable | A jQuery plugin for administrative tables that build themselves

KingTable is a jQuery plugin for administrative tables that are able to build themselves, on the basis of the input data.

Created by Roberto Prevato

Download

Example

KingTable jQuery plugin

tabular data


jQuery-KingTable

Table widget for administrative tables that are able to build themselves, on the basis of their input data. Supports client and server side pagination; client and server side search; custom filters views; automatic menu to hide and reorder columns and support for custom tools. Client side export feature into: csv, json and xml formats.

Objectives of the plugin

Live demo

The following demos are available online: * Live demo * Demo with complex filters * Demo with datetime filters and datepickers * Double table: two tables displayed in the same page

Tools

Documentation

Refer to the wiki page. A full list of possible options is available inside the dedicated wiki page.

Zepto support

Despite being designed primarily as a jQuery plugin, the KingTable widget supports zepto.js. In order to use it with zepto, the callbacks, deferred and selector plugins are required.

  <script src="scripts/libs/zepto.js"></script>
  <script src="scripts/libs/zepto.callbacks.js"></script>
  <script src="scripts/libs/zepto.deferred.js"></script>
  <script src="scripts/libs/zepto.selector.js"></script>

Modes

The KingTable widget implements two working modes: * fixed (collections that do not require server side pagination) * normal (collections that require server side pagination)

And supports both optimized and simple collections. Refer to the dedicated wiki page for more information.

Fixed mode

A fixed table is displaying a collection that doesn't require server side pagination, but may still benefit from client side pagination. When working on applications, it commonly happens to deal with collections that are not meant to grow over time, and they have a small size. For example, a table of categories in a e-commerce website to sell clothes, or a table of user roles in most applications. In these cases, it makes sense to return whole collections to the client. There are two ways to define a fixed KingTable: * instantiating a KingTable with url from where to fetch a collection; then code the server side to return an array of items * instantiating a KingTable passing a data option with an instance of array: in this case, it is assumed that the collection is fixed

var table = new $.KingTable({
  data: [{...},{...},{...}]
});
//or... code the server side to return an array of items
var table = new $.KingTable({
  url: "/api/categories"
});

Fixed tables perform search and pagination on the client side.

Normal mode

A normal table is one displaying a collection that requires server side pagination, since it is meant to grow over time. This is true in most cases, for example tables of products and customers in a e-commerce website.

var table = new $.KingTable({
  url: "/api/profiles"
});

When receiving an AJAX response, a normal table expects to receive the following structure:

{
  subset: [array],// array of items that respect the given filters
  total: [number] // the total count of items that respect the given filters; excluding the pagination: for example 13000
}

About usability

The jQuery-KingTable widget is designed to follow "old-school" design principles * the user should be able to immediately understand the size of the collection, so the pagination bar is designed to display the total amount of rows; of pages, and the number of results currently displayed * keyboard navigation: the KingTable controls can be navigated using the TAB; it is possible to navigate through pages using the left, right, A and D keys * filters are stored in the page url, so that if the user hit the refresh button, or send a url to another person, the page loads the right results * support for browser navigation buttons * the table logic handles ajax errors and displays a preloader while fetching data

Inline editing feature

Currently the jQuery-KingTable widget doesn't offer, out of the box, inline editing feature. This is intentional, since in most situations we deal with complex objects that cannot be easily edited inline. In any case, the provided plugin makes it easy to configure HTML and event handlers to implement inline editing feature, for example:

//example of custom logic to implement inline-editing feature
$("#table-container").kingtable({
  url: "/api/colors",
  events: {
    "click .color-row": function (e) {
      //e.currentTarget is the clicked element
      //implement here your logic to change the readonly template into an editable template
    }
  },
  columns: {
    color: {
      template: "<div class='color-row'>...</div>"
    }
  }
});

About localization

For full information, refer to the dedicated wiki page. The KingTable widget requires an utility to implement client side localization, which is used to display proper names of buttons (_refresh, page number, results per page, etc.). Currently the KingTable supports these two libraries: * I.js * i18n-js

The first should be used only if advanced features like parsing of dates; support for currencies; are not needed. The second should be used if advanced localization features are required.

How to integrate with your project

In order to use the jQuery-KingTable plugin, there are two options: * download the source code, and work with the R.js modules, like in the provided index.html page * download only the bundled source code (/dist/jquery.kingtable.js); or the minified source (/dist/jquery.kingtable.min.js) It is also necessary to download the Open Iconic fonts and the .css code: once again it is possible to use the .less source code; or download only the compiled .css.

How to integrate with server side code

The KingTable logic defines an interface, in order to offer pagination and search out of the box. When performing AJAX calls to fetch data that requires server side pagination (and therefore, server side sorting and searching), it sends the following information:

{
  fixed: [boolean],   // whether the table requires server side pagination or not
  page: [number],     // page number
  size: [number],     // results per page
  orderBy: [string],  // name of the property to use for sorting
  sortOrder: [string],// asc or desc
  search: [string],   // text search
  timestamp: [number] // the timestamp of the first time the table was rendered: useful for fast growing collections
}

When receiving an AJAX response, it expects the following structure:

{
  subset: [array],// array of items that respect the given filters
  total: [number] // the total count of items that respect the given filters: for example 13000
}

For a working example of server side implementation using Python Flask web framework, and MongoDB, see the Flask-Three-Template repository, spa-humbular branch.

Repository structure

How to run the provided development server

A development server is required for the development of the KingTable plugin, since some features require server side pagination, sorting and filtering (since filtering and sorting affect pagination). Python Flask was chosen because it's a lightweight and convenient web framework (one of my two personal favorites). Following are instructions on how to run a Flask development server.

In order to run the provided development server it is necessary to use Python and Flask (either Python 2.x or 3.x). If necessary, the recommended way is to install a version of Python, which by default include its pip (package management system for Python), then use pip to install Flask. Steps:

# creating a virtual environment in a folder called 'env', using Python 3.x in Ubuntu:
python3 -m venv env

# creating a virtual environment in a folder called 'env', using Python 3.x in Windows:
py -3 -m venv env
# if you activated the virtual environment, you can run simply using:
python server.py

# if you did not activate the virtual environment, you need to call the right Python executable:
env/bin/python server.py

# (or, for Windows users):
env\Scripts\python server.py

Code organization

Dependencies

Development tools

The source code makes use of the following two tools, of my creation: * Knight, html templates packer * Base64, pictures to base64 bulk converter

Themes

The KingTable includes different themes, available in the provided .css files inside the dist folder: Themes

For more information about the themes, refer to the dedicated wiki page.

KingTable

Related posts:

DownloadBuilder-js
pp3Diso
focusable
KingTable jQuery plugin

http://jquer.in/wp-content/uploads/2015/11/KingTable.jpg

jQuery plugin

Sign up for our weekly newsletter.

* We won't spam you ever