Refresh PrimeNG dataTable without reloading the table

Currently, I am implementing the functionality of adding new rows to a dataTable in my template. Here is the code snippet from the component:


rows: any = {}
newrow: any = {}

addNewRow(key: string) {

  let rows = {...this.rows}
  let newrow = {_key: Math.floor(Math.random() * 10000), title: this.newrow.title}
  
  if (rows[key]) {
    rows[key].push(newrow)
  } else {
    rows[key] = newrow
  }
  
  this.rows = rows

}

I found a useful tutorial on PrimeFaces' website, which has been quite helpful in guiding me through this process.

The issue I'm facing is that only the first row is being rendered in the table, even though when I check the values using {{rows | json}} in the template, all the data seems to be present:


{
  "210386": [
    {
      "_key": 9173,
      "title": "Row one"
    },
    {
      "_key": 6201,
      "title": "Row Two"
    }
  ]
}

Answer №1

In your situation, the variable this.rows is actually an object and not an array.

The issue may stem from how you're attempting to duplicate the contents of this.rows. By using the spread operator, you are creating a new object instead of an array.

To resolve this, consider utilizing the following approach:

let rows = [...this.rows]

UPDATE: I will provide further clarification in my response. The main problem lies in the fact that when you copy rows and reassign them, the reference to your object is lost. This prevents the ability to observe any changes made.

What you should be copying is the specific array that you are currently manipulating, rather than the entire object.

  addNewRow(key: string) {

    let newRow = {_key: Math.floor(Math.random() * 10000), title: this.newrow.title}

    let rows = [];

    if (this.rows.hasOwnProperty(key)) { // check if key already exists
      rows = [...this.rows[key], newRow];
    } else {
      rows = [newRow]
    }

    this.rows[key] = rows;

  }

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

Encountered an error with create-react-app and MaterialUI: Invalid hook call issue

I am encountering an issue while trying to set up Create-react-app with Material UI. The error message I receive pertains to Hooks. Could there be something else that I am missing? This is the specific error message being displayed: Error: Invalid hook ...

React component's state is not being correctly refreshed on key events

Currently facing an issue that's puzzling me. While creating a Wordle replica, I've noticed that the state updates correctly on some occasions but not on others. I'm struggling to pinpoint the exact reason behind this discrepancy. Included ...

Having trouble accessing a downloaded image saved in local files from Amazon S3 using the AWS SDK in Node.js

I am currently using the node.js aws-sdk package to download files from s3 storage. However, when I download a jpeg image and save it as a local file, I am unable to view it. Is this the correct method for downloading jpeg images? public async downloadFi ...

Combining b2c and b2e integration through Azure Active Directory

Is there an efficient method for combining Azure AD b2c and b2e within an Angular application? Can we provide two separate buttons on the login page and redirect users based on their selection? Alternatively, could social login be utilized, keeping in mi ...

What is the process for type checking a Date in TypeScript?

The control flow based type analysis in TypeScript 3.4.5 does not seem to be satisfied by instanceof Date === true. Even after confirming that the value is a Date, TypeScript complains that the returned value is not a Date. async function testFunction(): ...

Find any consecutive lowercase or uppercase letter and include one more

I have a task in Javascript that I need help with. The goal is to insert a special character between a lowercase and uppercase letter when they are matched together. For example: myHouse => my_House aRandomString => a_Random_String And so on... T ...

Having difficulty invoking the forEach function on an Array in TypeScript

I'm currently working with a class that contains an array of objects. export interface Filter { sf?: Array<{ key: string, value: string }>; } In my attempt to loop through and print out the value of each object inside the array using the forE ...

When selecting the "Open Link in New Tab" option in Chrome, the Angular app's routing will automatically redirect to the login page

I am facing a peculiar issue in my Angular 2 application that I need help troubleshooting. Currently, the routing within my app functions as intended when I click on links to navigate between different components. Here is an example of how the routing path ...

Refresh child route independently without relying on data from parent route

I am currently facing an issue with my component 'panel' that has two child routes: dashboard and holidays. Both of these child routes are supposed to receive data from the parent component service in their ngOnInit. While everything is working ...

Displaying text on a ribbon in Excel using Office JavaScript

Currently, I am working on an excel-addin project utilizing Angular and the Office JS API. My goal is to showcase a text message in an angular toast, while also displaying regular text on the right side of the ribbon/menu bar in Excel. My desired function ...

typescript decorator implemented on a class that is nested within another class

Is it possible to decorate a nested property within a class? Let's explore with an example: function log(name: string = 'DecoratedProp') { return function logHandler(target: any, field: any) { // get the key ...

Creating custom components in AngularJS 2 allows you to define methods unique to each component. Want to learn

I created my component (A) by combining multiple html elements, but I have two questions. How do I define methods (such as get, etc.) on my component? I have tried @Output, @ViewChild, etc. but they are not working. I am looking for an alternative way ...

Creating TypeScript atom packages

Has anyone successfully implemented this before? I couldn't locate any assistance for it. If someone could provide references to documentation or existing code, that would be great. I know that Atom runs on Node and that there is a TypeScript compil ...

"Encountered a problem while attempting to download the .xlsx file through http.get in an angular application interfacing

Attempting to download a .xlsx file using Angular 7 and web API in C#, encountering the following error: https://i.sstatic.net/7pwDl.png The code snippet from my service.ts is provided below: public exportExcelFile(matchedRows: string, reportInfoId: num ...

What is the correct way to utilize the mapState function within a TypeScript environment while implementing Vuex?

In my Vue.js project integrated with Vuex, I am using Typescript syntax. While trying to use the mapState method as computed in my .ts file, I encountered a syntax error. Currently, I am following the suggested syntax for computed function in the documenta ...

What is the best way to search for specific data in a MongoDB database?

When using angular2 in conjunction with meteor, the data contains the following information: { "_id" : "DxEraKtfYavoukdCK", "name" : "Aaron", "capacity" : 20, "available_capacity" : 15, "location" : "1" } { "_id" : "yMhEggaGmS7iio9P4", "name" : "Benard ...

"Learn how to dynamically update a value based on the selected option in a drop-down menu in Angular

Hello everyone. I am working on an angular project that includes a product page. Some products on this page can have multiple types, like so: Product ID-1 Type ID-1 Price-$10 Product ID-1 Type ID-2 Price-$15 Product ID-1 Type ID-3 Price-$25 In this sce ...

Angular Github Deployment Issue: Page malfunctioning as a result of strict-origin-when-cross-origin restriction

I am currently learning Angular and attempting to deploy it on Github Pages. However, I have encountered an issue where the app is not functioning properly. After inspecting the page, I discovered a CORS origin error when trying to access certain resource ...

Mastering the art of leveraging generics in conjunction with ngrx actions

Currently, I am in the process of developing an Angular 7 application that utilizes the NGRX store. In our application, we have numerous entities, each with its own dedicated list view. I decided to explore using generics with the NGRX store to avoid writi ...

What steps should be taken to trigger an API call once 3 characters have been entered into a field

In my current project, I am dealing with a parent and child component setup. The child component includes an input field that will emit the user-entered value to the parent component using the following syntax: <parent-component (sendInputValue)="g ...