@lidorsystems/integralui-web
Version:
IntegralUI Web - Advanced UI Components for Angular
274 lines (229 loc) • 10.6 kB
text/typescript
/*
Copyright © 2016-2019 Lidor Systems. All rights reserved.
This file is part of the "IntegralUI Web" Library.
The contents of this file are subject to the IntegralUI Web License, and may not be used except in compliance with the License.
A copy of the License should have been installed in the product's root installation directory or it can be found at
http://www.lidorsystems.com/products/web/studio/license-agreement.aspx.
This SOFTWARE is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language
governing rights and limitations under the License. Any infringement will be prosecuted under applicable laws.
*/
import { Component, ViewContainerRef, ViewChild, ViewChildren, ViewEncapsulation } from '@angular/core';
import { IntegralUIGrid } from '../../integralui/components/integralui.grid';
export class GridAddRemoveSample {
applicationRef: ViewContainerRef;
grid: IntegralUIGrid;
// An array that holds grid columns
public columns: Array<any>;
// An array that holds grid rows
public rows: Array<any>;
public disableButtons: boolean = true;
public insertPos: number = 0;
public removePos: number = 0;
private columnCount: number = 2;
private rowCount: number = 3;
private rowIndex: number = -1;
public gridStyle: any = {
general: {
normal: 'grid-adrm-normal'
}
}
constructor(){
this.columns = [
{ id: 1, headerText: "Header1", footerText: "Footer1" },
{ id: 2, headerText: "Header2", footerText: "Footer2" }
];
this.rows = [
{ id: 1, text: "Row 1", cells: [{ cid: 1, text: "Item11" }, { cid: 2, text: "Item12" }] },
{ id: 2, text: "Row 2", cells: [{ cid: 1, text: "Item21" }, { cid: 2, text: "Item22" }] },
{ id: 3, text: "Row 3", cells: [{ cid: 1, text: "Item31" }, { cid: 2, text: "Item32" }] }
];
}
// Add Remove Columns ----------------------------------------------------------------
createNewColumn(){
this.columnCount++;
return { id: this.columnCount, headerText: "Header" + this.columnCount, footerText: "Footer" + this.columnCount };
}
addColumn(){
let column: any = this.createNewColumn();
this.grid.addColumn(column);
this.grid.updateLayout();
}
removeColumn(){
if (this.grid.selectedColumn){
this.grid.removeColumn(this.grid.selectedColumn);
this.grid.updateLayout();
}
}
clearColumns(){
this.grid.clearColumns();
this.grid.updateLayout();
}
// Add Remove Rows ----------------------------------------------------------------
createNewRow(){
this.rowCount++;
let row: any = {
id: this.rowCount,
text: "Row " + this.rowCount,
cells: []
}
for (let j = 1; j <= this.columns.length; j++){
let columnId: number = this.columns[j-1].id;
row.cells.push({ cid: columnId, text: "Item" + this.rowCount + columnId });
}
return row;
}
addRow(){
let row: any = this.createNewRow();
this.grid.addRow(row);
}
insertRowAfter(){
let row: any = this.createNewRow();
this.grid.insertRowAfter(row, this.grid.selectedRow);
}
insertRowBefore(){
let row: any = this.createNewRow();
this.grid.insertRowBefore(row, this.grid.selectedRow);
}
insertRowAt(){
let row: any = this.createNewRow();
this.grid.insertRowAt(row, this.insertPos);
}
removeRow(){
if (this.grid.selectedRow)
this.grid.removeRow(this.grid.selectedRow);
}
removeRowAt(){
this.grid.removeRowAt(this.removePos, this.grid.selectedRow);
}
clearRows(){
this.grid.clearRows();
this.grid.updateLayout();
}
onRowAdded(e: any){
this.grid.updateLayout();
}
onRowRemoving(e: any){
this.rowIndex = -1;
let list: Array<any> = this.grid.getList();
if (list && list.length > 0)
this.rowIndex = list.indexOf(e.row);
}
onRowRemoved(e: any){
this.selectNewRow();
if (!this.grid.selectedRow)
this.disableButtons = true;
this.grid.updateLayout();
}
onClearRows(){
this.disableButtons = true;
}
onAfterSelect(e: any){
this.disableButtons = this.grid.selectedRow ? false : true;
}
selectNewRow(){
let list: Array<any> = this.grid.getList();
if (list && list.length > 0){
if (this.rowIndex == list.length)
this.rowIndex = list.length - 1;
if (this.rowIndex >= 0 && this.rowIndex < list.length){
if (this.rowIndex < list.length)
this.grid.selectedRow = list[this.rowIndex];
else
this.grid.selectedRow = list[list.length-1];
}
}
else
this.grid.selectedRow = null;
}
}