ng2-table icon indicating copy to clipboard operation
ng2-table copied to clipboard

Change class of a row.

Open eolcum opened this issue 8 years ago • 6 comments

Is it possible to change a style class of a specific row? I want to do it in the use case: user clicks on a row and the row highlights.

eolcum avatar Nov 09 '16 09:11 eolcum

I have the same requirement, I pulled down the code and currently trying to implement. Will share if I get it to work.

I am adding an id attribute to the td using the list index and a config property for the name, I will then add a rowSelect output with the index and selected row. Based on the return you will be able to style the row.

kubent avatar Nov 10 '16 18:11 kubent

Found a simpler solution, since it uses bootstrap all that needs to happen is for the tr to toggle the "selected" class. You can avoid multi-select by clearing all selected on the next click.

kubent avatar Nov 10 '16 18:11 kubent

I have the same question too... will follow on this.

flycat52 avatar Nov 13 '16 14:11 flycat52

A quick and dirty solution:

Step 1: Integrate jQuery Step 2: Give your result table an ID like:

<ng-table id="resultDataTable" ...

Step 3: Modify your onCellClick method:


onCellClick(data: any): any {
    /* get index of row */
    let index = this.tableData.indexOf(data.row);  

    /* add an class 'active' on click */
    $('#resultDataTable').on('click', 'tr', function (event: any) {
      //noinspection TypeScriptUnresolvedFunction
      $(this).addClass('active').siblings().removeClass('active');
    });
}

webia1 avatar Dec 09 '16 12:12 webia1

@valorkin do you have any ways to change class of row selected ? Like @kubent and @eolcum, i search to set css class on row selected by user.

Thx for your help.

bigbo26 avatar Feb 06 '17 10:02 bigbo26

As a workaround:

In the table.component.ts

export abstract class TableComponent {
    @ViewChild('tableContainer') tableContainer: any;
    tableSelectedItem: any;

    public onChangeTable(config: any, page: any = {page: this.page, itemsPerPage: this.itemsPerPage}): any {
        // Your changeTable code here

        this.getRows().then(() => setTimeout(() => this.highlightSelectedRow(this.tableSelectedItem), 10));
    }

    public onCellClick(data: any): any {
        this.highlightSelectedRow(data.row);
        this.tableSelectedItem = data.row;
    }

    private highlightSelectedRow(data: any): void {
        const rowNumber: number = data ? this.rows.findIndex(el => el.uid === data.uid) : -1;
        const trs = this.tableContainer.nativeElement.children[0].children[0].children[1].children;
        if (rowNumber !== -1) {
            if (rowNumber > -1 && rowNumber < trs.length) { // Avoid updating class if no need to
                if (trs[rowNumber].className === 'active') {
                    return;
                }
            }
            for (let i = 0; i < trs.length; i++) {
                trs[i].className = (i === rowNumber) ? 'active' : '';
            }
        }
    }
}

In table.component.html:

<div #tableContainer>
    <ng-table
        [config]="config"
        (tableChanged)="onChangeTable(config)"
        (cellClicked)="onCellClick($event)"
        [rows]="rows"
        [columns]="columns">
    </ng-table>
</div>

exasky avatar Feb 28 '18 10:02 exasky