Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit f0a3ade

Browse files
authored
Merge pull request #975 from ghiscoding/chore/update-tree-data-item
chore(demo): add code sample to update Tree Data item, closes #974
2 parents c4149c9 + 081092f commit f0a3ade

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/app/examples/grid-tree-data-parent-child.component.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ <h2>
6060
<span class="icon mdi mdi-plus color-white"></span>
6161
<span>Add New Item (in 1st group)</span>
6262
</button>
63+
<button (click)="updateFirstRow()" data-test="update-item-btn" class="btn btn-outline-secondary btn-sm">
64+
<span class="icon mdi mdi-pencil"></span>
65+
<span>Update 1st Row Item</span>
66+
</button>
6367
<button (click)="collapseAll()" data-test="collapse-all-btn" class="btn btn-outline-secondary btn-sm">
6468
<span class="icon mdi mdi-arrow-collapse"></span>
6569
<span>Collapse All</span>

src/app/examples/grid-tree-data-parent-child.component.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,29 @@ export class GridTreeDataParentChildComponent implements OnInit {
216216
}
217217
}
218218

219+
updateFirstRow() {
220+
// to update any of the grid rows, we CANNOT simply pass a new updated object
221+
// we MUST read it from the DataView first (that will include all mutated Tree Data props, like `__treeLevel`, `__parentId`, ...) and then update it
222+
const item = this.angularGrid.dataView.getItemById<any>(0);
223+
224+
// option 1
225+
/*
226+
// now that we have the extra Tree Data props, we can update any of the object properties (while keeping the Tree Data props)
227+
item.duration = `11 days`;
228+
item.percentComplete = 77;
229+
item.start = new Date();
230+
item.finish = new Date();
231+
item.effortDriven = false;
232+
233+
// finally we can now update the item which includes our updated props + the Tree Data props (`__treeLevel`, ...)
234+
this.angularGrid.gridService.updateItem(item);
235+
*/
236+
237+
// optiona 2 - alternative
238+
// we could also simply use the spread operator directly
239+
this.angularGrid.gridService.updateItem({ ...item, duration: `11 days`, percentComplete: 77, start: new Date(), finish: new Date(), effortDriven: false });
240+
}
241+
219242
collapseAll() {
220243
this.angularGrid.treeDataService.toggleTreeDataCollapse(true);
221244
}

test/cypress/e2e/example28.cy.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// <reference types="cypress" />
2+
import { changeTimezone, zeroPadding } from '../plugins/utilities';
23

34
function removeExtraSpaces(text) {
45
return `${text}`.replace(/\s+/g, ' ').trim();
@@ -208,6 +209,29 @@ describe('Example 28 - Tree Data (from a flat dataset with parentId references)'
208209
.contains('Task 500');
209210
});
210211

212+
it('should be able to update the 1st row item (Task 0)', () => {
213+
cy.get('[data-test=update-item-btn]')
214+
.contains('Update 1st Row Item')
215+
.click();
216+
217+
const now = new Date();
218+
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
219+
const today = changeTimezone(now, tz);
220+
221+
const currentDate = today.getDate();
222+
let currentMonth = today.getMonth() + 1; // month is zero based, let's add 1 to it
223+
if (currentMonth < 10) {
224+
currentMonth = `0${currentMonth}`; // add zero padding
225+
}
226+
const currentYear = today.getFullYear();
227+
228+
cy.get(`#grid28 [style="top:${GRID_ROW_HEIGHT * 0}px"] > .slick-cell:nth(0)`).should('contain', 'Task 0');
229+
cy.get(`#grid28 [style="top:${GRID_ROW_HEIGHT * 0}px"] > .slick-cell:nth(1)`).should('contain', '11 days');
230+
cy.get(`#grid28 [style="top:${GRID_ROW_HEIGHT * 0}px"] > .slick-cell:nth(2)`).should('contain', '77%');
231+
cy.get(`#grid28 [style="top:${GRID_ROW_HEIGHT * 0}px"] > .slick-cell:nth(3)`).should('contain', `${currentYear}-${zeroPadding(currentMonth)}-${zeroPadding(currentDate)}`);
232+
cy.get(`#grid28 [style="top:${GRID_ROW_HEIGHT * 0}px"] > .slick-cell:nth(4)`).should('contain', `${currentYear}-${zeroPadding(currentMonth)}-${zeroPadding(currentDate)}`);
233+
});
234+
211235
it('should collapse the Tree and not expect to see the newly inserted item (Task 500) because all child will be collapsed', () => {
212236
cy.get('[data-test=collapse-all-btn]')
213237
.contains('Collapse All')

0 commit comments

Comments
 (0)