etrepat/baum laravel包中缺少makeNthChildOf()方法

etrepat/baum laravel包中缺少makeNthChildOf()方法

问题描述:

我正在使用 laravel包以及jstree jQuery插件来管理类别。etrepat/baum laravel包中缺少makeNthChildOf()方法

在jstree中有一个move_node.jstree用于移动节点的方法。它具有有用的属性来获取有关移动动作的信息,如parentold _parent,...和position,它返回父节点中移动的子节点的新位置。

另一方面也有移动节点的方法(here)。但是没有像makeNthChildOf()这样的方法可用于将子节点放置在父节点的特定位置。像:

$node->makeNthChildOf(newPosition , $parent) 
在客户端

现在移动我写了这个节点:

$treeview.on('move_node.jstree', function (e, data) { 
      $.post('/move', { 
       'CategoryID': data.node.id, 
       'newParent': data.parent, 
       'oldParent': data.old_parent 
      }, function (res) { 
       data.instance.open_node(data.parent); 
      }, 'json'); 
     }); 

而在laravel:

public function move (Request $request) 
    { 
     $data = $request->only('newParent', 'oldParent', 'CategoryID'); 

     $currentNode = Category::find($data['CategoryID']); 
     if ($data['newParent'] == '#') { 
      $currentNode->makeRoot(); 
     } else { 
      $currentNode->makeChildOf($data['newParent']); 
     } 

     return ['success' => true]; 
    } 

但上面的代码不能这样做,我想和刚可以将父节点从父节点移动到另一个父节点。

我想知道是否有任何替代方法来做到这一功能?

好吧,我相信我找到了一种方法来实现这一点。我只是把它很快在一起,所以比较遗憾的是丑陋的代码:

首先,我用ajax,所以我建立了

formData = "node_id=" + data.node.id + "&name=" + data.node.text + "&_token=" + $("input[name=_token]").val() + "&parent=" + data.parent + "&position=" + data.position + "&old_position=" + data.old_position + "&old_parent=" + data.old_parent; 

接下来,我们创建了一个快速的辅助功能的节点信息的表单数据来确定我们当前节点在什么位置。它将循环遍历父节点的所有后继者并从字面上计数它们。

private function findPosition($decendants, $position) { 
    $i = 0; 
    foreach ($decendants as $decendant) { 
     if ($i == $position) { 
      return $decendant; 
     } 
     $i++; 
    } 
} 

,然后我们可以移动节点到它的正确位置:

//We first need to account for moving within same node, but below it's current position. Without this the node will be moved 1 position off. 
    if ($parent == $old_parent) { 
     if ($position > $old_position) { 
      $position = $position +1; 
     } 
    } 

    $parent = BaumTest::find($parent); 
    $decendants = $parent->getImmediateDescendants(); 
    $moveTo = $this->findPosition($decendants, $position); 
    $node = BaumTest::find($node_id); 

//If the parent we are moving to doesn't have any decendents, just move it now 
    if ($parent->isLeaf()) { 
     $node->makeChildOf($parent); 
     return array("error"=>false, "successMessage"=>"Sucessfully Moved!"); 
    } 

    //This takes care of it we are moving it into the last position 
    if (is_null($moveTo)) { 
     $node->makeLastChildOf($parent); 
     return array("error"=>false, "successMessage"=>"Sucessfully Moved!"); 
    } 

$node->moveToLeftOf($moveTo);