简体   繁体   中英

Unable to add custom buttons in ngx-datatable Angular

I am new to angular and ngx-datatable and before asking this questions on SO. I've already gone through the answers with same problem which I'm facing related to adding custom buttons in each row in ngx-datatable of angular .

My HTML template looks like this:


<ngx-datatable [rows]="rows" [loadingIndicator]="loadingIndicator" class="bootstrap"
        [selected]="selected" (activate)="onActivate($event, NewEventContent)">        
      
        <ngx-datatable-column *ngFor="let column of columns; let i = index;" 
        name="{{column.name}}" prop="{{column.prop}}">        

        <ngx-datatable-column name="Actions" prop="skuCode"></ngx-datatable-column>          

        <ng-template let-value="value" let-row="row" let-rowIndex="rowIndex"
          *ngIf="column.name==='Actions'" ngx-datatable-cell-template>
          <button type="button" class="btn btn-outline-success">Success {{rowIndex}}</button>
        </ng-template>
        
        </ngx-datatable-column>     
      </ngx-datatable>

My .ts file look like this

columns = [{name: 'Actions', prop: 'Id' }...];

I have no idea what I'm doing wrong it this and I've seen a similar type of approach in other answers of similar type of question but none of them worked for me.

Kindly help.

I've found an alternative way to solve this problem and successfully implemented custom button in each row. So I thought to answer the question so that it could be helpful for anyone.

After the change, my HTML template look like this.

<ngx-datatable [rows]="rows" class="material" [loadingIndicator]="loadingIndicator" 
      [columnMode]="'force'"
      [selected]="selected" (activate)="onActivate($event, NewEventContent)"
        [headerHeight]="50" [footerHeight]="50" [rowHeight]="'auto'" [columns]="columns">

        <ngx-datatable-column *ngFor="let column of columns; 
        let i = index;" name="{{column.name}}"
          prop="{{column.prop}}">
        </ngx-datatable-column>

        <ngx-datatable-column name="Actions" sortable="false" prop="Id">
          <ng-template let-row="row" let-value="value" let-rowIndex="rowIndex"
           ngx-datatable-cell-template>
            <button class="btn btn-dark" (click)="onSelect(row)">
              Edit{{rowIndex + 1}}
            </button>
          </ng-template>
        </ngx-datatable-column>

      </ngx-datatable>

kindly pay attention to the ng-template part and onSelect(row) function. The above solution works very well in my case.

Original answer https://github.com/swimlane/ngx-datatable/issues/489#issuecomment-356765048

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM