2
votes

I'm replacing a custom made table with PrimeNG's turbotable. I have some buttons I need to insert into the table that call special javascript functions I've written, but I'm stuck on how to insert the element into the table.

It displays as [object HTMLInputElement] instead of as a button. I know the code that generates the button is good, because it worked great in the old table set up. I think the problem is the turbotable it turning it into text and I'm not sure how to make it stay HTML.

This is what's displayed.

enter image description here

Here's the turbotable

              <p-table [columns]="resultsCols" [value]="results">
                <ng-template pTemplate="caption">
                  Agencies  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Count {{results?.length}}
                </ng-template>
                <ng-template pTemplate="header">
                  <tr>
                    <th >Options</th>
                    <th [pSortableColumn]="'agency'" >Agency</th>
                    <th [pSortableColumn]="'department'" class="ui-p-2">Department</th>
                    <th [pSortableColumn]="'affiliateCount'" class="ui-p-4">Affiliate Count</th>
                    <th [pSortableColumn]="'basigdate'" class="ui-p-6">BA Sig Date</th>
                  </tr>
                </ng-template>
                <ng-template pTemplate="body" let-r>
                  <tr>
                    <td>  {{r.btnEdit}}</td>
                    <td >{{r.agency}}</td>
                    <td class="ui-p-2">{{r.department}}</td>
                    <td class="ui-p-4">{{r.affiliateCount}}</td>
                    <td class="ui-p-6">{{r.basigdate}}</td>
                  </tr>
                </ng-template>
              </p-table>

Here's how I generate the button and populate the results array

var result = JSON.parse(xmlhttp.responseText);

for (var i = 0; i < result.length; i++) {

  var inputEdit = document.createElement("input");
  inputEdit.type = "button";
  inputEdit.value = "Edit";
  inputEdit.classList.add("btn-link");
  inputEdit.onclick = (
    function(i) {
      return function() {
        comp.setEditMode(i);
      }
    }
  )(result[i].id);


  var a = new agencySearchResult();
  a.agency = result[i].name;
  a.affiliateCount = result[i].affiliateCount;
  a.basigdate = result[i].baSigDate;
  a.department = result[i].department;
  a.btnEdit = inputEdit;

  comp.results.push(a); 
}

Lastly, here's where the agencySearchResult is defined in typescript

export class agencySearchResult {
  constructor() {};

  agency: string;
  department: string;
  affiliateCount: string;
  basigdate: string;

  btnEdit: HTMLInputElement;
}

I think the problem is the {{r.btnEdit}} line in the HTML, but I included all the relevant stuff just in case.

So Internet, how do you insert a javascript generated HTML element into a turbotable ?

1
Why do you create your button in TS instead of in HTML ?Antikhippe
So I can pass it a complicated onclick function that I can't seem to do with a button generated on the html side of things.Joseph Doss

1 Answers

0
votes

You should better write your HTML button inside your HTML component file instead of generating it in your TS code.

So I would suggest you to replace

<td>  {{r.btnEdit}}</td>

and

var inputEdit = document.createElement("input");
  inputEdit.type = "button";
  inputEdit.value = "Edit";
  inputEdit.classList.add("btn-link");
  inputEdit.onclick = (
    function(i) {
      return function() {
        comp.setEditMode(i);
      }
    }
  )(result[i].id);

with something like

<td><input type="button" value="Edit" class="btn-link" (click)="edit(r.id)"/></td>

and

edit(rowId) {
    alert('Edition of row ' + rowId);
    // do whatever you need
}

which is much more concise and readable.

See this StackBlitz