High-performance table View for jQuery
npm install jquery.dgtableDGTable.js (please migrate to @danielgindi/dgtable.js)
======================================================
This is a table View for vanilla JS, which is meant to be high-performance, and allow simple user interactions with the UI, such as:
* Sorting
* Sorting by more than one column
* Moving columns
* Resizing columns
* Full cell preview when hovering
* Native RTL support
* Variable row height
Other features implemented are:
* Mix absolute column widths with relative column widths
* Virtual table mode (to supply high performance with hundreds of thousands of rows). This is the default.
* Non-virtual table mode is fully supported, but for giant amounts of data it is not recommended.
* Option to set a fixed width for the table so resizing (relative) columns will still make sure the table will not be less (and/or more) than the specified width.
* Option to have both scrollbars inside the table. (set width: DGTable.Width.SCROLL)
jquery.dgtable users:
* Older jquery.dgtable can either keep using jquery.dgtable, or migrate to this new version which is more lightweight.
* The new version's API is the same as the old one, except that:
* No $el property
* No auto-clear of jQuery data.
* There is now an emit method instead of trigger.
* Event arguments are now always a single value/object.
* Props on DOM elements are now: 'columnName' on a cell, 'index'/'vIndex' on a row, 'columnName'/rowIndex'/'rowVIndex' on a cellPreview
My TODO list:
* TODO: Have a simple and accurate API documentation here in the readme
npm install, and npm install -g grunt-cli to prepare building environment.
grunt style to just test for correct styling.
grunt build or just grunt to compile for release.
var myTable = new DGTable(INIT_OPTIONS).
INIT_OPTIONS
COLUMN_OPTIONS[] (Array of COLUMN_OPTIONS objects)
string Name of the column
string=name Used for the header of this column
string=name Path to the data to show (Defaults to name)
string=name Path to the data to use for comparison (Defaults to dataPath)
number|string
10, 30, 50) will set an absolute width for the column.
'30%') or a 0-1 decimal value (i.e 0.2, 0.7) will set a relative width for the column, out of the full table's width.
0, null etc. will set an automatic width mode, base of the header's content length.
boolean=true Is this column resizable?
boolean=true Is this column sortable?
boolean=true Is this column movable?
boolean=true Is this column visible?
string Classes to add to the DOM element of this cell
boolean=false Should this column ignore the minimum width specified for the table columns?
number Suggested height for the table
DGTable.Width=DGTable.Width.NONE The way that the width of the table will be handled
DGTable.Width.NONE: No special handling
DGTable.Width.AUTO: Sets the width automatically
DGTable.Width.SCROLL: Creates a horizontal scroll when required
boolean=true When set, the table will work in virtual mode, which means only the visible rows are rendered. Rows must have fixed height in this mode.
number? Sets the estimated row height for the table. This is used for virtual table mode, to calculate the estimated scroll size. Will be auto calculated by default.
boolean=true Turns on or off the resizable columns globally.
boolean=true Turns on or off the movable columns globally.
number=1 How many columns can you sort by, one after another?
boolean=true When set, the columns will automatically grow to accommodate for the sort arrow.
boolean=true When set, relative width columns automatically grow to fill the table's width.
boolean=false When set, relative width columns automatically shrink to fill the table's width.
boolean=false When set, auto-width columns are automatically converted to relatives.
boolean=false When set, columns are stretched proportionally to fill the table width (only if there is space left). Will supersede relativeWidthGrowsToFillWidth in the future.
boolean=true When set, the sorting arrows will have 3 modes - asc, desc, and cancelled.
string Classes to add to the DOM element of all cells
string|string[]|COLUMN_SORT_OPTIONS|COLUMN_SORT_OPTIONS[] Columns to sort by
string or a COLUMN_SORT_OPTIONS:
string Column name
boolean=false Is this column sorted in descending order?
Function(string value, string columnName, Object rowData)string (optional)* A formatter function which will return the HTML for the cell. By default the formatter is a plain HTML formatter.
Function(string value, string columnName)string (optional)* A formatter function which will return the HTML for the cell's header. By default the formatter is a plain HTML formatter.
number=10 The size of the rows buffer, for virtual table
number=35 In pixels, the minimum width for a column
number=8 The size of the area where you can drag to resize.
function(columnName: string, descending: boolean, defaultComparator: function(a,b):number):{function(a,b):number} A callback that can pass a comparator function for each column and mode as required.
string='dgtable-resize' Class name for the dragged resizing element (showing when resizing a column)
string='dgtable' Class name for the table
boolean=true When set, hovering on truncated cells will show a preview of the full content.
boolean=true Allow for toggling off allowCellPreview for headers specifically.
boolean=true When set, the preview cell will receive its background automatically from the cell.
string='dgtable-cell-preview' Class name for the cell preview element
string='dgtable-wrapper' Element class name.
Element? Optional element to take over
Function(row: Object, args: Object): boolean (optional)* A filter function for using with the filter method
renderskeleton: The table is re-drawing it's base elements, including headers. Will always be followed by a render event.
render: The table has finished rendering (after adding rows etc.).
cellpreview: We are about to show a cell preview - { el: Element, rowIndex: number|null, name: string, rowData: Object|null, cell: Element, cellEl: Element }
table.hideCellPreview
cellpreviewdestroy: Cell preview element is about to be destroyed after hiding - { el: Element, name: string, filteredRowIndex: number|null, rowIndex: Object|null, cell: Element, cellEl: Element }
cellPreview event.
headerrowcreate: The header row has just been created - Element
headerrowdestroy: Called just before removing the header row DOM element from the table - Element
rowcreate: A row has just been created - { filteredRowIndex: number, rowIndex: number, rowEl: Element, rowData: Object }
rowclick: A row has just been created - { event: MouseEvent, rowIndex: number, rowIndex: number, rowEl: Element, rowData: Object }
rowdestroy: Called just before removing a row DOM element from the table - Element
addrows: Data rows have been added to the table - ({ count: number, clear: boolean })
addcolumn: A column was added - string (the column's name)
removecolumn: A column was removed - string (the column's name)
movecolumn: A column was moved - ({ name: string, src: number, dest: number })
showcolumn: A column was shown - string (the column's name)
hidecolumn: A column was hidden - string (the column's name)
columnwidth: A column was resized - ({ name: string, width: number|string, oldWidth: number|string })
filter: A filter was applied - any - the arguments passed to the filter method
filterclear: A filter was cleared
sort: The data was sorted - { sorts: { "column": "column's name", "descending": true|false }[], resort: true|undefined, comparator: Function }
headercontextmenu: A context menu should be shown for a header cell - ({ columnName: string, pageX: number, pageY: number, bounds: { left: number, top: number, width: number, height: number } })
on(eventName, {Function?} callback): Adds an event listener
once(eventName, {Function?} callback): Adds a one-shot event listener
off(eventName, {Function?} callback): Removes an event listener
render(): Renders the view. Should be called after adding to the DOM, and when the viewport has changed and the table has no knowledge of it.
clearAndRender({boolean} render = true): Forces a full render of the table
setColumns({COLUMN_OPTIONS[]} columns, {boolean} render = true) {DGTable}: Sets the table columns
addColumn({COLUMN_OPTIONS} columnData, {string|number} before = -1, {boolean} render = true) {DGTable}: Add a column to the table
removeColumn({string} column, {boolean} render = true) {DGTable}: Remove a column from the table
setFilter({Function(row: Object, args: Object): boolean} filterFunc) {DGTable}: Sets a new filtering function, set null for default.
setCellFormatter({Function(value: , columnName: string, row: Object):string|null} formatter) {DGTable}: Sets a new cell formatter.
setHeaderCellFormatter({Function(label: string, columnName: string):string|null} formatter) {DGTable}: Sets a new header cell formatter.
filter({Object} args) {DGTable}: Filter the visible rows in the table
filter({{column: string, keyword: string, caseSensitive: boolean}} args) {DGTable}: Syntax for default filtering function.
clearFilter() {DGTable}: Clears the current filter
setColumnLabel({string} column, {string} label) {DGTable}: Set a new label to a column
moveColumn({string|number} src, {string|number} dest, visibleOnly = true) {DGTable}: Move a column to a new position
sort({string?} column, {boolean?} descending, {boolean=false} add) {DGTable}: Sort the table. This does not render automatically, so you may need to call render() too.
resort() {DGTable}: Re-sort the table using current sort specifiers. This does not render automatically, so you may need to call render() too.
setColumnVisible({string} column, {boolean} visible) {DGTable}: Show or hide a column
isColumnVisible({string} column, {boolean} visible) {boolean}: Get the visibility mode of a column
setMinColumnWidth({number} minColumnWidth) {DGTable}: Globally set the minimum column width
getMinColumnWidth() {number}: Get the current minimum column width
setSortableColumns({number} sortableColumns) {DGTable}: Set the limit on concurrent columns sortedh
getSortableColumns() {number}: Get the limit on concurrent columns sorted
getHeaderRowElement() {Element}: Get the DOM element of the header row
setMovableColumns({boolean} movableColumns) {DGTable}: Undocumented yet*
getMovableColumns() {boolean}: Undocumented yet*
setResizableColumns({boolean} resizableColumns) {DGTable}: Undocumented yet*
getResizableColumns() {boolean}: Undocumented yet*
setOnComparatorRequired({function(columnName: string, descending: boolean, defaultComparator: function(a,b):number):{function(a,b):number}}|null comparatorCallback) {DGTable}: Sets a functions that supplies comparators dynamically
setCustomSortingProvider({{function(data: any[], sort: function(any[]):any[]):any[]}|null} customSortingProvider) {DGTable}: sets custom sorting function for a data set
setColumnWidth({string} column, {number|string} width) {DGTable}: Undocumented yet*
getColumnWidth({string} column) {string|null}: Undocumented yet*
getColumnConfig({string} column name) {SERIALIZED_COLUMN}: Undocumented yet*
getColumnsConfig() {Object}: Undocumented yet*
getSortedColumns() {Array.} : Undocumented yet*
getHtmlForRowCell(row: number, columnName: string) {string}: Returns the HTML for specified cell in a row.
cellFormatter.
getHtmlForRowDataCell(rowData: Object, columnName: string) {string|null}: Returns the HTML string for a specific cell. Can be used externally for special cases (i.e. when setting a fresh HTML in the cell preview through the callback).
getDataForRow(rowIndex: number): Object: Gets the row data for a specific row
getRowCount(): number: Gets the number of rows
getIndexForRow(row: Object): number: Finds the index of the specified row
getFilteredRowCount(): number: Gets the number of filtered rows
getIndexForFilteredRow(row: Object): number: Finds the index of the specified row within the filtered results
getDataForFilteredRow(row: number): Object: Undocumented yet*
getRowElement(rowIndex: number): Element: Returns the element of the specified row (unfiltered index)
getRowYPos(rowIndex: number): number?: Returns the Y pos of the specified row (unfiltered index)
tableWidthChanged() {DGTable}: Undocumented yet*
tableHeightChanged() {DGTable}: Undocumented yet*
addRows({Object[]} data, {number} at = -1, {boolean} resort = false, {boolean} render = true) {DGTable}: Adds the specified rows at the specified position, and optionally resorts the data
removeRow({number} rowIndex, {boolean} render = true) {DGTable}: Removes one row at the specified position
removeRows({number} rowIndex, {number} count, {boolean} render = true) {DGTable}: Removes rows at the specified position
refreshRow({number} rowIndex) {DGTable}: Refreshes the row specified
refreshAllVirtualRows() {DGTable}: Refreshes all virtual rows
setRows(data: Object[], resort: boolean=false) {DGTable}: Rests the table rows to the provided array of rows.
getUrlForElementContent({string} id) {string?}: Undocumented yet*
isWorkerSupported() {boolean}: Undocumented yet*
createWebWorker({string} url) {Worker?}: Undocumented yet*
unbindWebWorker({Worker} worker) {DGTable}: Undocumented yet*
hideCellPreview() {DGTable}: Hide any cell preview showing currently, or prevent showing a cell preview from within the cellpreview event.
destroy(): Destroy the table and free all of its memory.