Row Header Formatting

Posted by: ken_perregaux on 26 February 2019, 5:44 pm EST

  • Posted 26 February 2019, 5:44 pm EST

    We are using Wijmo FlexGrid (5.20183.550) with Angular 6 and we have created a our own component which extends WjFlexGrid because we didn’t want to have to put a template on every table we use (we have several) to show the row number in the row header.

    I have 2 problems…

    1. When we do this and go into edit mode we have no lost the little edit icon. Can you explain how to have both the edit icon and the row number in the row header.

    2. Sometimes I want to override the row header template in one of our tables to show an icon to depict something special is in the row. I have not been very successful with this other than using itemFormatter in the table. However the row header column does not expand to the ‘new’ size due to the initial template being used to calculate the size (I’m guessing). I have also tried using the onFormatItem in the base class for the row number and that doesn’t let me do anything in the components table (even with itemFormatter). What is the best way to override the template and/or is there a better way our ‘base’ component can set the row headers so that it can be overriden.

  • Posted 27 February 2019, 3:06 am EST

    Hi,

    1. When we do this and go into edit mode we have no lost the little edit icon.

    When we add custom templates, then grid uses our custom template and does not apply the default formatting. So to display the icon the icon along with the numbers, what we need to do is add a span tag with ‘wj-glyph-pencil’ class if the current item is editing.

    Please refer to the following code snippet:

    <wj-flex-grid #grid [itemsSource]="data" (initialized)="init(grid)">
    <ng-template wjFlexGridCellTemplate [cellType]="'RowHeader'" let-row="row" let-item="item">
        {{row.index}}
        <span *ngIf="item == grid.editableCollectionView.currentEditItem" class="wj-glyph-pencil"></span>
      </ng-template>
    </wj-flex-grid>
    
    

    2.However the row header column does not expand to the ‘new’ size due to the initial template being used to calculate the size (I’m guessing).

    To change the columns width, we need to set the defaultSize of the rowHeaders column collection. Please refer to the following code snippet:

    flex.rowHeaders.columns.defaultSize = 50;
    

    You may also refer to the following sample demonstrating the same:

    https://stackblitz.com/edit/angular-ksddbf?file=src%2Fapp%2Fapp.component.html

    In the above sample, we have used the cell-templates to apply formatting and initialized event to set the default Column size for rowHeaders. However, it may also depend on how the FlexGrid is inherited, if the component is created by actually extending the WjFlexGrid class then we may override the initialize method to set the default values whereas if our component is just a wrapper around the WjFlexGrid then initialized event is the perfect place for setting default values.

    Also, could you please share some code snippets of your implementation so that we may suggest you the best possible alternative accordingly.

    ~Sharad

  • Posted 27 February 2019, 10:44 am EST

    Here is our extension table .ts…

    import { Component, forwardRef } from ‘@angular/core’;

    import { WjFlexGrid, wjFlexGridMeta } from ‘wijmo/wijmo.angular2.grid’;

    import * as wjGrid from ‘wijmo/wijmo.grid’;

    import * as wjCore from ‘wijmo/wijmo’;

    @Component({

    selector: ‘wat-flexgrid’,

    templateUrl: ‘./wat-flexgrid.component.html’,

    inputs: […wjFlexGridMeta.inputs, ‘rowToSelect’, ‘fromOnGridInit’, ‘restoreSelectionMode’],

    outputs: […wjFlexGridMeta.outputs],

    providers: [

    { provide: ‘WjComponent’, useExisting: forwardRef(() => WatFlexGridComponent) },

    …wjFlexGridMeta.providers

    ]

    })

    export class WatFlexGridComponent extends WjFlexGrid

    {

    rowToSelect = 0;

    fromOnGridInit = false;

    restoreSelectionMode = null;

    created()
    	this.allowSorting = false;
    }
    
    onFormatItem(e: wjGrid.FormatItemEventArgs)
    {
    	// Remove padding when editing cells
    	if (e.cell.children[0] instanceof HTMLInputElement)
    	{
    		e.cell.style.padding = '0px';
    	}
    }
    
    onLoadedRows(e?: wjCore.EventArgs): void
    {
    	//If we don't have any rows, get out
    	if (this.rows == null || this.rows.length === 0)
    	{
    		return;
    	}
    
    	//If the specified row to select doesn't exist, select the last row
    	//because it probably means we just deleted the (previous) last row
    	if (this.rows[this.rowToSelect] == null)
    	{
    		this.rowToSelect = this.rows.length - 1;
    	}
    
    	//The main reason to have onLoadedRows is to select a specified row
    	this.selectRow();
    
    	//Some components use the fromOnGridInit flag and it needs to be reset
    	this.fromOnGridInit = false;
    
    	//Some components turn off selection mode, and it needs to be restored
    	if (this.restoreSelectionMode != null)
    	{
    		this.selectionMode = this.restoreSelectionMode;
    		this.restoreSelectionMode = null;
    	}
    }
    
    selectRow()
    {
    	this.scrollIntoView(this.rowToSelect, 0);
    	this.select(this.rowToSelect, 0);
    }
    

    }

    And the .html…

    <ng-template wjFlexGridCellTemplate [cellType]=“‘RowHeader’” let-cell=“cell”>

    {{cell.row.index+1}}

    And the .html when using our new table…

    <wat-flexgrid #rsnflex
            selectionMode="Row"
    	[itemsSource]="data"
    	(cellEditEnded)="onCellEditEnded($event)"
    	(selectionChanged)="onSelectionChanged($event)"
    	id="tableInjections">
    </wat-flexgrid>
    

    So as you can see we use a template to show the row number in out table and I want to override this when we use the table. If in the .ts file for this I use an itemFormatter then I can get something near to what I want but I would like the column to get bigger based on the HTML being used not a defaultSize (or am I not understanding what defaultSize is?). The reason I ask is because if I set the defaultSize to 25 and the number of data rows to 200…the numbers and/or the ‘s’ get cutoff…how do I get it to grow to the data?

  • Posted 28 February 2019, 3:29 am EST

    The reason why you were not able to override the ‘RowHeader’ template was that the template defined in the table was compiling before the default ‘RowHeader’ template causing the default template to override the template defined in the table.

    To overcome this problem we need to wrap the template in the table inside an ng-template and then in our extended grid, we need to define a place where this template needs to be projected.

    Please refer to the following sample which demonstrates the same:

    https://stackblitz.com/edit/angular-zhse7z?file=src%2Fapp%2Fapp.component.html

    To auto-size the columns according to the content, we could simply call the autoSizeColumn/autoSizeColumns method on the grid(created callback is a good candidate for this purpose).

    Api doc: https://demos.wijmo.com/5/Angular/WijmoHelp/WijmoHelp/topic/wijmo.grid.FlexGrid.Class.html#autoSizeColumns

    Also, in the provided code snippet, we could observe that you are overriding the onLoadedRows and onFormatItem methods without calling super.onLoadedRow()/super.onFormatItem() in the respective methods. These are used for raising events in the base FlexGrid class so overriding them would make the grid to not trigger loadedRows and formatItem event. A better approach would be to attach handler to the loadedRows/formatItem event instead of overriding the event raiser methods.

  • Posted 28 February 2019, 3:35 pm EST

    Thanks the template is now working. However, the one thing that I still can’t get to work is the autoSizeColumn. It does nothing for me in the extended tables create method (with our without the setTimeout). The only place it has any affect is if I use it in the loadedRowsHandler (and even then it doesn’t work for my new templates for the row header).We load our data from the server, I’m wondering if its some kind of timing thing…I tried the setTimeout with a 1000 and still didn’t work.

  • Posted 1 March 2019, 1:04 am EST

    It is indeed a timing issue. In this case, we could use updatedLayout event to autoSize the columns. Please refer to the following code snippet and updated sample and let us know if you face any further issues:

    loadedRowsHandler(self, e?: wjCore.EventArgs): void {
        let updatedLayoutHandler = function(s,e){
          s.updatedLayout.removeHandler(updatedLayoutHandler);
          s.autoSizeColumn(0, true, 9);
        }
        self.updatedLayout.addHandler(updatedLayoutHandler);
    }
    

    https://stackblitz.com/edit/angular-bl7uxb?file=src%2Fapp%2Fextended-grid%2Fextended-grid.component.ts

  • Posted 1 March 2019, 4:38 pm EST

    Thanks, however, I am still having a small issue. If the table has more rows than can be seen, the auto sizing goes crazy and makes the header row very big so that the rest of the data can’t be seen even when scrolled and only if the ngIf’d element(s) are in rows that are not visible. The width also seems to get wider the more rows there are. If the ngIf works for all rows there are no problems. It’s almost like the column grows by the number of rows that satisfy the ngIf. Actually looking at the widths of the elements added next to the row number if I multiply that width times the number of rows affected, that is the width that the row header column is. It also is dependent on the size of the ‘img’ element or length of text I am adding.

    Your example doesn’t which is really strange.

  • Posted 4 March 2019, 12:09 am EST

    We are sorry but we are unable to replicate the issue at our end. Could you please share a small sample replicating the issue or let us know if we need to make some changes in the previously shared sample in order to replicate the issue so that we could further investigate the issue. Also could you please share your environment details such as OS, browser, Wijmo build version so that we may test on the same environment.

  • Posted 5 March 2019, 2:55 am EST

    Hi Ken,

    We have created a thread on our private support portal in favour of this thread. We would request you to continue the further discussion there. Support case could be found here: http://supportone.componentone.com/casedetail/369325

    Regards

Need extra support?

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

Learn More

Forum Channels