Skip to content

Commit

Permalink
fix(TreeData): addItem should keep current sorted column
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Jun 6, 2024
1 parent e03eee8 commit 7f1bbe0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ <h2>
<div class="col-md-12">
<button (click)="addNewRow()" data-test="add-item-btn" class="btn btn-primary btn-xs btn-icon">
<span class="icon mdi mdi-plus color-white"></span>
<span>Add New Item (in 1st group)</span>
<span>Add New Item to "Task 1" group</span>
</button>
<button (click)="updateFirstRow()" data-test="update-item-btn" class="btn btn-outline-secondary btn-xs btn-icon">
<span class="icon mdi mdi-pencil"></span>
Expand Down
15 changes: 5 additions & 10 deletions src/app/examples/grid-tree-data-parent-child.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,15 @@ export class GridTreeDataParentChildComponent implements OnInit {
}

/**
* A simple method to add a new item inside the first group that we find (it's random and is only for demo purposes).
* After adding the item, it will sort by parent/child recursively
* A simple method to add a new item inside the first group that has children which is "Task 1"
* After adding the item, it will resort by parent/child recursively but keep current sort column
*/
addNewRow() {
const newId = this.dataViewObj.getItemCount();
const parentPropName = 'parentId';
const treeLevelPropName = 'treeLevel'; // if undefined in your options, the default prop name is "__treeLevel"
const newTreeLevel = 1;
// find "Task 1" which has `id = 1`
const parentItemFound = this.dataViewObj.getItemById(1);

// find first parent object and add the new item as a child
const childItemFound = this.dataViewObj.getItems().find((item: any) => item[treeLevelPropName] === newTreeLevel);
const parentItemFound = this.dataViewObj.getItemByIdx(childItemFound[parentPropName]);

if (childItemFound && parentItemFound) {
if (parentItemFound?.__hasChildren) {
const newItem = {
id: newId,
parentId: parentItemFound.id,
Expand Down

0 comments on commit 7f1bbe0

Please sign in to comment.