Insert first child

I want to create a new child as first item below a parent. For this I can use neither insertNewChild nor insertNewNext.

The only workaround that comes to mind is creating the child and then moving it to the top with moveItem. But the moveItem function is also quite stupid.

How about a method where I can pass an index of the position where my new/moved item should go?

Hello

The only workaround that comes to mind is creating the child and then moving it to the top with moveItem.
It is right way.

But the moveItem function is also quite stupid.
Why do you think so?

The only workaround that comes to mind is creating the child and then moving it to the top with moveItem.
It is right way.

It is not.
insertNewChild inserts it as last child and insertNewNext inserts as next sibling after a specified item.

But the moveItem function is also quite stupid.
Why do you think so?

Because move “up” doesn’t always move up. It could also move the item to a totally different level if the item before it happens to have children and is open. You have to use “up_strict” if you want to do that. And the modes are not even documented (listed, but not explained) in the API on docs.dhtmlx.com/doku.php?id=dhtm … t_moveitem . You only find it if you read through all pages but not if using the API as a reference.

The easiest would be to call moveItem with the parent id and an index of the new position within the children of the parent. But you preferred to invent 8 different modes which work not intuitively at all.

insertNewChild inserts it as last child and insertNewNext inserts as next sibling after a specified item.

Yes, and then to call moveItem() - that is because i unswered “It is the right way” - to do all the steps.

About documentation: we will try to add more useful description to this method.

May be you can try the next:

[code]var parentId = “…”;
var newId=(new Date()).valueOf();
var id = tree.getItemIdByIndex(parentId ",0);
if(id){
tree.insertNewNext(id,newId,“Item text”);
tree.moveItem(newId,“up_strict”);
}
else{
tree.insertNewChild(parentId ,newId,“Item text”);
}
If there are any children use insertNewNext() and replace items.
If there is no children - just insertNewChild()