Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
215
Persisting changeable state while keeping underlying column definitions
posted

It is listed as a limitation that the columns state doesn't allow functions and so to handle those in the initColumns but I have several templates for the column content so that doesn't seem very practical. Additionally if you store the entire column state then as far as I can tell if I add new columns in future any users with state stored won't pick up those new columns. So rather than restore the entire state of columns I just want to apply the things the user can actual change - visibility, pinning and order. Is there a way to get the existing columns definition to merge with the state first or to apply those parts programatically? I can see you can set pinning through api functions so I could read the state and call functions myself but I can't see how to hide or move columns through api. Is there anyway to achieve this?

Parents Reply
  • 16310
    Offline posted in reply to Katy Faulkner

    Hi Katy,

    See this sample that I have prepared for you.

    What it does is it extends the IgxGridState directive and subscribes to the onColumnInit event for the corresponding grid. This way, you save the burden of having to subcsribe to each of the many grids you have, rather than that - you do it once in the directive, and this directive is applied to each grid (as it is now, right ?)

    In short, here is the summarized thing to do:

    export class IgxGridStateExtendedDirective extends IgxGridStateDirective {
    public template: TemplateRef<any>;
    constructor(
        @Host() @Optional() protected currGrid: IgxGridComponent) {
            currGrid.onColumnInit.subscribe((column: IgxColumnComponent) => {
                onColumnInit(column, this.template);
            });
        }
    }
      
    export function onColumnInit(column: IgxColumnComponent, template: any) {
    if (column.field === 'IsActive') {
        column.bodyTemplate = template;
    }
    }
      
    export class GridSaveStateComponent implements OnInit, AfterViewInit {
        public restoreGridState() {
            this.state.template = this.isActiveTemplate;
            ...
        }
    }
     

    There are few more things to do, see them in the example. In short, from the component that holds your grid, you need to pass the templates/functions to the state directive, as it is done in the restoreGridState method. My example does it for one template, but you can do it for anything.

    Please review it and let me know if you have any questions.

    Hristo

Children