Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function for getting parent based on path. #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ interface ICursorPosition<TDataType> {
| insert(position: ICursorPosition, nodeModel: ISlTreeNodeModel) | Insert nodes by the current cursor position. |
| remove(paths: number[][]) | Remove nodes by paths. For example `.remove([[0,1], [0,2]])`
| getFirstNode(): ISlTreeNode | Get the first node in the tree |
| getLastNode(): ISlTreeNode | Get the last node in the tree
| getNextNode(path: number[], filter?: (node: ISlTreeNode<TDataType>) => boolean): ISlTreeNode<TDataType>; | Get the next node. You can skip the next nodes by using `filter`
| getPrevNode(path: number[], filter?: (node: ISlTreeNode<TDataType>) => boolean): ISlTreeNode<TDataType>; | Get the previous node. You can skip the previous nodes by using `filter`
| getLastNode(): ISlTreeNode | Get the last node in the tree |
| getNextNode(path: number[], filter?: (node: ISlTreeNode<TDataType>) => boolean): ISlTreeNode<TDataType>; | Get the next node. You can skip the next nodes by using `filter` |
| getPrevNode(path: number[], filter?: (node: ISlTreeNode<TDataType>) => boolean): ISlTreeNode<TDataType>; | Get the previous node. You can skip the previous nodes by using `filter` |
| getParentNode(path: number[]): ISlTreeNode<TDataType>; | Get the parent node for given path |

# Slots

Expand Down
1 change: 1 addition & 0 deletions src/sl-vue-tree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class SlVueTree<TDataType> extends Vue {
getLastNode(): ISlTreeNode<TDataType>;
getNextNode(path: number[], filter?: (node: ISlTreeNode<TDataType>) => boolean): ISlTreeNode<TDataType>;
getPrevNode(path: number[], filter?: (node: ISlTreeNode<TDataType>) => boolean): ISlTreeNode<TDataType>;
getParentNode(path: number[]): ISlTreeNode<TDataType>;
updateNode(path: number[], patch: Partial<ISlTreeNodeModel<TDataType>>): void;
getSelected(): ISlTreeNode<TDataType>[];
traverse(cb: (node: ISlTreeNode<TDataType>, nodeModel: ISlTreeNodeModel<TDataType>, siblings: ISlTreeNodeModel<TDataType>[]) => boolean | void, nodeModels?: ISlTreeNodeModel<TDataType>[], parentPath?: number[]): ISlTreeNode<TDataType>[] | boolean;
Expand Down
10 changes: 10 additions & 0 deletions src/sl-vue-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,16 @@ export default {
return !shouldStop ? nodes : false;
},

getParentNode(path) {
if (!path) return null;

if(path.length <= 1) return null;

var parentPath = path.slice(0, -1)

return this.getNode(parentPath);
},

traverseModels(cb, nodeModels) {
let i = nodeModels.length;
while (i--) {
Expand Down