| Posted by: WUICTeam - Sunday, November 1, 2026 9:01:26 AM |
|---|
| [b]Update:[/b] the [c]changeTracking[/c] input of [c]wuic-data-source[/c] is now honoured: [c][changeTracking]="false"[/c] turns tracking off unless the table's extra properties set it. Fixed in [url=https://wuic-framework.com/downloads/release-notes#v1.8.2]WUIC Framework 1.8.2[/url]. [i]The original answer below describes versions before 1.8.2.[/i] [b]Question[/b] I change several rows of a [c]wuic-data-source[/c] in memory and want to send them to the server in a single request, or throw them all away. Calling [c]syncData[/c] once per row works, but is there a better way? And what happens if the grid reloads in the meantime? [b]Answer[/b] Use the change tracker: edit the rows locally, then call [c]batchSave()[/c] to save them all in one request, or [c]rollbackChanges()[/c] to restore the original values. [c]hasPendingChanges()[/c] and [c]getPendingChanges()[/c] tell you what is waiting. Change tracking is on by default. It is switched off only if the table's extra properties set [c]changeTracking[/c] to false; that value is read when the metadata loads and replaces whatever you passed to the [c]changeTracking[/c] input, so the input alone will not turn it off. [code] // edit in memory: every real difference is tracked per record const rec = datasource.getClientRecordByPKey(42); datasource.setClientRecordValue(rec, { status: 'closed' }); if (datasource.hasPendingChanges()) { const response = await datasource.batchSave(); if (response === null) { // nothing to send, cancelled by beforeSync$, or validation failed } } // or discard everything and restore the old values datasource.rollbackChanges(); [/code] Things to know: [list] [*][c]batchSave()[/c] resolves with [c]null[/c] when there is nothing to send, when a [c]beforeSync$[/c] handler cancels (the event has [c]operation === 'batch'[/c]) or when validation fails. Check for it before assuming the save happened. [*]If you set a field back to its original value, the change disappears from the tracker, so it is not sent. [*]Both methods accept a subset of changes ([c]getPendingChanges()[/c] returns the list), so you can save or discard only some records. [*]Careful with [c]fetchData()[/c]: with pending changes it asks the user to confirm, and if they accept, the changes are rolled back. Save before reloading, or the edits are lost. [/list] [url=https://wuic-framework.com/docs/datasource]Documentation: Datasource[/url] |