Populating a PrimeNG data grid using an array

I am currently facing a challenge while working with Angular and PrimeNG. I am new to this technology stack and trying to populate a table, but I seem to be missing something as there are no errors in the console. Here is the code snippet that I am struggling with:

Within the component.ts file

...

columns: Column[] = [];
first = 0;
rows = 20;
tasks = [
    {
      id: 'Task 1',
      name: 'Start task 1',
      start: '2021-11-08',
      end: '2021-11-15',
      progress: 80,
      dependencies: 'Task 3, Task 4'
    },
    {
      id: 'Task 2',
      name: 'Start task 2',
      start: '2021-11-18',
      end: '2021-12-02',
      progress: 20,
      dependencies: 'Task 1'
    }
  ];

ngOnInit() {
    const columns = ['Id', 'Name', 'Start', 'End', 'Progress', 'Dependencies'];
}

public onPageChange(event: any): void {
    this.first = event.first;
    this.rows = event.rows;
  }

...

Within the component.html file

...

<p-table styleClass="sticky-table" #TasksTable [autoLayout]="true" [value]="tasks" selectionMode="single" [paginator]="true" [rows]="rows" [columns]="columns" [rowsPerPageOptions]="[10,20,50,100,200]" [first]="first" (onPage)="onPageChange($event)" [style]="{width:'100%'}"
            sortMode="multiple" [reorderableColumns]="true" [resizableColumns]="true">

            <ng-template pTemplate="header">
                <tr id="sticky-header">
                    <th class="flex-header" *ngFor="let col of columns" [pSortableColumn]="col.field" pResizableColumn pReorderableColumn>
                        <span>
                                <p-sortIcon [field]="col.field"></p-sortIcon>
                                {{col.header}}
                            </span>
                        <p-columnFilter [showIcon]="false" operator="or" [type]="!col.type ? 'text' : col.type" display="menu" [field]="col.field"></p-columnFilter>
                    </th>
                </tr>

            </ng-template>
            <ng-template pTemplate="body" let-tasks>
                <tr [pSelectableRow]="tasks">
                    <ng-container *ngFor="let col of columns">
                        <td>
                            <ng-container>
                                {{tasks[col.field]}}
                            </ng-container>
                        </td>
                    </ng-container>
                </tr>
            </ng-template>
            <ng-template pTemplate="paginatorright">
                <p-button icon="pi pi-refresh" (onClick)="refresh()" [pTooltip]="'common.refresh'" tooltipPosition="bottom" showDelay="300"></p-button>
            </ng-template>
</p-table>

...

I am aiming to populate the p-table with a hardcoded array of tasks for testing purposes, even though it may not be the best practice. Any assistance on resolving this issue would be greatly appreciated!

Thank you in advance!

Answer №1

It is necessary to update the columns variable in the following way:

cols: any[];

ngOnInit() {
  this.cols = [{
      field: 'id',
      header: 'Id'
    },
    {
      field: 'name',
      header: 'Name'
    },
    {
      field: 'start',
      header: 'Start'
    },
    {
      field: 'end',
      header: 'End'
    },
    {
      field: 'progress',
      header: 'Progress'
    },
    {
      field: 'dependencies',
      header: 'Dependencies'
    }
  ];
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the steps to locally test my custom UI library package built with tsdx in a React.js project

I am currently utilizing tsdx to develop a React UI library, and I am looking to test it within my Next.js project before officially publishing it to the npm package. Initially, I attempted using npm link, which worked initially. However, when I made ch ...

The Angular 2 router encounters issues with reusing a Component instance when navigating to the same route with varying parameters

Based on the information provided in Angular 2 documentation: The default behavior of the router is to reuse a component instance when navigating to the same component type without visiting a different component first. However, the parameters may change ...

What is the best way to execute a function that retrieves data from a MySQL query and then sends it back as a result in Express.js?

How can I refactor my code to efficiently call a function that returns the result of a MySQL query and send it back in an Express.js response? I am attempting to streamline my SQL queries by exporting them into individual functions to eliminate duplicatio ...

How do I insert items into an ES6 Map using a specific object key/value type in Typescript?

I'm looking to utilize Maps instead of object maps to define keys and values. However, it appears that Typescript doesn't fully support index types for ES6 Maps. Are there any alternatives or workarounds available? Furthermore, I want to enforce ...

Eliminate any instances of white space within a MatInput field in order to

I’m a newcomer to Angular2 and I’m attempting to eliminate whitespace for a required field. I’m unsure of how to trim the input field before the validator checks it. ...

Exploring nested objects within an instance

I'm facing an issue with accessing a variable object within my main object. I am able to access 'start', 'end', and 'category' without any problem, but I am unsure how to access the variable Object in my Angular web app d ...

"What are the necessary components to include in UserDTO and what is the reasoning behind their

Presenting the User entity: package com.yogesh.juvenilebackend.Model; import jakarta.annotation.Generated; import jakarta.persistence.*; import lombok.*; @Entity @Getter @Setter @NoArgsConstructor @AllArgsConstructor @RequiredArgsConstructor public class ...

How to use Angular 7 to send a JSON object as a parameter in an HTTP GET

I am attempting to send a JSON structure to a REST service from Angular in this manner: let test5var = { "test5var1": { "test5var2": "0317", "test5var3": "9556" }, ...

Dealing with TypeScript and the Mongoose loadClass problem

Working with Mongoose's class schemas has been very beneficial for me. Incorporating TypeScript into my Node project has enhanced the development process. I made sure to refer to Mongoose the Typescript way...? in order to ensure that my Model align ...

Unable to assign the selected attribute to a dynamically loaded Ion-select component in Ionic 2

I'm facing an issue with dynamically loading <ion-select> and setting default selection using the selected attribute. It doesn't seem to work as expected. Can anyone help me understand why? You can view the code on Plunker app/home.page.h ...

Using masonry-layout with Next Js leads to a ReferenceError stating that window is not defined

Implementing the masonry-layout library by David Desandro in my Next app has been a smooth process. You can find the link here. When I apply it, the masonry layout functions perfectly as intended. Here's how I'm incorporating it successfully: imp ...

Refreshing an Angular2 page is triggered by the update of a specific property

Just starting out with Angular2 and I'm puzzled as to why my page keeps refreshing when I try to set some properties from form data. Below is the component snippet: import { Component } from '@angular/core'; import { Credentials } from &ap ...

What methods are available for utilizing DOMStringMap within TypeScript?

Imagine I have this function: angular.forEach(myElements, function prepareElements(myEl: HTMLElement, index) { myEl.dataset.myProperty = "whatever"; }) However, I encounter an issue with error TS2094: The property 'myProperty' does not exi ...

Currency Digital Style

I'm working on converting the amount retrieved from my API into a format specific to the user's locale. When using the console: Intl.NumberFormat('en-IN').format(450000) // "4,50,000" But in an Angular 2 component template: {{ Intl. ...

What is the solution to the error message "Uncaught TypeError: createTheme_default is not a function"?

While working on my react application with vite, typescript, and mui, I encountered the following error: enter image description here This issue seems to be connected to material ui. Sometimes, deleting the 'deps' folder in '\node_mod ...

Challenges encountered when implementing a personal library in a separate project

After updating a library I own, I seem to have encountered an issue when trying to use it in another project. However, the reason for this problem eludes me. A multitude of error logs with a similar theme are appearing: ERROR in ./node_modules/@company-na ...

Appending an item to an array in TypeScript

I'm feeling lost. I'm attempting to insert new objects into an array in TypeScript, but I encountered an error. My interface includes a function, and I'm puzzled. Can anyone offer guidance? interface Videos{ title: string; descriptio ...

Obtain accurate dispatch type by leveraging ConnectedProps in conjunction with redux-thunk

Currently, I am utilizing Redux-Toolkit and Typescript. Specifically, my goal is to implement ConnectedProps as recommended in the redux documentation. However, it appears that the dispatch type is not recognized correctly (it is identified as a normal Dis ...

Experimenting with axios.create() instance using jest

I have attempted multiple solutions for this task. I am trying to test an axios instance API call without using any libraries like jest-axios-mock, moaxios, or msw. I believe it is possible, as I have successfully tested simple axios calls (axios.get / axi ...

JavaScript alert box

I'm fairly new to the world of web development, with knowledge in CSS & HTML and currently learning TypeScript. I'm attempting to create a message icon that opens and closes a notifications bar. Here's where I'm at so far: document.getE ...