Zicrael / ngx-tree-dnd

Angular 7 support, data sortable draggable smart tree.
https://ngx-tree-dnd.stackblitz.io
MIT License
39 stars 11 forks source link

checkTreeLength bug #39

Closed luzel closed 5 years ago

luzel commented 5 years ago

For empty treeStorage there is TypeError which breaks tree render.

TypeError: Cannot read property 'options' of undefined at NgxTreeService.push../node_modules/ngx-tree-dnd/fesm5/ngx-tree-dnd.js.NgxTreeService.checkTreeLength (ngx-tree-dnd.js:840) at Observable._subscribe (ngx-tree-dnd.js:75) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:42) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:28) at NgxTreeParentComponent.push../node_modules/ngx-tree-dnd/fesm5/ngx-tree-dnd.js.NgxTreeParentComponent.getTreeData (ngx-tree-dnd.js:1057) at NgxTreeParentComponent.set [as treeData] (ngx-tree-dnd.js:941) at updateProp (core.js:22207) at checkAndUpdateDirectiveInline (core.js:21962) at checkAndUpdateNodeInline (core.js:23265) at checkAndUpdateNode (core.js:23227)

NgxTreeService.checkTreeLength method has wrong IF statement. For empty treeStorage (0 is smaller then 2) code tries to read first array item which is non existing by index of zero.

if (this.treeStorage.length < 2) { this.treeStorage[0].options.showDeleteButton = false; } else { try { for (var _b = __values(this.treeStorage), _c = _b.next(); !_c.done; _c = _b.next()) { var el = _c.value; if (el && el.options) { el.options.showDeleteButton = true; } } } catch (e_7_1) { e_7 = { error: e_7_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_7) throw e_7.error; } } }

Possible fix could be?

if (this.treeStorage.length == 0) { // do nothing } else { // no need to remove delete button // if (this.treeStorage.length == 1) { // this.treeStorage[0].options.showDeleteButton = false; // } ...

Package version "ngx-tree-dnd": "^2.5.0"

Zicrael commented 5 years ago

@luzel thank you for finding error, sorry have a lot of work, so fix errors only when I have time for it. Let me fix it.

Zicrael commented 5 years ago

@luzel fixed in 'ngx-tree-dnd' version: '2.6.0', Thank you for reporting issue.

luzel commented 5 years ago

Hi @Zicrael no problem. Just pulled new version and empty Tree works as expected.

Thank you.