GithubHelp home page GithubHelp logo

key-lime-box / dynamic-table Goto Github PK

View Code? Open in Web Editor NEW
30.0 12.0 13.0 1.42 MB

Dynamic Table jQuery Plug-in - A table that can be sorted, filtered and edited, similar to common spreadsheet application.

Home Page: http://code.keylimebox.org/dynamic-table/

License: MIT License

CSS 5.05% JavaScript 94.95%
jquery javascript jquery-ui spreadsheet dynamic-table filter grid grid-editor data-grid

dynamic-table's Introduction

Dynamic Table - jQuery Plug-in

Introduction

The Dynamic Table is a table that displays data in a similar way to a spreadsheet but allows the data to be loaded from your backend and control over what can be edited and how it is being saved.

It allows advanced filtering and sorting as well as interaction with your application.

Features

  • Spreadsheet feel
  • Load data via AJAX
  • Paged scrolling (handles easily tens of thousands of lines)
  • Filters and Sorting
  • Editable fields
  • Events on select
  • jQuery UI based

Prerequisites

  • jQuery
  • jQuery UI
  • moment.js - This is optional, to allow nicer rendering of dates. However, you have to make sure it is available in the global scope.

If you are using Bootstrap the JQuery-Bootstrap project is recommended to make the components look more like Bootstrap.

Example

To see how the dynamic table works in production see this working sample that goes with the Getting Started section.

Getting Started

0) Import

First import the dynamic-table.jquery.js and any dependencies.

1) Initialize Plug-in

To initialize the component select an empty element, preferably a <div> and apply the jQuery plug-in:

    $("#sample-grid").dynamicTable({
        fillParent : false,
        showCounter: true
    });

Available options are:

  • fillParent (default: true): Automatically keeps the grid the same size as the parent object this is usefull for any sort of full screen applications.
  • showCounter (default: false): Displays a counter column as the first column, which gives the row number.
  • showCheck (default: false): Displays a checkmark at the beginning of the row, that allows the selection of multiple rows. Only works when the counter column is also visible.
  • changeColumns (default: false): If activates an icon shows at the end of the column headers, which allows the user to change the visibility of columns.
  • rowHeight (default: 35): The height of rows in pixels.
  • settingsHandler (default: $.fn.dynamicTable.defaultSettingsHandler): The handler for persisting user updates to the table, such as column widths. See Saving User Settings

2) Define Columns

Next you define the columns as an array of column definitions

var myColumns = [{
                    name       : "Name",
                    type       : "string",
                    visible    : true,
                    filterType : "search",
                    width      : 200
                },
                [...]
                ]

Available options are:

  • id: An unique identifier used when persisting user changes to the column such as column width.
  • name: The name of the column.
  • type (default: "string") : The datatype of the columns. Options are: string, date, number and boolean.
  • field: This is mostly for use when in the data passed in the rows are an object rather than an array, in which case this attribute is used to lookup which value to show in which cell.
  • visible (default: false): Whether or not the column is visible or not. This allows for hiddend data columns, such as ID fields etc.
  • filterType (default: list): The type of filter to display for the column. Options are: list, search and dateRange.
  • width (default: 100): The width of the column in pixels.
  • format: The format used for columns, such as date an number columns. The format is based on the moment.js format.
  • editor: Instance of the editor used to allow this cell to be edited.
  • cssClass : Specify a css class that is applied to this column. If a string is supplied the class is applied to all cells in that column. Alternatively a function can be supplied that returns the class dynamically. The function is of the following format function(aColumn, aValue, aDisplayValue). The aColumn parameter gets passed the column data, the aValue parameter gets passed the original value and the aDisplayValue gets passed the string which is to be rendered.

3) Getting the data

You can get the data using an AJAX call ($.getJSON()) or generate it in your JavaScript.

The table takes and array of rows. These rows can be either arrays or regular objects. In case objects are supplied the field attribute needs to be set on the columns, so the table knows which column to map which field to.

When the rows are arrays the table will map the field by index to the respective column.To hide items in the array, add invisible columns to the column list.

Sample data as array:

var myData = [
   [10001, "Bill Smith", new Date(1956, 3, 12), "United States", "Texas", "English"],
   [10002, "Michael Jones", new Date(1975, 7, 23), "United States", "Florida", "English"],
   [10003, "Heinz Mayer", new Date(1972, 8, 2), "Germany", "Bayern", "German"],
   [10004, "Mary Miller", new Date(1981, 1, 6), "United States", "California", "English"],
   [10005, "Jose Gonzalez", new Date(1959, 1, 6), "Mexico", null, "Spanish"],
];

Sample data as object:

var myData = [
   {id : 10001, name : "Bill Smith", birthDate : new Date(1956, 3, 12).getTime(), country : "United States", state : "Texas", note : "Test", language : "English"},
   {id : 10002, name : "Michael Jones", birthDate : new Date(1975, 7, 23).getTime(), country : "United States", state : "Florida", language : "English"},
   {id : 10003, name : "Heinz Mayer", birthDate : new Date(1972, 8, 2), country : "Germany", state : "Bayern", language : "German"},
]

4) Putting it all together

Now that we have our element on the page, our columns and our data we just need to load it all into the table:

$("#sample-grid").dynamicTable("data", myData, myColumns);

Methods

The following are the methods available. They all are called using the jQuery standard:

$("#sample-grid").dynamicTable("[name]", [parameters...]);
  • data: Loads the data into the table. It takes two mandatory parameters:
    • data The data to be displayed as described earlier
    • columns The column definitions as described earier
    • keepFilters (optional) Whether or not the filters should be kept or reset. Defaults to false.
  • clearAllFilters: Clears all filters that are currently set on the grid. This method takes no arguments.
  • counts: Returns an object with basic counts of the data in the table. The following are the returned attributes:
    • total All records loaded into the table
    • filtered The amount of records currently visible based on the selected filter

Interacting with the table

To allow your application to interact with the table, it dispatches two events:

  • rowSelect: Dispatched when the row gets selected either by keyboard interaction or single click.
  • rowDoubleClick: Dispached when the row gets double-clicked.

The event parameter gets a row property attached, which contains the original data of the affected row.

$("#sample-grid").on("rowSelect", function(aEvent) {
    $("#selected-data").html("You selected <strong>" + aEvent.row[1] + "</strong>");     
});  

Editing content

So far we have only dealt with content loaded from the database that the user cannot interact with.

This is done by editors. These are not natively part of the plugin and come as separate plug-in: dynamic-table-editor.jquery.js

So first you have to import this file.

To instanciate an editor call the following:

var myEditor = $("<div/>").dynamicTableEditor({
    type : "text",
    editHandler: function(aData) {
        // Save here:
        //$.post(
        //    ...
        //);
    }
});

The following options are available:

  • type (default: text): The type of editor. Available types are:
    • text: Simple text field.
    • list: Select list
    • date: Datepicker
  • editHandler: The function that gets called when an edit is complete. This is the point where you save the data back to the server or wherever. This function gets called with two arguments:
    • value: The value of the editor
    • context: Object that contains data on the current column and row.

For list editors only:

  • values: A list of values to be rendered into the list.
  • firstBlank (default: true): Whether to render the first item of the list as blank item.
  • idProperty: If the values are a complex object, this defines which property is the ID that gets returned when selected.
  • nameProperty: If the values are a complex object, this defines which property is the name that gets shown

Sample list editor:

var myEditor = $("<div/>").dynamicTableEditor({
    type : "list",
    values : [{id : 1, name : "Option #1"}, {id : 2, name : "Option #2"}, {id : 3, name : "Option #3"}],
    idProperty : "id",
    nameProperty : "name",
    editHandler: function(aData) {
        // ...
    }
});

Saving User Settings

At times users make adjustments to the table which should be persisted between pageviews and even sessions. This persistence is handled by the SettingsHandlers, which get invoked whenever a user changes the state of a column to save the data and whenever the data gets reloaded to override the defaults with the last data from the user. The basic interface is:

{
    // Called to persist the state of a column
    saveColumn : function (aColumn) { },
    // Gets passed a column and is expected to make the neccesary updates 
    // based on what was saved previously
    updateColumn : function (aColumn) { },
}

To help you a couple of default implementations have been provided.

  • $.fn.dynamicTable.defaultSettingsHandler(aPrefix) This handler is activated by default. It persists user modifications to the localStorage. For this to work best add an id property to each column, whcih will be used to persist the data. The prefix can be set to prefix the key used in the localStorage to prevent potential conflicts.
  • $.fn.dynamicTable.noopSettingsHandler() This handler does nothing and can be used when user changes should not be persisted.
  • $.fn.dynamicTable.callbackSettingsHandler(aSaveColumn, aUpdateColumn) An implementation which allows you to register callback. Both callbacks get passed the column to be saved. It is recommended to only save and update column attributes the user can actually change.

The settings handler can be passed into the options as the settingsHandler property.

Example usage:

    $("#sample-grid").dynamicTable({
        fillParent : false,
        showCounter: true,
        settingsHandler : $.fn.dynamicTable.callbackSettingsHandler(
            function(aColumn) {
                // Save data here
                console.log(aColumn);
            },
            function(aColumn) {
                // Update the column with saved data here
                aColumn.width = 500;
            })
    });

dynamic-table's People

Contributors

ced-b avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dynamic-table's Issues

Json example

Hello,

I am trying to fill the table with a json file but it isn't working. The grid renders each row but the data is blank.

Do you have any sample using JSON? If you do, can you please share with us?

Thank you very much

Bug on selecting/editing cells with big tables

If you scroll to the bottom of big table (>1000 rows, better with 10000 rows), it will be impossible to select cells from top of visible area (it makes "page up" instead of selecting).

If you scroll back to the top, the error disappears. If you scroll to 50%, the area of this effect is smaller.

The bug can be easily reproduced by increasing the number of rows in /sample/index2.html

Sort Up and Sort Down issue

Hello,

After I've implement the AJAX call to get my data from a SQL Server, the Sort UP and Sort Down aren't working anymore.
I've tried to use this with IE v11.0 and Chrome v59 and both are not showing any console error.

Thanks again for ur support.

Using Object Data renders a blank grid

Following the sample code and the documentation I tried to swap out the sample array for an object,

The grid renders each row but the data is blank - I checked the columns map and tried the sample data from the documentation (only replacing the myData in the sample) and it doesn't work.

Is there something in the column definition or setup call that needs changing too or is this a bug?

Change background color of a cell

@ced-b sorry to bother you one more time but I hope this will be the last one.

I am trying to change a cell background color if this cell has a specific text and I am using the code below:

$("#sample-grid").dynamicTable("data", myData, myColumns); $("td:contains('TEXT 1')").addClass('yellowBg'); $("td:contains('TEXT 2')").addClass('greenBg'); $("td:contains('TEXT 3')").addClass('redBg');

and the CSS code:

.greenBg { background-color: green; color: green; } .redBg { background-color: red; color: red; } .yellowBg { background: yellow; color: yellow; }

It is working fine but a have more than 100 rows in this table, and only colors until row 100. Row 101 until the end aren't changing the BG color.
And when I create a filter, any filter, the background returns to the default (white).

Any idea what is it happens?

Thanks again for all your support. Your plugin is amazing and it is helping me a lot!

Change color of a column after filtered

Hello @ced-b

Sorry to bother you but there is a way to change the color of a table header after filtered? For example, if I've selected a option in a specific column, it will change the text color or background color to green?

Thanks one more time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.