Skip to content

Instantly share code, notes, and snippets.

@PavanKu
Created February 28, 2018 01:25
Show Gist options
  • Save PavanKu/0d9f7f49861b64b909ae8f064949b4db to your computer and use it in GitHub Desktop.
Save PavanKu/0d9f7f49861b64b909ae8f064949b4db to your computer and use it in GitHub Desktop.
Copy Paste from Excel to ngPrime Datatable
//Component.html
<p-column *ngFor="let col of cols; let i = index" [field]="col.field" [header]="col.header">
<ng-template pTemplate="body" let-car="rowData">
<span *ngIf="car.editable" [attr.data-index]="i">
<input type="text" (keyup)="populate($event, i)" >
</span>
<span *ngIf="!car.editable">{{car[col.field]}}</span>
</ng-template>
</p-column>
//Component code
populate(event, index) {
const inputElement = event.target;
const rowElement = inputElement.parentElement.parentElement.parentElement.parentElement;
const value = inputElement.value;
if(value.includes("\t")) {
const cellValues = value.split("\t").map(str => str.trim());
cellValues.forEach((val, i) => {
const element = rowElement.querySelector(`span[data-index="${index + i}"] input`);
if(element) {
element.value = val;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment