具有左右值的可嵌套树结构句柄我已经完成了添加父节点、子节点和删除节点的功能。但是如何在使用 nestable.js 拖放移动节点后保存树值以免看到我的输出我想做什么....我想用左数据、右数据值进行管理我的数据库结构是请帮助了解如何管理拖放功能。 最佳答案 我试图对此发表评论,但网站告诉我我需要 50 声誉才能这样做,所以这可能会也可能不会回答您的问题,我必须等到回家时才能在数据库中摆弄类似的东西/Javascript 结构。我对 left/right_id 表等相当熟悉。我第一次看到它们是在 phpBB3.0 中,我花了很多时间阅读它们。可以帮助您加快查询的一件事是添加 parent_id 列,这样您就可以立即获得所有子注释,而无需在 SQL 语句中使用“left_id > 1 AND right_id 现在,对于您的 javascript 问题,当我回到家时,我将如何尝试解决此问题是声明一个 id 键数组 => 子值。所以,在你上面给出的截图示例中,你可能会得到这样的结果array( 2 => array( 5 => array( 3 ), 6 ), 3 => array( 8 ), 4)为了得到这个,你必须为列表中的每个成员遍历 DOM,使用它们的 id 和 array.push 函数根据需要添加值。一旦我可以到达我的家里,我有一个 PHP 服务器设置,我会尝试并提供更多帮助,但这应该让您了解如何开始它。您是否使用任何 Javascript 框架(例如 MooTools、jQuery)?编辑:所以,我已经研究了很长一段时间,我已经有了一个通用的东西。下面是直接负责循环遍历 DOM 树并分配 left/right_id 值的代码。根据自己的喜好编辑它应该相当容易。 我偏爱 MooTools,所以我使用 mootools 库编写了这个(它提供了一些非常好的 Element 功能,实际上给了我一个多级拖动和排序工具来测试它)。如果您更喜欢使用另一个库,那么切换代码来为其工作应该相当容易,但我不使用 jQuery,所以很遗憾我在这方面没有帮助 下载所有相关文件的链接:Herefunction calculate_ids(el,left_id, parent_id){ var el = $(el); //makes the element a MooTools Element var left_id = left_id || 1; //If left_id is given, we are using this function recursively var parent = parent_id || 0; //Gives us a quick method in order to figure out what level we are currently on var id_array = {}; /*ID array object will look like { 1:{ 'left_id':1, 'right_id':2, 'parent':0 }, 2:{ 'left_id':3, 'right_id':6, parent_id:0 }, 3:{ 'left_id':4, 'right_id':5 }, etc. } */ var els = el.getChildren('li'); //Get the immediate children els.each(function(child){ var id = child.get('id'); //The ID var descendants = child.getElements('li'); //Gets ALL most descendent children that match the format, these are ordered the way they are in the DOM var right_id = left_id + descendants.length * 2 + 1 id_array[id] = { 'left_id':left_id, 'right_id':right_id, 'parent':parent }; if(descendants.length > 0){ //There are child elements to this thing, recursion! id_array = Object.merge(id_array, calculate_ids( child.children[0], left_id + 1, id) ); } left_id = right_id + 1; //Increment the left_id counter }); return id_array; }已知的问题我发布的示例代码在第一次计算后无法正确计算左/右 ID,或者如果之前计算过它们向上/向下移动一个级别。我猜这与多级排序类如何移动项目有关。此代码旨在作为起点。我真的很忙(不幸的是),所以我将来可能会也可能不会解决这个问题,但无论哪种方式,这段代码都应该作为一个很好的起点。关于php - 具有左右值的可嵌套树,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21450712/
10-12 12:47
查看更多