A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.dhtmlx.com/suite/grid/usage/ below:

JavaScript Grid - Work with Grid

Work with Grid Working with Grid in the TreeGrid mode​

For information on working with with Grid in the TreeGrid mode, read the TreeGrid mode guide.

Working with columns and cells​

The API of DHTMLX Grid allows setting configuration of columns, getting an object of a particular column as well as the parameters of a certain cell.

Setting columns configuration​

You can specify the configuration of Grid columns on the fly via the setColumns() method. It takes an array with columns objects as a parameter.

grid.setColumns([
{ id: "a", header: [{ text: "New header for column a" }] },
{ id: "b", header: [{ text: "New header for column b" }] },

]);

You can find the full list of the available configuration options of a Grid column here.

Getting configuration of a column​

It is possible to return an object with attributes of a column via its id. Use the getColumn() method for this purpose.

const column = grid.getColumn("b"); 

The method returns an object with configuration of the specified column. You can find the list of properties that the return object can contain here.

Getting configuration of a cell​

There is the getCellRect() method that returns an object with coordinates of a cell. The method takes as parameters the ids of the row and the column the cell belongs to:

const rect = grid.getCellRect("1", "c");

The return object includes the following attributes:

x (number) the X coordinate of a cell y (number) the Y coordinate of a cell height (number) the height of a cell width (number) the width of a cell Hiding/showing a column​

It is possible to show and hide a column in the grid via the showColumn() and hideColumn() methods.


grid.showColumn(colId);

grid.hideColumn(colId);

Related sample: Grid. Show / hide column

Since the object of a column has the hidden property, the showColumn() method changes the value of the hidden property to false while the hideColumn() method changes the value of the property to true.

Checking visibility of a column​

You can check whether a column is hidden or shown on a page using the isColumnHidden() method. The method returns true, if a column is hidden, and false if it's visible.

grid.isColumnHidden("country"); 

Related sample: Grid. Is column hidden

You may want to manipulate a filter specified in the header of a grid, for example, to set/unset focus on the filter, to change the filter, or clear it. To do that, you should apply the getHeaderFilter() method to get an object with methods of the header filter and apply the necessary method. For example:


grid.getHeaderFilter("country").setValue("Brazil");


grid.getHeaderFilter("country").focus();


grid.getHeaderFilter("country").blur();


const filter = grid.getHeaderFilter("country").getFilter();
console.log(filter);




grid.getHeaderFilter("country").clear();

Related sample: Grid. Get header filter

Working with rows​ Adding/removing a row​ Adding a row​

You may add a new row into the grid by using the add() method of DataCollection:

grid.data.add({
"country": "Estonia",
"population": "1326535",
"yearlyChange": "0.0070",
"netChange": "3782",
"density": "31",
"area": "45339",
"migrants": "3911",
"fert": "1.59",
"age": "42.4",
"urban": "0.6790",
"id": "136"
}, 0);
Removing a row​

To remove the necessary row from the grid, apply the remove() method of DataCollection. Pass the id of the row that should be removed to the method:

Here is an example of removing a currently selected row:

const cell = grid.selection.getCell();
grid.data.remove(cell.row.id);

Related sample: Grid. Delete row

For more information about the selection functionality in Grid, read the Selection and Work with selection object articles.

Removing all rows​

If you need to remove all rows at once, use the removeAll() method of DataCollection:

Hiding/showing a row​

Starting from v7.0, it is possible to show and hide a row in the grid via the showRow() and hideRow() methods.


grid.showRow(rowId);

grid.hideRow(rowId);

Related sample: Grid. Show / hide row

Checking visibility of a row​

You can check whether a row is hidden or shown on a page using the isRowHidden() method. The method returns true, if a row is hidden, and false if it's visible.

Related sample: Grid. Show / hide row

Adding/removing spans​

You can manipulate columns and rows spans inside the grid with the help of the corresponding API methods: addSpan(), removeSpan() and getSpan().

Adding spans​

In order to add a column/row span into the grid, use the addSpan() method. Pass an object with configuration of a span as a parameter:

grid.addSpan({ 
row: "0",
column: "a",
rowspan: 5
});

These are possible fields of a span object:

row (string|number) mandatory, the id of a row column (string|number) mandatory, the id of a column rowspan (number) optional, the number of rows in a span colspan (number) optional, the number of columns in a span text (string|number) optional, the text in a spanned row/column css (string) optional, the name of the CSS class that will be applied to a span Getting spans​

You can return the column/row span a cell is a part of using the getSpan() method. It takes the ids of the row and the column the cell belongs to as parameters:

const span = grid.getSpan("10", "a"); 

As a result, you'll get an object with a span configuration, if any span includes the specified cell. Attributes of a span object are described above.

Removing spans​

To remove an existing span, make use of the removeSpan() method. It takes the ids of the row and the column as parameters:

grid.removeSpan("10", "a");
Working with data​ Filtering data​

You can filter grid data by the specified criteria with the help of the filter() method of DataCollection. The method takes as a parameter an object with the properties described below:

rule (object|function) the filtering criteria. It can be: config (object) optional, an object with the following properties:
grid.data.filter(function (item) {
return item.a > 0 && item.b !== "Apple";
});

grid.data.filter(function (item) {
return item.a > 0 && item.b !== "Apple";
}, {
add: true
});

grid.data.filter({
by: "a",
match: "Orange",
compare: function (value, match, item) {
if (item.a !== "Some") {
return val === "New";
}
return false;
}
}, {
add: true,
});

Related sample: Grid. Basic filter

Sorting data​

It is possible to sort data in the grid via the sort() method of DataCollection. The method takes two parameters:

rule (object) an object with parameters for sorting. It can take the following attributes: config (object) defines the parameter of sorting. It takes one attribute:
grid.data.sort(
{
by:"a",
dir:"desc",
as: item => (item.toUpperCase())
},
{ smartSorting: true }
);

Related sample: Grid. Sorting

Sorting by multiple columns​

pro version only

The described functionality requires PRO version of the DHTMLX Grid (or DHTMLX Suite) package.

You can sort Grid by multiple columns simultaneously.

Related sample: Grid. Sorting by multiple columns (multisorting)

Multi-sorting is enabled on initialization of the component. In the example below Grid data is sorted with the help of the sort() method of DataCollection by several columns:

const grid = new dhx.Grid("grid_container", {
columns: [

],
editable: true,
group: {
order: ["animal_type"]
},
groupable: true,
data: dataset
});

grid.data.sort({ by: "volunteer_name", dir: "desc" }, { smartSorting: true });
grid.data.sort({ by: "task_status", dir: "asc" });
grid.data.sort({ by: "animal_type", dir: "asc" });

Related sample: Grid. Grouping with sorting by multiple columns (multisorting)

If you need to disable the multi-sorting ability, set the multiSort Grid property to false.

const grid = new dhx.Grid("grid_container", {
columns: [

],
multiSort: false,
data: dataset
});
Custom sorting​

You can also specify the rule attribute in a passed object instead of the default ones and set a custom function for sorting. For example:

grid.data.sort({
rule: (a, b) => a.id > b.id ? 1 : (a.id < b.id ? -1 : 0)
});
Getting the sorting state​

To get the current state of sorting data in Grid, use the getSortingStates() method of DataCollection. The method allows getting the result of sorting data by multiple columns and returns an array of objects with the following properties:

by (string | number) the id of a data field to sort by dir (string) the direction of sorting: "asc" or "desc" as (function) a custom function of converting values before comparing rule (function) a custom sorting function smartSorting (boolean) (if applied) specifies whether a sorting rule should be applied each time after changing the data set
const state = grid.data.getSortingStates(); 

Editing data​

You can easily edit the desired cell of a grid with the help of the editCell() method. It takes two parameters:

rowId (string, number) the id of a row colId (string, number) the id of a column

For example, you can edit the first cell of the "project" column like this:

grid.editCell(grid.data.getId(0), "project");

Related sample: Grid. Edit the first cell

To finish editing of a cell, use the editEnd() method. The method takes a boolean value as a parameter to define whether the edited data will be saved after the editing of a cell is complete (if true, the made changes won't be saved).

grid.editEnd(); 

grid.editEnd(true);
Exporting data​

You can easily export data of Grid into the Excel, CSV, PDF, or PNG format.

Exporting data to Excel​

Since v9.2 DHTMLX Grid uses the WebAssembly-based library Json2Excel to enable the export to Excel functionality and the xlsx() method of the Export module to export data from Grid into an Excel file. You can use either the public export server or a local export server.

Thus, to have the possibility of exporting files you need to:

grid.export.xlsx({
url: "../libs/json2excel/1.3/worker.js?vx",
name: "my_file",
tableName: "grid",
dateFormatMask: "mm.dd.yy"
});

You can check the latest version of the Json2Excel library at the github repository.

Read the details on dates formatting in Excel in the related Excel documentation.

Related sample: Grid. Export to xlsx and csv

note

The export module server used in the Suite versions up to v9.1 is still available. You can either set the path to the public export server as:

grid.export.xlsx({
url: "//export.dhtmlx.com/excel"
});

or provide a local path to the export module on your computer as a value of the url property of the export method.

Exporting data to CSV​

You can export data from Grid to the CSV format with the csv() method of the Export module. The method takes an object with export settings as a parameter (all settings are optional).

grid.export.csv({
name: "my_file",
rowDelimiter: "\t",
columnDelimiter: ";"
});

Related sample: Grid. Export to xlsx and csv

The csv() method returns a CSV string with Grid data.

Exporting data to PDF​

The pdf() method of the Export module allows you to export data from Grid into a PDF file. The method takes an object with export settings as a parameter (all settings are optional).

grid.export.pdf({
format: "A4",
scale: 0.75,
displayHeaderFooter: true,
theme: "dark"
});

Related sample: Grid. Export to PDF/PNG

Exporting data to PNG​

The png() method of the Export module allows you to export data from Grid into a PNG file. The method takes an object with export settings as a parameter (all settings are optional).

grid.export.png({
theme: "dark"
});

Related sample: Grid. Export to PDF/PNG

Grouping data​

pro version only

The described functionality requires PRO version of the DHTMLX Grid (or DHTMLX Suite) package.

You can group row data by column values to make them more suitable for analysis. The Grid row data grouping functionality allows aggregating data in a group, adjusting the appearance, order and configuration of data grouping and rendering statistics in the summary rows.

It is possible to set a predefined Grid configuration to initialize Grid with grouped data or to use the DataCollection API for grouping Grid data.

Related sample: Grid. Grouping

important

Enabling data grouping​

To use the row data grouping functionality in Grid, you need to apply the group configuration property of Grid. You can set the group property to true to enable grouping, or to specify it as a configuration object to configure data grouping .

const grid = new dhx.Grid("grid_container", {
columns: [

],
group: true,
groupable: true
data: dataset
});

note

Note that when you initialize Grid with the group configuration property, the tree-like mode is enabled for Grid and it will have the type: tree property in its configuration.

You can also specify what Grid data will be used for grouping using the groupable properties of Grid and of a column.

The groupable property of Grid enables grouping data by the values in all columns via the user interface:

const grid = new dhx.Grid("grid_container", {
columns: [

],
group: true,
groupable: true
data: dataset
});

note

The groupable property works only with the group panel.

With the Grid groupable property enabled you can:

Related sample: Grid. Grouping

The groupable property of a column allows grouping data by the values of a certain column via the user interface:

const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "country", header: [{ text: "Country" }], groupable: true },
{ id: "population", header: [{ text: "Population" }] },
{ id: "area", header: [{ text: "Land Area (Km²)" }] }
],
group: true,
data: dataset
});

In the above snippet rows will be grouped by the "country" column values, as the groupable: true property is specified in its configuration.

Configuring data grouping​

With the group configuration option set as an object you can display and manage the group panel, adjust rendering of columns, the order of grouping and specify the data aggregation settings. The group configuration object has the properties enumerated below:

const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "country", header: [{ text: "Country" }], groupable: true },
{ id: "population", header: [{ text: "Population" }] },
{ id: "area", header: [{ text: "Land Area (Km²)" }] }
],
group: {
panel: true
},
data: dataset
});
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "country", header: [{ text: "Country" }], groupable: true },
{ id: "city", header: [{ text: "City" }] },
{ id: "population", header: [{ text: "Population" }] }
],
group: {
panel: true,
panelHeight: 60
},
data: dataset
});

Related sample: Grid. Grouping customization

const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "country", header: [{ text: "Country" }], groupable: true },
{ id: "city", header: [{ text: "City" }] },
{ id: "population", header: [{ text: "Population" }] },
],
group: {
panel: true,
hideableColumns: false
},
data: dataset
});

Related sample: Grid. Grouping customization

const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "country", header: [{ text: "Country" }], groupable: true },
{ id: "city", header: [{ text: "City" }], groupable: true },
{ id: "population", header: [{ text: "Population" }] }
],
group: {
panel: true,


showMissed: "Missed"
},
data: dataset
});

Related sample: Grid. Grouping missing data

A predefined configuration is needed in cases when the group panel is supposed to be used and the rendered group should have the total row.

const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "country", header: [{ text: "Country" }] },
{ id: "population", header: [{ text: "Population" }] },
{ id: "density", header: [{ text: "Density (P/Km²)" }] }
],
group: {
panel: true,
fields: {
population: {
summary: "bottom",
map: {
population: ["population", "sum"],
customSummary: function(rows) {
return rows.length;
}
}
}
},
order: ["population"]
},
groupable: true,
data: dataset
});

In the above snippet row data is grouped by the values of the "population" column. The "group" column contains:

a) the grouped values of the "population" column and the number of elements that suit the grouping criteria

b) the total rows under the grouped values set by the summary property

Related sample: Grid. Grouping and totals in the summary row

Related sample: Grid. Grouping customization

Below you'll find the examples of configuring Grid via the order property:

  1. You can specify the columns' ids to group data by in the order of their succession. Grouping starts from the first specified column and will continue for each following one:
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "country", header: [{ text: "Country" }], groupable: true },
{ id: "city", header: [{ text: "City" }], groupable: true },
{ id: "population", header: [{ text: "Population" }] }
],
group: {
order: ["country", "city"]
},
data: dataset
});
  1. You can also use callback functions in an array. A callback function takes the current data row and returns the value for grouping:
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "country", header: [{ text: "Country" }] },
{ id: "city", header: [{ text: "City" }]},
{ id: "population", header: [{ text: "Population" }] }
],
group: {
order: [
(row) => row.population > 1000000
? "Large Population"
: "Small Population"
]
},
groupable: true,
data: dataset
});
  1. You can use a configuration object as an element that defines the grouping order:
const grid = new dhx.Grid("grid_container", {
columns: [
{
id: "country",
header: [{ text: "Country" }],
template: (value, row) => {

if (row.$groupSummary) {
return `count: ${row.count}`;
}
return value;
}
},
{
id: "population",
header: [{ text: "Population" }],
template: (value, { $groupSummary, ageAvg, populationAvg }) => {
if (!$groupSummary) return value;
return `Age avg: ${ageAvg}; Population avg: ${populationAvg}`;
},
},
{ id: "city", header: [{ text: "City" }], },
{ id: "age", header: [{ text: "Age" }] },
],
group: {
fields: {
population: {
summary: "top",
map: {
ageAvg: ["age", "avg"],
populationAvg: ["population", "avg"],
}
},
},
order: [
{
by: "country",
summary: "top",
map: {
count: ["country", "count"]
}
},
"population",
],
hideableColumns: false,
},
groupable: true,
data: dataset
});
  1. You can also combine columns' ids, functions and configuration objects. Such an approach provides a flexible control over the grouping order by applying static values for some columns and dynamic values calculated by a function for other columns:
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "country", header: [{ text: "Country" }] },
{ id: "city", header: [{ text: "City" }], },
{ id: "population", header: [{ text: "Population" }] },
],
group: {
order: [
"country",
(row) => row.city[0].toUpperCase()
]
},
groupable: true,
data: dataset
});

Related sample: Grid. Grouping customization

You can specify the id of a column in two ways:

  1. as a string value of the column property of the group object:
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "country", header: [{ text: "Country" }], groupable: true },
{ id: "city", header: [{ text: "City" }], groupable: true },
{ id: "population", header: [{ text: "Population" }] }
],
group: {
column: "group"
},
data: dataset
});
  1. as a string value of the id property of the column object inside the group object
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "country", header: [{ text: "Country" }], groupable: true },
{ id: "city", header: [{ text: "City" }], groupable: true },
{ id: "population", header: [{ text: "Population" }] }
],
group: {
column: {
id: "group",
sortable: false,
tooltip: false,
}
},
data: dataset
});

A column with grouped data may contain the following properties in its configuration:

column: {
id: string | number,
header: [
{
text?: string | ((content: ISummaryList) => string),
tooltip?: boolean | object,
tooltipTemplate?: (
content: { value: string } & ISummaryList,
header: IHeader,
column: ICol
) => string | boolean,
align?: "left" | "center" | "right",
css?: string,
content?: "inputFilter" | "selectFilter" | "comboFilter",
filterConfig?: {
filter?: (item: any, input: string) => boolean,
multiselection?: boolean,
readonly?: boolean,
placeholder?: string,
virtual?: boolean,
template?: function
},
customFilter?: (item: any, input: string) => boolean,
headerSort?: boolean,
sortAs?: (cellValue: any) => string | number,
htmlEnable?: boolean,
}
],
footer?: [
{
text?: string | ((content: ISummaryList) => string),
tooltip?: boolean | object,
tooltipTemplate?: (
content: { value: string } & ISummaryList,
header: IHeader,
column: ICol
) => string | boolean,
align?: "left" | "center" | "right",
css?: string,
htmlEnable?: boolean,
},
],
align?: "left" | "center" | "right",
resizable?: boolean,
sortable?: boolean,
mark?: { min?: string, max?: string } |
(cell: any, columnCells: any[], row?: object, column?: object) => string,
template?: (cellValue: any, row: object, column: object) => string,
tooltip?: boolean | object,
tooltipTemplate?: (cellValue: any, row: object, column: object) => string,
}

Note that the column object of the group configuration option has some properties of a Grid column. You can check the descriptions of the group column object properties enumerated above in the Grid column properties guide.

Making group panel elements closable​

You can enable closing of all the elements of the group panel using the closable configuration option of Grid.

const grid = new dhx.Grid("grid_container", {
columns: [
{ width: 150, id: "country", header: [{ text: "Country" }] },
{ width: 150, id: "population", header: [{ text: "Population" }] },
{ width: 150, id: "density", header: [{ text: "Density (P/Km²)" }] },
{ width: 150, id: "area", header: [{ text: "Land Area (Km²)" }] },
],
group: true,
groupable: true,
closable: true,
data: dataset
});

Related sample: Grid. Grouping customization

You can also use the closable configuration property of a column. It allows making a particular group panel element closable or permanently enabled:

const grid = new dhx.Grid("grid_container", {
columns: [
{
width: 150,
id: "country",
header: [{ text: "Country" }],


closable: false,
},
{
width: 150,
id: "population",
header: [{ text: "Population" }],
groupable: true,
},
{ width: 150, id: "density", header: [{ text: "Density (P/Km²)" }] },
{ width: 150, id: "area", header: [{ text: "Land Area (Km²)" }] },
],
group: {
order: ["country", "population"]
},
groupable: true,
data: dataset
});
Using DataCollection API for data grouping​

After you've enabled grouping in Grid, you can use the DataCollection API to work with data grouping.

Grouping Grid data​

To group Grid data, use the group() method of DataCollection. The method takes the following parameters:

There are several examples of grouping Grid data via the group() method of DataCollection:

const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "salary", header: [{ text: "Salary" }] },
{ id: "experience", header: [{ text: "Experience (years)" }] },
{ id: "city", header: [{ text: "City" }] }
],
group: true,
groupable: true,
data: dataset
});

grid.data.group([
function(row) {
if (row.salary < 30000) return "Low income";
if (row.salary >= 30000 && row.salary < 70000) return "Medium income";
return "High income";
},
"city"
]);
const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "department", header: [{ text: "Department" }] },
{ id: "employees", header: [{ text: "Number of Employees" }] },
{ id: "location", header: [{ text: "Location" }] }
],
group: true,
groupable: true,
data: dataset
});

grid.data.group([{
by: "department",
map: {
employees: ["employees", "sum"],
location: (rows) => {

const uniqueLocations = [...new Set(rows.map(r => r.location))];
return uniqueLocations.join(", ");
}
},
summary: "top"
}]);

Related sample: Grouping and totals in the summary row via data collection

const grid = new dhx.Grid("grid_container", {
columns: [
{ id: "name", header: [{ text: "Name" }] },
{ id: "age", header: [{ text: "Age" }] },
{ id: "city", header: [{ text: "City" }] }
],
group: true,
groupable: true,
data: dataset
});

grid.data.group(["city"], {


showMissed: "Unknown City"
});

Related sample: Grid. Grouping missing data

Ungrouping Grid data​

To ungroup Grid data, use the ungroup() method of DataCollection.

Checking whether Grid data is grouped​

To check whether Grid data is grouped at the moment, use the isGrouped() method of DataCollection. The method returns true if Grid data is grouped at the moment and false if it isn't grouped.

if (grid.data.isGrouped()) {
console.log("The data is currently grouped");
}
Localizing data grouping labels​

You can render Grid data grouping labels in the necessary language by translating the related strings and applying a ready locale for the component. The default Grid locale looks like this:

const en = {
total: "Total",
groupText: "Group",
dropAreaTitle: "Group by:",
dropAreaPlaceholder: "Drag the column header here",
}

Check the details at the Localization guide.

The API of DHTMLX Grid provides the possibility to set scrolls to the necessary position and to get the current state of scrolls.

Scrolling to specific coordinates​

You can scroll grid content to exact position defined by x and y coordinates via the scroll() method. Pass the coordinates as parameters of the method.

Scrolling to specific grid cell​

It is also possible to scroll grid content to a particular cell. Pass the ids of the row and the column as parameters:

grid.scrollTo("15", "c");

Related sample: Grid. Controlling scroll behavior

Getting the state of scroll​

To return the current state of scroll, use the getScrollState() method.

const state = grid.getScrollState(); 

It returns an object with x,y coordinates of a position the grid has been scrolled to.

Repainting Grid​

In case you've changed some configuration settings of a grid, you can repaint it on a page via the paint() method:

Destructing Grid​

When it's necessary to release resources occupied by Grid during its activity, you can make use of the destructor() method:

Using Selection API​

For information on using Selection API, read Work with Selection object.

Using RangeSelection API​

For information on using RangeSelection API, read Work with RangeSelection Module.

Using BlockSelection API​

For information on using BlockSelection API, read Work with BlockSelection module.

Working with Clipboard​

For information on using the Clipboard module in Grid, read Work with Clipboard module.

Working with DragPanel​

For information on using the DragPanel module in Grid, read Work with DragPanel module.

Working with History API​

For information on using the History API in Grid, read Work with History module.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4