I am currently using the beginAddRowByIndex function with batchEdit, and want to be able to supply an object that contains default values for some of the columns. Looking through the grid base component, as well as the crud service, I didn't see any great way of doing it. Instead I tried listening to the rowEditEnter EventEmitter, and updated the e.rowData object to my liking. I then called e.owner.detectChanges(). This updated the new row to contain my default values, however, when the row was committed, it didn't contain my default values.
I also tried the following to options in rowEditEnter, to no avail:
Is there a different way that I can provide these values to the user, and have them persist (unless changed by the user) when the row is committed?
I also reviewed this support ticket from a while back, because I will need to set multiple column values when a column changes, but the provided solution doesn't work for rows that are added via beginAddRowByIndex. How can the solution in the support ticket be modified to work for new rows as well?
Hello,
Thank you for posting on our forums.
You can't assign a new object to rowData, but you can assign values to each property the object have e.g:
Tihomir Tonev said:
This solution may work for normal editing mode, but it does not work for batch editing mode. Through trial and error I was finally able to supply default values, and have them persist when the new row is committed, by doing the following:
public onRowEditEnter(e: IGridEditEventArgs): void { // check to see if we have already set the dataType default value if (e.isAddRow && !e.rowData.dataType) { const transaction: any = { id: e.rowID, type: 'update', newValue: { dataType: PropertyDataType.Text } }; e.owner.transactions.add(transaction, e.rowData); e.rowData.dataType = PropertyDataType.Text; e.owner.crudService.updateRowEditData(e.owner.crudService.row, e.rowData); e.owner.cdr.detectChanges(); } }
It's not the prettiest solution by any means. If anyone has a better solution, please let me know.
As for the requirement to set multiple column values when a column changes, I was able to achieve it using the following:
public onCellEditDone(e: IGridEditEventArgs): void { // Make the description match the entityId if the description is empty, or equals the old entityId if (e.column.field === 'entityId' && (e.oldValue === e.rowData.description || !e.rowData.description)) { const transaction: any = { id: e.rowID, type: 'update', newValue: { description: e.newValue } }; e.owner.transactions.add(transaction, e.rowData); e.rowData.description = e.newValue; e.owner.crudService.updateRowEditData(e.owner.crudService.row, e.rowData); e.owner.cdr.detectChanges(); } }
Hi Michael, thanks for sharing the solution and it helped me.
One questions to you is that do we need to add transaction if it's for normal editing mode for single row adding?
Thanks