jQWidgets Forums

jQuery UI Widgets Forums Angular Command Column buttons only at child rows

This topic contains 2 replies, has 2 voices, and was last updated by  Stanislav 7 years, 4 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author

  • svedavya
    Participant

    Hi Team,

    We are really grateful for such a wonderful Grid which you have provided with lots of features and thanks a lot for your efforts and service. Recently i have come across a scenario to add edit buttons only to child rows inside tree grid and I have followed command
    columns documentation for angular 4 with following link https://www.jqwidgets.com/angular/angular-treegrid/angular-treegrid-commandcolumn.htm and generated grid. whereas this will add buttons at every row but in my case i need buttons to be shown only for child rows and leave parent as empty cell. Can you please help me with the workaround to identify child rows inside rendered():void function.

    Appreciate your support

    Thanks & Regards
    Sandeep Vedavyas


    svedavya
    Participant

    Hi Team,

    Any suggestions on this, please ? appreciate your help.

    Thanks
    Sandeep


    Stanislav
    Participant

    Hello Sandeep,

    Here is a snippet of an example.

    import { Component, ViewChild, ViewEncapsulation, ElementRef } from '@angular/core';
    
    import { jqxTreeGridComponent } from 'jqwidgets-scripts/jqwidgets-ts/angular_jqxtreegrid';
    
    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html'
    })
    
    export class AppComponent {
        @ViewChild('myTreeGrid') myTreeGrid: jqxTreeGridComponent;
        @ViewChild('events') events: ElementRef;
    
        data: any[] = [
            {
                'id': '1', 'name': 'Corporate Headquarters', 'budget': '1230000', 'location': 'Las Vegas',
                'children':
                [
                    {
                        'id': '2', 'name': 'Finance Division', 'budget': '423000', 'location': 'San Antonio',
                        'children':
                        [
                            { 'id': '3', 'name': 'Accounting Department', 'budget': '113000', 'location': 'San Antonio' },
                            {
                                'id': '4', 'name': 'Investment Department', 'budget': '310000', 'location': 'San Antonio',
                                'children':
                                [
                                    { 'id': '5', 'name': 'Banking Office', 'budget': '240000', 'location': 'San Antonio' },
                                    { 'id': '6', 'name': 'Bonds Office', 'budget': '70000', 'location': 'San Antonio' },
                                ]
                            }
                        ]
                    },
                    {
                        'id': '7', 'name': 'Operations Division', 'budget': '600000', 'location': 'Miami',
                        'children':
                        [
                            { 'id': '8', 'name': 'Manufacturing Department', 'budget': '300000', 'location': 'Miami' },
                            { 'id': '9', 'name': 'Public Relations Department', 'budget': '200000', 'location': 'Miami' },
                            { 'id': '10', 'name': 'Sales Department', 'budget': '100000', 'location': 'Miami' }
                        ]
                    },
                    { 'id': '11', 'name': 'Research Division', 'budget': '200000', 'location': 'Boston' }
                ]
            }
        ];
    
        source: any =
        {
            dataType: 'json',
            dataFields: [
                { name: 'name', type: 'string' },
                { name: 'budget', type: 'number' },
                { name: 'id', type: 'number' },
                { name: 'children', type: 'array' },
                { name: 'location', type: 'string' }
            ],
            hierarchy:
            {
                root: 'children'
            },
            localData: this.data,
            id: 'id'
        };
    
        dataAdapter: any = new jqx.dataAdapter(this.source);
    
        columns: any[] =
        [
            { text: 'ID', editable: false, dataField: 'id', width: 150 },
            { text: 'Name', dataField: 'name', width: 250 },
            { text: 'Budget', align: 'right', cellsAlign: 'right', cellsFormat: 'c2', dataField: 'budget', width: 150 },
            { text: 'Location', dataField: 'location', width: 130 },
            {
                text: 'Edit', cellsAlign: 'center', align: 'center', columnType: 'none', editable: false, sortable: false, dataField: null,
                cellsRenderer: (row: number, column: any, value: any, rowData: any): string => {
                    if (rowData.children) {
                        return '';
                    } else {
                        return <code><div data-row='</code> + row + 

    ‘ class=’editButton’ style=’margin-left: 4em; float: left’></div>
    <div data-row=’+ row +‘ class=’cancelButton’ style=’display: none; float: left; margin-left: 1em’></div>`;
    }
    }
    }
    ];

    editSettings: any =
    {
    saveOnPageChange: true, saveOnBlur: true,
    saveOnSelectionChange: false, cancelOnEsc: true,
    saveOnEnter: true, editOnDoubleClick: false, editOnF2: false
    };

    rendered = (): void => {
    let myClass = document.querySelectorAll(‘.editButton’);
    console.log(myClass)

    if (myClass.length !== 0) {
    let uglyEditButtons = jqwidgets.createInstance(‘.editButton’, ‘jqxButton’, { width: 60, height: 24, value: ‘Edit’ });
    let flattenEditButtons = flatten(uglyEditButtons);

    let uglyCancelButtons = jqwidgets.createInstance(‘.cancelButton’, ‘jqxButton’, { width: 60, height: 24, value: ‘Cancel’ });
    let flattenCancelButtons = flatten(uglyCancelButtons);

    function flatten(arr: any[]): any[] {
    if (arr.length) {
    return arr.reduce((flat: any[], toFlatten: any[]): any[] => {
    return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
    }, []);
    }
    }

    if (flattenEditButtons) {
    for (let i = 0; i < flattenEditButtons.length; i++) {
    flattenEditButtons[i].addEventHandler(‘click’, (event: any): void => {
    this.editClick(event);
    });
    }
    }

    if (flattenCancelButtons) {
    for (let i = 0; i < flattenCancelButtons.length; i++) {
    flattenCancelButtons[i].addEventHandler(‘click’, (event: any): void => {
    let rowKey = event.target.getAttribute(‘data-row’);
    this.myTreeGrid.endRowEdit(rowKey, true);
    });
    }
    }
    }
    }

    ready = (): void => {
    this.myTreeGrid.expandAll();
    }

    rowKey: number = -1;

    rowClick(event: any): void {
    this.rowKey = event.args.key;
    };

    editClick(event: any): void {
    let editButtonsContainers = document.getElementsByClassName(‘editButton’);
    let cancelButtonsContainers = document.getElementsByClassName(‘cancelButton’);

    let value = event.target.innerText;
    if (value === ‘Edit’) {
    this.myTreeGrid.beginRowEdit(this.rowKey.toString());

    for (let i = 0; i < editButtonsContainers.length; i++) {
    (<HTMLElement>editButtonsContainers[i]).style.marginLeft = ‘4em’;
    (<HTMLElement>cancelButtonsContainers[i]).style.display = ‘none’;
    }

    (<HTMLElement>editButtonsContainers[this.rowKey – 1]).innerText = ‘Save’;
    (<HTMLElement>editButtonsContainers[this.rowKey – 1]).style.marginLeft = ‘1em’;

    (<HTMLElement>cancelButtonsContainers[this.rowKey – 1]).style.display = ‘inline-block’;

    } else {
    (<HTMLElement>editButtonsContainers[this.rowKey – 1]).innerText = ‘Edit’;
    (<HTMLElement>editButtonsContainers[this.rowKey – 1]).style.marginLeft = ‘4em’;

    (<HTMLElement>cancelButtonsContainers[this.rowKey – 1]).style.display = ‘none’;

    this.myTreeGrid.endRowEdit(this.rowKey.toString());
    }
    }
    }`

    Best Regards,
    Stanislav

    jQWidgets Team
    http://www.jqwidgets.com/

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.