- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 jsTree 查看作为嵌套集(左、右、级别等)存储在 mySQL 数据库中的分层数据。这工作正常,但我需要允许用户通过上传 CSV 文件来导入数据。当他们这样做时,表格中的任何现有数据都将被删除,因此我不必担心更新左/右字段。
他们将要上传的数据将采用以下格式:
"Code","Title"
"100","Unit 100"
"200","Unit 200"
"101","Task 101: This is a task"
"102","Task 102: Another task"
"201","Task 201: Yet another"
"300","Unit 300"
"301","Task 301: Another one"
一切都将是一级节点的主要“组”的子节点。所有可被 100 整除的“代码”(即 100、200、300)都将是级别 2(父节点..“组”的子节点)。所有其他的将是它们各自父节点的 3 级(子)节点(即 101 和 102 是 100 的子节点,201 是 200 的子节点,等等)
mySQL 中的结果表应该如下所示:
id parent_id position left right level title
1 0 0 1 18 0 ROOT
2 1 0 2 17 1 Group
3 2 0 3 8 2 Unit 100
4 2 1 9 12 2 Unit 200
5 3 0 4 5 3 Task 101: This is a task
6 3 1 6 7 3 Task 102: Another task
7 4 0 10 11 3 Task 201: Yet another
8 2 2 13 16 2 Unit 300
9 8 0 14 15 3 Task 301: Another one
这棵树看起来像这样:
我的问题是:使用 PHP,完成此任务的最佳方法是什么?我已经有了代码,可以提取上传的 CSV 文件中包含的数据并将其存储在数组中,但我不确定将其转换为嵌套集的逻辑应该是什么样子。
现在,数据存储在名为 $data 的二维数组中(格式为 $data[$col][$row]):
$data[0][0] = "Code";
$data[0][1] = "100";
$data[0][2] = "200";
$data[0][3] = "101";
$data[0][4] = "102";
$data[0][5] = "201";
$data[0][6] = "300";
$data[0][7] = "301";
$data[1][0] = "Title";
$data[1][1] = "Unit 100";
$data[1][2] = "Unit 200";
$data[1][3] = "Task 101: This is a task";
$data[1][4] = "Task 102: Another task";
$data[1][5] = "Task 201: Yet another";
$data[1][6] = "Unit 300";
$data[1][7] = "Task 301: Another one";
Array ( [0] => Array ( [0] => Code [1] => 100 [2] => 200 [3] => 101 [4] => 102 [5] => 201 [6] => 300 [7] => 301 ) [1] => Array ( [0] => Title [1] => Unit 100 [2] => Unit 200 [3] => Task 101: This is a task [4] => Task 102: Another task [5] => Task 201: Yet another [6] => Unit 300 [7] => Task 301: Another one ) )
如有任何帮助,我们将不胜感激。我现在可以正确计算 parent_id、位置和级别...我只需要弄清楚左/右部分。这是我目前正在使用的代码(感谢让我开始使用 Matteo):
$rows = array();
// insert ROOT row
$rows[] = array(
'id' => 1,
'parent_id' => 0,
'position' => 0,
'left' => 1,
'right' => 10000, // just a guess, will need updated later
'level' => 0,
'title' => 'ROOT',
);
echo "<br>";
print_r($rows[0]);
// insert group row
$rows[] = array(
'id' => 2,
'parent_id' => 1,
'position' => 0,
'left' => 2,
'right' => 9999, // just a guess, will need updated later
'level' => 1,
'title' => 'Group',
);
echo "<br>";
print_r($rows[1]);
// next ID to be used
$id = 3;
// keep track of code => ID correspondence
$map = array();
// parse data
for ($i = 1, $c = count($data[0]); $i < $c; ++$i) {
// save ID in the map
$map[$data[0][$i]] = $id;
// initialize the current row
$row = array(
'id' => $id,
'parent_id' => 1,
'position' => 0,
'left' => 0,
'right' => 0,
'level' => 1,
'title' => $data[1][$i],
);
// if the code is multiple of 100
if ($data[0][$i] % 100 == 0) {
$row['parent_id'] = 2;
$row['level'] = 2;
$row['position'] = (floor($data[0][$i] / 100)) - 1;
} else {
// get parent id from map
$row['parent_id'] = $map[floor($data[0][$i] / 100) * 100];
$row['level'] = 3;
$row['position'] = $data[0][$i] % 100;
}
// add the row
$rows[] = $row;
++$id;
echo "<br>";
print_r($row);
}
最佳答案
给定你的 $data
数组,你可以像这样解析它:
// this will contain all the rows to be inserted in your DB
$rows = array();
// insert ROOT row
$rows[0] = array(
'id' => 1,
'parent_id' => 0,
'position' => 0,
'level' => 0,
'left' => 1,
'right' => 10000,
'title' => 'ROOT',
);
// insert group row
$rows[1] = array(
'id' => 2,
'parent_id' => 1,
'position' => 0,
'level' => 1,
'left' => 2,
'right' => 9999,
'title' => 'Group',
);
// keep trace of code => ID correspondence
$map = array();
// next ID to be used
$id = 3;
// keep father => sons relationship
$tree = array();
// keep trace of code => row index correspondence
$indexes = array();
// next row index
$index = 2;
// parse your data
for ($i = 1, $c = count($data[0]); $i < $c; ++$i) {
// current code
$code = $data[0][$i];
// save ID in the map
$map[$code] = $id;
// update the indexes map
$indexes[$code] = $index;
// prepare the current row
$row = array(
'id' => $id,
'title' => $data[1][$i],
)
// get the value of code mod 100
$mod = $code % 100;
// if the code is multiple of 100
if ($mod == 0) {
// the parent_id is 2
$row['parent_id'] = 2;
// it is level two
$row['level'] = 2;
// compute position
$row['position'] = floor($code / 100) - 1;
}
else {
// get the parent code
$parent = floor($code / 100) * 100;
// get parent id from map using parent code
$row['parent_id'] = $map[$parent];
// it is level three
$row['level'] = 3;
// save position
$row['position'] = $mod;
// save in relationship tree
$tree[$parent][] = $code;
}
// add the row
$rows[$index] = $row;
// prepare next id
++$id;
// update row index
++$index;
}
// sort the relationship tree base on the parent code (key)
ksort($tree, SORT_NUMERIC);
// next left value
$left = 3;
// now, using the relationship tree, assign left and right
foreach ($tree as $parent => $sons) {
// calculate parent left value
$parentLeft = $left;
// prepare next left value
++$left;
// to be sure that the sons are in order
sort($sons, SORT_NUMERIC);
// assign values to sons
foreach ($sons as $son) {
// index in the rows array
$index = $indexes[$son];
// set left value
$rows[$index]['left'] = $left;
// set right value
$rows[$index]['right'] = $left + 1;
// increment left value
$left += 2;
}
// calculate parent right value
$parentRight = $left;
// prepare next left value
++$left;
// index of parent in the rows array
$index = $indexes[$parent];
// set the values
$rows[$index]['left'] = $parentLeft;
$rows[$index]['right'] = $parentRight;
}
// update group row right value
$rows[1]['right'] = $left;
// prepare next left value
++$left;
// update root row right value
$rows[0]['right'] = $left;
此时,您可以一次插入所有行。
编辑:现在脚本应该正确处理所有必需的值。
关于PHP/MySQL : Import data and store in hierarchical nested set for use with jsTree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11797288/
我试图在不重新加载或刷新页面的情况下重新加载 jstree,重新加载应该发生在按钮的 onclick 上,因此当单击按钮时我调用 $("#tree-container").jstree('destro
在我的页面上,我有一个按钮,单击该按钮会刷新该页面上找到的 jstree。 现在,当 jstree 加载时,将从 2D 数组中读取数据,并根据该数组中找到的数据更改节点图标。我需要一个函数来刷新或重
我正在尝试使用 jstree 并在打开页面时打开一个节点及其所有父节点。这是我用来测试的html代码。 TEST1
有没有人尝试过使用类型插件更改 jstree 特定节点的颜色?而且我还想避免该节点上的点击事件。 我需要这样做以防止用户选择相同的节点。 最佳答案 关于如何更改任何被点击节点的颜色的示例: $(fun
我将 jsTree 与复选框插件一起使用。我将级联设置为“向下”。这很好用,除了当我想加载包含已选中和未选中节点的数据时,级联会覆盖节点的“状态”设置。例如。 var data = [ {"i
为了在jsTree中存储任意数据节点,我将自定义属性(例如 node-data)传递给 create_node方法。但我需要在单个属性中存储多条数据。 (例如姓名、年龄等)哪种格式最适合节点数据属性的
有没有办法以编程方式关闭 jsTree 中的上下文菜单?我实现了一种使用 show_contextmenu 在悬停时打开上下文菜单的方法,但我需要在鼠标离开时关闭它 最佳答案 好的,通过查看代码我找到
当还选择了父节点时,我在选择子节点时遇到了困难,还想打开子文件夹并选择所有子节点(不确定我是否说清楚)所以我知道如何通过以下方式获取所有子节点: selected_nodes = $("#demo")
我需要显示一个 JSTree 但不能展开和折叠。有什么办法可以实现吗? 在下面的链接中提到禁用展开和折叠在 jstree 中不可用,这似乎是一个错误。 https://groups.google.co
如何在 jsTree 1.0 中禁用移动,但不禁用 dnd 插件? 谢谢。 最佳答案 我找到了解决方案。 $("#tree").jstree({ //... "crrm" : {
我的 jstree 配置如下: var $proceduresTree = $('#procedures-tree'); $proceduresTree.jstree({ 'core' : {
我使用插件 Jstree 绘制文件夹和文件树。 我想获取顶部的文件夹列表,然后获取文件列表(文件夹和文件列表必须按字母顺序排序)。 这是我的树初始化函数: $('#jstree_demo_div').
这是我处理这个问题的最后手段。 问题是我无法从下面的示例数据中获取具体值。 我很着急,如果您能回答这个问题,我将不胜感激。 这是我的代码。 $("#organize").jstree( {
在我的网站中,我有一个下拉选择选项,列出了 MySQL 中可用的数据库。在每个数据库中,我都有一个名为树的表。每当选择选项更改为新数据库或发生页面重新加载时,我都会将数据库名称发布到 php 文件中。
我正在使用 jstree 并且 select_node.jstree 不会触发,这是代码 $(document).ready(function () { //$("#MySplitter").spli
要将节点添加到我的树中,我使用 JavaScript 推送: var parent1 = { "id": "parent1", "data": {extra:"some extra d
如何获取jsTree中的所有节点? 我正在用 xml 构建 jsTree Root -----A -----A1 -----A1.1
如果我没记错的话,当使用 jstree 时,如果检查了父节点,那么它的子节点也会被检查。像这样 我正在做的是,我正在进行 ajax 调用并仅加载父节点。我检查了父节点,当我展开父节点时,我正在进行另一
是否可以为 jsTree jQuery plugin/library 生成的类名添加前缀?如果没有,是否有已知的路径可以让单个页面共享两个样式不同的 jsTree 元素? 问题: 我有一个(基于 PH
我的 jsTree 包含在树加载时设置的 html 数据(请参阅下面的 javascript)。这工作正常。但是,我希望能够根据某些用户操作在 Ajax 请求中重新加载整个树。我基本上需要重新加载从
我是一名优秀的程序员,十分优秀!