简体   繁体   English

PrimeNG dataTable不是“重新加载”表

[英]PrimeNG dataTable not 'reload' table

I have [value]="rows[indexString]" for dataTable in template. 我的模板中的dataTable有[value]="rows[indexString]" In 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

}

Basically am following tutorial from here 基本上是从这里开始教程

Only the first row rendered in table, in template {{rows | json}} 表格{{rows | json}} {{rows | json}} , everything is there: {{rows | json}} ,一切都在这里:

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

In your case, this.rows is an object , not an array . 就您而言, this.rows是一个object ,而不是一个array

I think the problem might be the way you're copying this.rows . 我认为问题可能出在您复制this.rows You are using spread operator to create a new object from this.rows copy. 您正在使用传播运算符从this.rows副本创建新对象。 What you want is an array. 您想要的是一个数组。

Try this instead: 尝试以下方法:

let rows = [...this.rows]

EDIT: I'll complete my answer. 编辑:我会完成我的答案。 The problem is reference to your object is lost when copying rows ans reassigning them. 问题是在复制行并重新分配它们时丢失了对您对象的引用。 Then observation of changes can't occur. 这样就不会观察到变化。

What you need to copy is the array you are currently working with. 您需要复制的是当前正在使用的阵列。 Not the whole object. 不是整个对象。

  addNewRow(key: string) {

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

    let rows = [];

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

    this.rows[key] = rows;

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM