The Web Components Hierarchical Grid Column Moving feature in Ignite UI for Web Components allows quick and easy column reordering. This can be done through the Column Moving API or by dragging and dropping the headers to another position via mouse or touch gestures. In the Web Components Hierarchical Grid, you can enable Column Moving for pinned and unpinned columns and for Multi-Column Headers as well.
Reordering between columns and column groups is allowed only when they are at the same level in the hierarchy and both are in the same group. Moving is allowed between columns/column-groups, if they are top level columns.
If a column header is templated and the Column Moving is enabled or the corresponding column is groupable, then the templated elements need to have the draggable attribute set to false!
If the pinned area exceeds its maximum allowed width (80% of the total IgcHierarchicalGridComponent width), a visual clue notifies the end user that the drop operation is forbidden and pinning is not possible. This means you won't be allowed to drop a column in the pinned area.
Web Components Hierarchical Grid Column Moving Overview Example
EXAMPLE
TS
HTML
CSS
import'igniteui-webcomponents-grids/grids/combined';
import { IgcHierarchicalGridComponent, IgcPaginatorComponent, IgcColumnComponent } from'igniteui-webcomponents-grids/grids';
import HierarchicalCustomers from'./HierarchicalCustomers.json';
import { IgcColumnTemplateContext } from'igniteui-webcomponents-grids/grids';
import { html, nothing } from'lit-html';
import"igniteui-webcomponents-grids/grids/themes/light/bootstrap.css";
import"./index.css";
exportclassSample{
private hierarchicalGrid1: IgcHierarchicalGridComponent
private paginator: IgcPaginatorComponent
private column1: IgcColumnComponent
private column2: IgcColumnComponent
private column3: IgcColumnComponent
private column4: IgcColumnComponent
private column5: IgcColumnComponent
private column6: IgcColumnComponent
private column7: IgcColumnComponent
private column8: IgcColumnComponent
private column9: IgcColumnComponent
private column10: IgcColumnComponent
private _bind: () =>void;
constructor() {
var hierarchicalGrid1 = this.hierarchicalGrid1 = document.getElementById('hierarchicalGrid1') as IgcHierarchicalGridComponent;
var paginator = this.paginator = document.getElementById('paginator') as IgcPaginatorComponent;
var column1 = this.column1 = document.getElementById('column1') as IgcColumnComponent;
var column2 = this.column2 = document.getElementById('column2') as IgcColumnComponent;
var column3 = this.column3 = document.getElementById('column3') as IgcColumnComponent;
var column4 = this.column4 = document.getElementById('column4') as IgcColumnComponent;
var column5 = this.column5 = document.getElementById('column5') as IgcColumnComponent;
var column6 = this.column6 = document.getElementById('column6') as IgcColumnComponent;
var column7 = this.column7 = document.getElementById('column7') as IgcColumnComponent;
var column8 = this.column8 = document.getElementById('column8') as IgcColumnComponent;
var column9 = this.column9 = document.getElementById('column9') as IgcColumnComponent;
var column10 = this.column10 = document.getElementById('column10') as IgcColumnComponent;
this._bind = () => {
hierarchicalGrid1.data = this.hierarchicalCustomers;
column1.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column2.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column3.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column4.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column5.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column6.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column7.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column8.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column9.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column10.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
}
this._bind();
}
private _hierarchicalCustomers: any[] = HierarchicalCustomers;
publicgethierarchicalCustomers(): any[] {
returnthis._hierarchicalCustomers;
}
public hierarchicalGridPinHeaderTemplate = (ctx: IgcColumnTemplateContext) => {
const column = (ctx asany).column;
return html`<div><spanstyle="float:left">${column.field}</span><spanstyle="float:right" @pointerdown=${(e: any) => this.toggleColumnPin(column)}>📌</span></div>`;
};
publictoggleColumnPin(field: IgcColumnComponent) {
if(field) {
field.pinned = !field.pinned;
}
}
}
new Sample();
ts
<!DOCTYPE html><html><head><title>Sample | Ignite UI | Web Components | infragistics</title><metacharset="UTF-8" /><linkrel="shortcut icon"href="https://static.infragistics.com/xplatform/images/browsers/wc.png" ><linkrel="stylesheet"href="https://fonts.googleapis.com/icon?family=Material+Icons" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Kanit&display=swap" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Titillium Web" /><linkrel="stylesheet"href="https://static.infragistics.com/xplatform/css/samples/shared.v8.css" /><linkrel="stylesheet"href="/src/index.css"type="text/css" /></head><body><divid="root"><divclass="container sample ig-typography"><divclass="container fill"><igc-hierarchical-gridauto-generate="false"moving="true"name="hierarchicalGrid1"id="hierarchicalGrid1"><igc-paginatorname="paginator"id="paginator"per-page="15"></igc-paginator><igc-columnfield="CustomerID"data-type="string"name="column1"id="column1"></igc-column><igc-columnfield="Company"data-type="string"width="150px"name="column2"id="column2"></igc-column><igc-columnfield="ContactName"data-type="string"width="150px"name="column3"id="column3"></igc-column><igc-columnfield="ContactTitle"data-type="string"width="150px"name="column4"id="column4"></igc-column><igc-columnfield="Address"data-type="string"name="column5"id="column5"></igc-column><igc-columnfield="City"data-type="string"name="column6"id="column6"></igc-column><igc-columnfield="PostalCode"data-type="string"name="column7"id="column7"></igc-column><igc-columnfield="Country"data-type="string"name="column8"id="column8"></igc-column><igc-columnfield="Phone"data-type="string"name="column9"id="column9"></igc-column><igc-columnfield="Fax"data-type="string"name="column10"id="column10"></igc-column><igc-row-islandchild-data-key="Orders"auto-generate="false"moving="true"><igc-columnfield="OrderID"data-type="number"></igc-column><igc-columnfield="EmployeeID"data-type="number"></igc-column><igc-columnfield="OrderDate"data-type="date"></igc-column><igc-columnfield="RequiredDate"data-type="date"></igc-column><igc-columnfield="ShippedDate"data-type="date"></igc-column><igc-columnfield="ShipVia"data-type="number"></igc-column><igc-columnfield="Freight"data-type="number"></igc-column><igc-columnfield="ShipName"data-type="string"></igc-column><igc-columnfield="ShipAddress"data-type="string"></igc-column><igc-columnfield="ShipCity"data-type="string"></igc-column><igc-columnfield="ShipPostalCode"data-type="string"></igc-column><igc-columnfield="ShipCountry"data-type="string"></igc-column><igc-row-islandchild-data-key="OrderDetails"auto-generate="false"moving="true"><igc-columnfield="ProductID"data-type="number"></igc-column><igc-columnfield="UnitPrice"data-type="number"></igc-column><igc-columnfield="Quantity"data-type="number"></igc-column><igc-columnfield="Discount"data-type="number"></igc-column></igc-row-island></igc-row-island></igc-hierarchical-grid></div></div></div><!-- This script is needed only for parcel and it will be excluded for webpack -->
<% if (false) { %><scriptsrc="src/index.ts"></script><% } %>
</body></html>html
/* shared styles are loaded from: *//* https://static.infragistics.com/xplatform/css/samples */css
Like this sample? Get access to our complete Ignite UI for Web Components toolkit and start building your own apps in minutes. Download it for free.
In addition to the drag and drop functionality, the Column Moving feature also provides API methods to allow moving a column/reordering columns programmatically:
moveColumn - Moves a column before or after another column (a target). The first parameter is the column to be moved, and the second parameter is the target column. Also accepts an optional third parameter Position (representing a DropPosition value), which determines whether to place the column before or after the target column.
// Move the ID column after the Name columnconst idColumn = grid.getColumnByName("ID");
const nameColumn = grid.getColumnByName("Name");
grid.moveColumn(idColumn, nameColumn, DropPosition.AfterDropTarget);
typescript
move - Moves a column to a specified visible index. If the passed index parameter is invalid (is negative, or exceeds the number of columns), or if the column is not allowed to move to this index (if inside another group), no operation is performed.
// Move the ID column at 3rd position.const idColumn = grid.getColumnByName("ID");
idColumn.move(3);
typescript
Note that when using the column moving feature, the ColumnMovingEnd event will be emitted if the operation was successful. Also note that in comparison to the drag and drop functionality, using the column moving feature does not require setting the moving property to true.
Events
There are several events related to the column moving to provide a means for tapping into the columns' drag and drop operations. These are ColumnMovingStart, ColumnMoving and ColumnMovingEnd.
You can subscribe to the ColumnMovingEnd event of the IgcHierarchicalGridComponent to implement some custom logic when a column is dropped to a new position. For example, you can cancel dropping the Category column after the Change On Year(%) column in the following code snippet.
<igc-hierarchical-gridid="dataGrid"auto-generate="false"moving="true"><igc-columnfield="Category"></igc-column><igc-columnfield="Change On Year(%)"data-type="Number" ></igc-column></igc-hierarchical-grid>html
constructor() {
var dataGrid = this.dataGrid = document.getElementById('dataGrid') as IgcHierarchicalGridComponent;
dataGrid.data = this.data;
dataGrid.addEventListener("columnMovingEnd", this.onColumnMovingEnd);
}
typescript
publiconColumnMovingEnd(event) {
if (event.detail.source.field === "Category" && event.detail.target.field === "Change On Year(%)") {
event.detail.cancel = true;
}
}
typescript
Styling
In addition to the predefined themes, the grid could be further customized by setting some of the available CSS properties.
In case you would like to change some of the colors, you need to set a class for the grid first:
import'igniteui-webcomponents-grids/grids/combined';
import { IgcHierarchicalGridComponent, IgcPaginatorComponent, IgcColumnComponent } from'igniteui-webcomponents-grids/grids';
import HierarchicalCustomers from'./HierarchicalCustomers.json';
import { IgcColumnTemplateContext } from'igniteui-webcomponents-grids/grids';
import { html, nothing } from'lit-html';
import"igniteui-webcomponents-grids/grids/themes/light/bootstrap.css";
import"./index.css";
exportclassSample{
private grid: IgcHierarchicalGridComponent
private paginator: IgcPaginatorComponent
private column1: IgcColumnComponent
private column2: IgcColumnComponent
private column3: IgcColumnComponent
private column4: IgcColumnComponent
private column5: IgcColumnComponent
private column6: IgcColumnComponent
private column7: IgcColumnComponent
private column8: IgcColumnComponent
private column9: IgcColumnComponent
private column10: IgcColumnComponent
private _bind: () =>void;
constructor() {
var grid = this.grid = document.getElementById('grid') as IgcHierarchicalGridComponent;
var paginator = this.paginator = document.getElementById('paginator') as IgcPaginatorComponent;
var column1 = this.column1 = document.getElementById('column1') as IgcColumnComponent;
var column2 = this.column2 = document.getElementById('column2') as IgcColumnComponent;
var column3 = this.column3 = document.getElementById('column3') as IgcColumnComponent;
var column4 = this.column4 = document.getElementById('column4') as IgcColumnComponent;
var column5 = this.column5 = document.getElementById('column5') as IgcColumnComponent;
var column6 = this.column6 = document.getElementById('column6') as IgcColumnComponent;
var column7 = this.column7 = document.getElementById('column7') as IgcColumnComponent;
var column8 = this.column8 = document.getElementById('column8') as IgcColumnComponent;
var column9 = this.column9 = document.getElementById('column9') as IgcColumnComponent;
var column10 = this.column10 = document.getElementById('column10') as IgcColumnComponent;
this._bind = () => {
grid.data = this.hierarchicalCustomers;
column1.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column2.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column3.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column4.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column5.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column6.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column7.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column8.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column9.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
column10.headerTemplate = this.hierarchicalGridPinHeaderTemplate;
}
this._bind();
}
private _hierarchicalCustomers: any[] = HierarchicalCustomers;
publicgethierarchicalCustomers(): any[] {
returnthis._hierarchicalCustomers;
}
public hierarchicalGridPinHeaderTemplate = (ctx: IgcColumnTemplateContext) => {
const column = (ctx asany).column;
return html`<div><spanstyle="float:left">${column.field}</span><spanstyle="float:right" @pointerdown=${(e: any) => this.toggleColumnPin(column)}>📌</span></div>`;
};
publictoggleColumnPin(field: IgcColumnComponent) {
if(field) {
field.pinned = !field.pinned;
}
}
}
new Sample();
ts
<!DOCTYPE html><html><head><title>Sample | Ignite UI | Web Components | infragistics</title><metacharset="UTF-8" /><linkrel="shortcut icon"href="https://static.infragistics.com/xplatform/images/browsers/wc.png" ><linkrel="stylesheet"href="https://fonts.googleapis.com/icon?family=Material+Icons" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Kanit&display=swap" /><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Titillium Web" /><linkrel="stylesheet"href="https://static.infragistics.com/xplatform/css/samples/shared.v8.css" /><linkrel="stylesheet"href="/src/index.css"type="text/css" /></head><body><divid="root"><divclass="container sample ig-typography"><divclass="container fill"><igc-hierarchical-gridauto-generate="false"moving="true"name="grid"id="grid"id="grid"><igc-paginatorname="paginator"id="paginator"per-page="15"></igc-paginator><igc-columnfield="CustomerID"data-type="string"name="column1"id="column1"></igc-column><igc-columnfield="Company"data-type="string"width="150px"name="column2"id="column2"></igc-column><igc-columnfield="ContactName"data-type="string"width="150px"name="column3"id="column3"></igc-column><igc-columnfield="ContactTitle"data-type="string"width="150px"name="column4"id="column4"></igc-column><igc-columnfield="Address"data-type="string"name="column5"id="column5"></igc-column><igc-columnfield="City"data-type="string"name="column6"id="column6"></igc-column><igc-columnfield="PostalCode"data-type="string"name="column7"id="column7"></igc-column><igc-columnfield="Country"data-type="string"name="column8"id="column8"></igc-column><igc-columnfield="Phone"data-type="string"name="column9"id="column9"></igc-column><igc-columnfield="Fax"data-type="string"name="column10"id="column10"></igc-column><igc-row-islandchild-data-key="Orders"auto-generate="false"moving="true"><igc-columnfield="OrderID"data-type="number"></igc-column><igc-columnfield="EmployeeID"data-type="number"></igc-column><igc-columnfield="OrderDate"data-type="date"></igc-column><igc-columnfield="RequiredDate"data-type="date"></igc-column><igc-columnfield="ShippedDate"data-type="date"></igc-column><igc-columnfield="ShipVia"data-type="number"></igc-column><igc-columnfield="Freight"data-type="number"></igc-column><igc-columnfield="ShipName"data-type="string"></igc-column><igc-columnfield="ShipAddress"data-type="string"></igc-column><igc-columnfield="ShipCity"data-type="string"></igc-column><igc-columnfield="ShipPostalCode"data-type="string"></igc-column><igc-columnfield="ShipCountry"data-type="string"></igc-column><igc-row-islandchild-data-key="OrderDetails"auto-generate="false"moving="true"><igc-columnfield="ProductID"data-type="number"></igc-column><igc-columnfield="UnitPrice"data-type="number"></igc-column><igc-columnfield="Quantity"data-type="number"></igc-column><igc-columnfield="Discount"data-type="number"></igc-column></igc-row-island></igc-row-island></igc-hierarchical-grid></div></div></div><!-- This script is needed only for parcel and it will be excluded for webpack -->
<% if (false) { %><scriptsrc="src/index.ts"></script><% } %>
</body></html>html