irossimoline / angular4-material-table

Angular 4 table based on @angular/cdk table structure, to allow row insertion, edition, validation and deletion.
MIT License
55 stars 24 forks source link

Cannot overwrite row.currentData #42

Open filipkbt opened 4 years ago

filipkbt commented 4 years ago

Hi, I'm checking if any of properties in object contains '' (empty string) and i'm trying to override it with null value.

for (let key in row.currentData) {
  if ((!row.currentData[key]) || (row.currentData[key] && (row.currentData[key].toString().trim() === '') {
    row.currentData[key] = null;
  }
}

but after all, when I debug row.currentData selected property still has value: '' instead of null. What's wrong?

blavenie commented 4 years ago

To update data, you should use the validator (if any) :

// Partial update:
row.currentData = {key: value}; 
// Or equivalent
row.validator.patchValue({key: value}); 

// Full update (all keys): 
row.validator.setValue(data); 

This is because row.currentData can be a getter, when using reactive form validator. See https://github.com/irossimoline/angular4-material-table/blob/master/src/table-element-reactive-forms.ts#L13