FlexSheet. Disable cell edit and change background

Posted by: Gopala.Chintakindi on 26 September 2018, 2:54 pm EST

    • Post Options:
    • Link

    Posted 26 September 2018, 2:54 pm EST - Updated 3 October 2022, 8:22 pm EST

    I am able to disable cell edit but when I change background color it is not working. Please see attached image.

    I am dynamically creating sheets and adding data. JSON will tell me which cells are editable and which are not.

    <wj-flex-sheet #flexSheet [selectedSheetIndex]="0" (initialized)="flexSheetInitialize(flexSheet)" (dragover)="dragOverFile($event)" (drop)="dropFile($event, flexSheet)" >
        <wj-sheet name="Country" [itemsSource]="countryFlexSheetData"></wj-sheet>
        <wj-sheet name="Number" [rowCount]="20" [columnCount]="8"></wj-sheet>
        <wj-sheet name="Date" [rowCount]="20" [columnCount]="8"></wj-sheet>
    </wj-flex-sheet>
    

    Code to add new columns to my sheet.

    public addNewData() {
            this.commonService.getSheetData().subscribe(
                (columnsCollection: ISheetColumnCollection) => {     
                    console.log(columnsCollection)           ;
                    if (columnsCollection !== null && columnsCollection !== undefined &&
                        columnsCollection.columns !== null && columnsCollection.columns !== undefined &&
                        columnsCollection.columns.length > 0) {
                            columnsCollection.columns.map(
                                (column: ISheetColum) => {
                                    this.flexSheet.columns.push(new wjcGrid.Column({ header: column.header, width: column.width}));
                                    if (column.cells !== null && column.cells !== undefined &&
                                       column.cells.length > 0) {
                                           let rowIndex = 1;
                                           const columnIndex = this.flexSheet.columns.length - 1;
                                           for (let i = 0; i <  column.cells.length; i++) {
                                               const cell = column.cells[i];
                                               if (cell.isHeadingCell !== undefined && cell.isHeadingCell !== null &&
                                                cell.isHeadingCell === true) {
                                                    // header cell
                                                    this.flexSheet.setCellData(0, columnIndex, cell.value);
                                                    headerColumnStyle = <wjcGridSheet.ICellStyle>{};
                                                    headerColumnStyle.backgroundColor = cell.style.backgroundColor;
                                                    headerColumnStyle.fontWeight = cell.style.fontWeight;
                                                    headerColumnStyle.fontFamily = cell.style.fontFamily;
        
                                                    const newHeaderCells: wjcGrid.CellRange[] = [];
                                                    newHeaderCells.push(
                                                        new wjcGrid.CellRange(
                                                            0, this.flexSheet.columns.length - 1, 
                                                            0, this.flexSheet.columns.length - 1));
                                                    this.flexSheet.applyCellsStyle(headerColumnStyle, newHeaderCells);
                                                    this.disableRowsColumns.push(
                                                        {
                                                            row: 0, 
                                                            col: columnIndex, 
                                                            changeBackground: false
                                                        });
                                                } else {
                                                    this.flexSheet.setCellData(rowIndex, columnIndex, cell.value);
                                                    rowIndex = rowIndex + 1;
                                                    if(cell.disabled !== null && cell.disabled !== undefined &&
                                                        cell.disabled === true) {
                                                            this.disableRowsColumns.push(
                                                                {
                                                                    row: rowIndex, 
                                                                    col: columnIndex, 
                                                                    changeBackground: true
                                                                });
                                                        }
                                                }
                                           }
                                       }
                                }
                            );
                        }
                }, 
                (error: any) => {
                    console.log(error);
                }
            );
        }
    

    Code to disable the cell and change backgorund. I want to know when to call this code

      public disableFlexSheetCells() {
            this.flexSheet.beginningEdit.addHandler(
                function(sender, e: any) {
                    if (e.row === 3 && e.col === 3) {
                        e.cancel = true;
                    }
                }
            );
            const element: HTMLElement = this.flexSheet.cells.getCellElement(3, 3);
            if (element !== null && element !== undefined) {
                element.setAttribute("style", "background-color: red;");
            }
        }
    

  • Posted 27 September 2018, 3:33 am EST

    Hi,

    If we understand correctly then you are trying to add some columns dynamically and making some cells read-only and want to style the read-only cells differently.

    If that is the case then you may handle the formatItem event and check if the cell is read-only and style them accordingly.

    Please refer to the following sample which implements the same:

    https://stackblitz.com/edit/angular-hmtebq?file=app%2Fapp.component.ts

    ~Sharad

  • Posted 27 September 2018, 1:35 pm EST

    Thanks. I will look into this and get back to you.

  • Posted 27 September 2018, 2:45 pm EST

    Thank you very much. It works. I added below code in my public flexSheetInitialize(flexSheet: wjcGridSheet.FlexSheet) method

        const that = this;
            flexSheet.beginningEdit.addHandler(
                function(sender: wjcGridSheet.FlexSheet, e: any) {                
                    if (sender.selectedSheet.name === 'UI') {
                       if (that.isReadOnlyCell(e.row, e.col)) {
                           e.cancel = true;
                       }
                    }
            });
    
            // style read only cell
            flexSheet.formatItem.addHandler((s: wjcGridSheet.FlexSheet, ev: any) => {
                if (s.selectedSheet.name === 'UI') {
                    if (that.isReadOnlyCell(ev.row, ev.col)) {
                        //read only cell
                        const readOnlyCell = this.readOnlyCells.find( cell => cell.row === ev.row && cell.col === ev.col);
                        if (readOnlyCell !== null && readOnlyCell !== undefined &&
                        readOnlyCell.changeBackground !== null && readOnlyCell.changeBackground !== null &&
                        readOnlyCell.changeBackground === true) {
                            // change background color
                            wjcCore.addClass(ev.cell, 'read-only-cell');
                        } else {
                            // donot change background color
                            wjcCore.removeClass(ev.cell, 'read-only-cell');                        
                        }
                    } else {
                        //not readonly
                        wjcCore.removeClass(ev.cell, 'read-only-cell');
                    }
                }
            });
    
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels