turbogrid
// global
var TG = window.turbogrid;
var { Grid, Util } = window.turbogrid;
// cjs
var TG = require("turbogrid");
var { Grid, Util } = require("turbogrid");
// esm
import TG from "turbogrid";
import { Grid, Util } from "turbogrid";
Grid constructor, creates a new grid instance with container.
//selector
var grid = new Grid(".container");
//or element
var container = document.querySelector(".container");
var grid = new Grid(container);
//or options with container
var grid = Grid({
container,
... other options
});
Methods
For Detailed data structure, check data structure
grid.setData({
columns: [],
rows: []
});
Returns current data.
var data = grid.getData();
For Detailed options, check default options
//set multiple options
grid.setOption({
optionKey1: optionValue1,
optionKey2: optionValue2
});
//set single option
grid.setOption(optionKey, optionValue);
Returns current options by name or all options.
//get all options
var options = grid.getOption();
//get single option
var optionValue = grid.getOption(optionKey);
Set or customize Grid formatters.
//set multiple formatters
grid.setFormatter({
string: function(value, rowItem, columnItem, cellNode) {
return value;
}
});
//set single formatter
grid.setFormatter("string", formatterHandler);
default cell formatter: string, number, date, icon, blank, checkbox, tree, null
default header formatter: header
Formatter arguments:
function(value, rowItem, columnItem, cellNode) {
// value is formatted by null formatter
// get original value:
var originalValue = rowItem[columnItem.id];
var rowIndex = rowItem.tg_index;
var columnIndex = columnItem.tg_index;
// using default formatter returns:
var defaultFormatter = this.getDefaultFormatter("[formatter-name]");
return defaultFormatter(value + " new text", rowItem, columnItem, cellNode)
}
Demo Formatter
Returns type specified formatter(Function).
var stringFormatter = grid.getFormatter("string");
Returns type specified default formatter(Function).
var treeFormatter = grid.getDefaultFormatter("tree");
Attach a handler to an event for Grid. See all event types.
grid.bind("onUpdated", function(e, d){
//console.log(d);
});
Demo Events
Attach a handler to an event. The handler is executed at most once per event type. It will be removed once it triggers for the first time.
grid.once("onUpdated", function(e, eventData){
//console.log(eventData);
});
Remove a previously-attached event handler from Grid.
grid.unbind();
grid.unbind("onUpdated");
grid.unbind("onUpdated", handler);
Execute all handlers attached to Grid for an event.
this.trigger(eventType, eventData);
Returns all event types.
var allEventTypes = grid.getAllEvents();
Demo Events
grid.render();
grid.resize();
grid.resize(600, 400);
Demo Resize
grid.destroy();
Different data types of the rowIndex/columnIndex parameter are supported :
- If parameter type is Number, grid will search the row/column item by index. (recommend, highest performance)
- If parameter type is String, grid will iterates all items to match the row/column id.
- If parameter type is Object, grid will look at the object properties:
- If there is a tg_index property inside the object, grid will search the row/column item by index. (higher priority)
- if there is an id property (type of string), grid will iterates all items to match the row/column id.
var columnItem = grid.getColumnItem(columnIndex);
var rowItem = grid.getRowItem(rowIndex);
var rowItem = grid.getRowItem(123);
var rowItem = grid.getRowItem("id_123");
var rowItem = grid.getRowItemBy("id", "id_123");
var rowItem = grid.getRowItemBy("key", "value");
grid.showLoading();
grid.hideLoading();
grid.setLoading("Loading ...");
grid.setLoading(function(container) {
return document.createElement("div");
});
Demo Show/Hide Loading
grid.showMask();
grid.showMask({
opacity: 0.3
});
grid.hideMask();
Demo Show/Hide Loading
grid.expandAllRows();
grid.collapseAllRows();
grid.toggleAllRows();
Demo Row Collapse/Expand
grid.expandRow(rowIndex);
grid.collapseRow(rowIndex);
grid.toggleRow(rowIndex);
Demo Row Collapse/Expand
grid.expandRowLevel(level);
Demo Row Collapse/Expand
Returns all columns and rows data without private properties or by keysSettings.
var exportedData = grid.exportData();
var exportedData = grid.exportData({
the_key_need: true,
key_key_no_need: false
});
Demo Export
var subs = [{name:"row1"},{name:"row2"}];
grid.setRowSubs(rowIndex, subs);
Demo Dynamic Load Subs
var rows = [{name:"row1"},{name:"row2"}];
grid.setRows(rows);
Demo Dynamic Set Rows
grid.addRow({
id: "id1",
name: "row item"
});
grid.addRow(["Row 1", "Row 2"]);
grid.addRow(["Row 1", "Row 2"], parentIndex);
grid.addRow("Row Before","level_0", 0);
grid.deleteRow(2);
grid.deleteRow([1, 2]);
Demo Row Add/Delete
offset: if < 0 move up; if > 0 move down
grid.moveRowsToTop(["row_id1", "row_id2"]);
grid.moveRowsUp(["row_id1", "row_id2"]);
grid.moveRowsDown(["row_id1", "row_id2"]);
grid.moveRowsToBottom(["row_id1", "row_id2"]);
grid.moveSelectedRowsToTop();
grid.moveSelectedRowsUp();
grid.moveSelectedRowsDown();
grid.moveSelectedRowsToBottom();
grid.moveRows(["row_id"], -1);
grid.moveRows(["row_id"], -2);
grid.moveRows("row_id", 1);
Demo Row Move
grid.selectAll();
grid.selectAll(false);
Demo Row Select
grid.setRowSelected(rowIndex);
grid.setRowSelected(rowIndex, false);
grid.setRowSelected();
Demo Row Select
Returns a list for multiple selection
var selectedRow = grid.getSelectedRow();
var selectedRows = grid.getSelectedRows();
Demo Row Select
hover: true or false
grid.setRowHover(rowIndex, true);
grid.setRowHover(rowIndex, false);
Demo Frozen Middle
Remove sort state if data changed
grid.setSortColumn(sortColumn);
grid.removeSortColumn();
Demo Row Sort
Dynamic set column width, minWidth/maxWidth could be updated
grid.setColumnWidth(columnIndex, width);
Demo Column Width Resize
grid.showColumn(columnIndex);
grid.hideColumn(columnIndex);
grid.showColumn([1, 3]);
grid.hideColumn([1, 3]);
Demo Show/Hide Column
grid.addColumn({
id: "id1",
name: "column item"
});
grid.addColumn(["Column 1", "Column 2"]);
grid.addColumn(["Column 1", "Column 2"], parentIndex);
grid.addColumn("Column Before","level_0",0);
grid.deleteColumn(2);
grid.deleteColumn([1, 2]);
Demo Column Add/Delete
grid.scrollToRow(rowIndex);
grid.scrollToColumn(columnIndex);
grid.scrollToCell(rowIndex, columnIndex);
Demo Scroll
grid.scrollRowIntoView(rowIndex);
grid.scrollColumnIntoView(columnIndex);
grid.scrollCellIntoView(rowIndex, columnIndex);
Demo Scroll
grid.setScrollTop(200);
grid.setScrollLeft(200);
var st = grid.getScrollTop();
var sl = grid.getScrollLeft();
Demo Scroll
grid.updateRow(rowIndex);
grid.updateRow(rowIndex, rowData);
grid.updateCell(rowIndex, columnIndex);
grid.updateCell(rowIndex, columnIndex, cellValue);
Demo Flush
Returns 0 or scrollbarSize if has vertical scrollbar (hasVScroll)
getScrollbarHeight()
Returns 0 or scrollbarSize if has horizontal scrollbar (hasHScroll)
var sbw = grid.getScrollbarWidth();
var sbh = grid.getScrollbarHeight();
Scroll View size = Scroll Pane size - Scrollbar size
var svw = grid.getScrollViewWidth();
var svh = grid.getScrollViewHeight();
Scroll Pane size = Scroll View size + Scrollbar size
var spw = grid.getScrollPaneWidth();
var sph = grid.getScrollPaneHeight();
Total rows length. default total is false; total is true means including collapsed/filtered rows;
var len = grid.getRowsLength();
var totalLen = grid.getRowsLength(true);
Total rows height
var totalHeight = grid.getRowsHeight();
var rowHeight = grid.getRowHeight(rowIndex);
Returns current the index list of visible rows and columns.
var viewport = grid.getViewport();
// { rows, columns }
var nodes = grid.find(".selector-name");
var cellNode = grid.getCellNode(rowIndex, columnIndex);
var headerItemNode = grid.getHeaderItemNode(columnIndex);
var columnHeaderNode = grid.getColumnHeaderNode(columnIndex);
Executing a function for each column/row.
grid.forEachColumn(function(column, index, parent) {
//
});
grid.forEachRow(function(row, index, parent) {
//
});
var rowItem = grid.getRowItem(rowIndex);
if (grid.isRowSelectable(rowItem)) {
console.log("selectable");
}
Returns true or false.
Attach a handler to onUpdated event and the handler is executed at most once.
grid.onNextUpdated(function(e, eventData){
//console.log(eventData);
});
Data
[{
name: "item 1",
subs: [{
name : "item 2",
subs: [{
name : "item 3",
subs: [{
...
}]
}, ...]
}, ...]
}, ...]
var data = {
columns : [...],
rows : [...]
};
grid.setData(data);
See column available properties: columnProps
var columns = [{
id:"c1",
type : "string",
name:"Column Name 1"
}, {
id : "c2",
type : "number",
name : "Column Name 2"
}, {
id: "c3",
name: "Column Name 3",
subs: [{
id: "c3_s1",
name: "Column Name 3-1"
}, {
id: "c3_s2",
name: "Column Name 3-2"
}]
}];
See row available properties: rowProps
var rows = [{
id : "r1",
name : "Row Name 1",
c1 : "string value 1",
c2 : 1,
c3_s1 : "value 3 - 1",
c3_s2 : "value 1 - 2"
}, {
id : "r2",
type : "group",
name : "Row Name 2",
c1 : "string value 2",
c2 : "value 2",
c3_s1 : "value 3 - 1",
c3_s2 : "value 1 - 2",
subs : [{
id : "r3",
type : "holding",
name : "Row Name 3",
c1 : "string value 3",
c2 : 3,
c3_s1 : "value 3 - 1",
c3_s2 : "value 1 - 2"
}]
}];
Sets options from data (The priority is higher than "setOption")
var data = {
options : {
sortField : "name"
},
columns : columns,
rows : rows
};
grid.setData(data);
Dynamic load rows data. No rows data but require total length of rows.
var data = {
columns: columns,
rowsLength: 50000
};
grid.setData(data);
Demo Dynamic Load Rows
Events
Event Type | Event Data | Example |
---|---|---|
|
|
Demo Events
|
|
|
Demo Events
|
|
|
Demo Sort
|
|
|
Demo Column Add/Delete
|
|
|
Demo Events
|
|
|
Demo Row Add/Delete
|
|
|
Demo Events
|
|
|
Demo Dynamic Load Subs
|
|
|
Demo Row Drag
|
|
|
Demo Row Drag
|
|
|
Demo Row Move
|
|
|
Demo Events
|
|
|
Demo Row Select
|
|
|
Demo Events
|
|
|
Demo Events
|
|
|
Demo Events
|
|
|
Demo Touch
|
|
|
Demo Scroll
|
|
|
Demo Events
|
|
|
Demo Scroll
|
|
|
Demo Resize
|
|
|
Demo Resize
|
|
|
Demo Events
|
|
N/A |
Demo Flush
|
Lifecycle
Phases | Sub Phases | Payload | Capacity | ||
---|---|---|---|---|---|
create |
create style and html in the container
set data
set options
set formatters
add events
|
low | loading APIs |
|
|
render | init options | low | all APIs |
|
|
render header | middle | ||||
render body | render rows | middle | |||
render cells | high | ||||
update |
update body
update row(s)
update cell(s)
|
|
|||
destroy | remove all | low | none |
|
tg
The "tg" is Grid prefix or namespace.
CSS className:
.tg-pane {}
.tg-body{}
.tg-row{}
.tg-cell{}
Private property:
//rowItem.tg_index
//rowItem.tg_level
//rowItem.tg_group
The private properties will be removed when exportData()