0

I have an angular 2 project that uses primeNG's datatable. I have a list of person which populates the datatable. What I want is to set the minimum value of rows in the datatable. Let's say I want the table to have a minimum of 10 rows. So even if my person list is below 10 I want the table rows to be always set to 10 rows to which all of the remaining rows will be a just blank rows. How can I set that in PrimeNg datatable. Here's my plunkr http://plnkr.co/edit/SPwdr4nYoYJ0z4hIzkUK?p=preview.

 <p-dataTable [value]="persons" [editable]="true"  resizableColumns="true" reorderableColumns="true" scrollable="true" scrollHeight="80vh"> 
    <p-column field="firstName" header="First Name" [editable]="true"></p-column>
    <p-column field="lastName" [editable]="true" header="Last Name"></p-column>
    <p-column field="favoriteColor"  header="Favorite Color"></p-column>
    <p-column field="registered" header="Registered">
        <template let-person="rowData" pTemplate>
          <p-inputSwitch [(ngModel)]="person.registered"></p-inputSwitch>
      </template>
    </p-column>
</p-dataTable>
ilovejavaAJ
  • 1,631
  • 4
  • 26
  • 35

1 Answers1

1

Use the below code ,

if(this.persons.length<10){
        let temp= {"firstName": "","lastName":"","registered":'',"favoriteColor": ""};
        for(let i=this.persons.length;i<10;i++){
          this.persons.push(temp);
        }console.log(this.persons);
      }

The output look likes

enter image description here

Updated Plunker

Aravind
  • 40,391
  • 16
  • 91
  • 110
  • Another question. My person array length is changeable. In my application there's an option in which I can add a person and delete a person. I edited your plunker but I can't add a person in the list. http://plnkr.co/edit/djOPDA?p=preview – ilovejavaAJ May 28 '17 at 01:32
  • In your plunk u there was no button for adding a new item – Aravind May 28 '17 at 02:18
  • I didnt get your problem. elaborate – Aravind May 28 '17 at 12:07
  • Just a wrong call of method but I already updated the plunkr. The added person object to the list is inserted to the last index. How can I add the new object after the index of the last object with value not at the end of the array. This also goes with the delete person method. Calling pop method deletes the last object of the array but what I want is to pop the last object with a value. – ilovejavaAJ May 28 '17 at 12:39
  • you can still push an object to array. it adds to its last index. – Aravind May 28 '17 at 13:08
  • is there a way to set the minimum number of rows without adding null objects to the list? – ilovejavaAJ Jun 09 '17 at 14:29
  • How can you do that!?? If u can do it edit my post please – Aravind Jun 09 '17 at 14:36