How to expand a string from a datatable programmatically

I want to expand the line directly from the typescript method, I tried with @viewchild, but it does not work:

HTML:

<p-dataTable [value]="array" [responsive]="true" expandableRows="true" #dt
   (onRowClick)="dt.toggleRow($event.data)">
    <template let-test pTemplate="rowexpansion">
        <div class="ui-grid ui-grid-responsive ui-fluid">
            <div class="ui-grid-row">
                Bouh !
            </div>
        </div>
    </template>
    <p-column field="name" header="Vin"></p-column>
    <p-column field="company" header="Year"></p-column> 
</p-dataTable>
<button class="btn" (click)="addComment()">
    <p>add comment</p>
</button>

Typescript:

import { DataTable } from 'primeng/components/datatable/datatable';

export class MyAwesomeComponent implements OnInit {

@ViewChild('dt') datatable : DataTable;

addComment(): void {
    console.log(this.datatable);
    this.datatable.toggleRow(1);
}
Line

doesn't expand if someone can tell me how to do the same thing as the event (onRowClick), but inside the typescript method, I will be grateful.

Showcase for another html example

+4
source share
5 answers

There is a property expandedRowsthat is an array of all current extended strings. You should be able to add / remove your line to this list to switch the line extension.

You will need:

<p-dataTable [expandableRows]="true" [expandedRows]="expandedItems"></p-dataTable>

and

expandedItems: Array<any> = new Array<any>();
// expand row
this.expandedItems.pop(this.gridData[rownumber]);
// hide row
this.expandedItems.push(this.gridData[rownumber]);

, ... https://plnkr.co/edit/Zra8UUMv4XQCv0lbRbMg?p=preview

+6

toggleRow , , :

<p-dataTable #dt [value]="cars" expandableRows="true"
    (onRowClick)="dt.toggleRow($event.data)">

  <p-column field="year" header="Year"></p-column>

  <ng-template let-car pTemplate="rowexpansion">
    expanded row
    {{ car.brand }}
  </ng-template>

</p-dataTable>
+1

, ,

 <p-dataTable 
                    [value]="reviewPanels">
<p-column [style]="{'width':'35px'}" expander="true" ></p-column>
<p-column  ></p-column>
<ng-template let-cp pTemplate="rowexpansion">

 <p-dataTable 
         [value]="cp.listreviewerlist" >
<p-column></p-column>
</p-dataTable>
</ng-template>
</p-dataTable>
0

:

addComment(): void {
    console.log(this.datatable);
    this.datatable.toggleRow(this.datatable.selection);
}
0

(, [pSelectableRow] = "rowData" .

<p-button label="Save" [pRowToggler]="rowData"></p-button>

- , #dtEntry: -

<p-table #dtEntry [columns]="dataEntryColumns" [value]="data.rptSkMimp" 
            selectionMode="single" [paginator]="true" [rows]="10" dataKey="txnId">

- - , (rowdata, dtEntry)

<p-button icon="fa fa-fw fa-plus" label="Save" (click)="onRowSave(rowData, dtEntry)"></p-button>

You may have the following script in your code to handle onRowSave.

onRowSave(rowData:any, dt:any) {   
dt.toggleRow(rowData);   

}

0
source

Source: https://habr.com/ru/post/1672481/


All Articles