Ok. - I got it working now.
As there are so many views to my question here, I like to post the example with that I did it now:
Table Export with Structure and Sample Data:
CREATE TABLE IF NOT EXISTS `test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`parent_id` bigint(20) NOT NULL DEFAULT '0',
`sorting` bigint(20) NOT NULL DEFAULT '0',
`name` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;
INSERT INTO `test` (`id`, `parent_id`, `sorting`, `name`) VALUES
(1, 0, 0, 'Page 1'),
(2, 0, 1, 'Page 2'),
(3, 0, 2, 'Page 3'),
(4, 0, 3, 'Page 4'),
(5, 0, 4, 'Page 5'),
(6, 0, 5, 'Page 6');
Client side code (test.html):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script src="common/dhtmlx.js"></script>
<script src="codebase/dhtmlxdataprocessor_debug.js"></script>
<script src="codebase/connector.js"></script>
<link rel="stylesheet" href="common/dhtmlx.css" type="text/css" charset="utf-8">
<title></title>
</head>
<body>
<div id="treeboxbox_tree" style="width:250px; height:218px;background-color:#f5f5f5;border :1px solid Silver;; overflow:auto;"></div>
<script>
tree = new dhtmlXTreeObject("treeboxbox_tree", "100%", "100%", 0);
tree.setSkin('dhx_skyblue');
tree.setImagePath("./common/imgs/");
tree.enableDragAndDrop(true);
tree.setDragBehavior("complex");
tree.loadXML("tree/test_connector.php");
myDataProcessor = new dataProcessor("tree/test_connector.php");
myDataProcessor.init(tree);
</script>
</html>
Server side code (test_connector.php):
<?php
require_once("../config.php");
$res=mysql_connect($mysql_server,$mysql_user,$mysql_pass);
mysql_select_db($mysql_db);
require_once("../codebase/tree_connector.php");
$tree = new TreeConnector($res, "MySQL");
$tree->enable_log("temp.log",true);
function custom_sort($sorted_by)
{
$sorted_by->clear();
$sorted_by->add("sorting","ASC");
}
function my_update($action)
{
$sql = "SELECT sorting FROM test WHERE id = {$action->get_id()};";
$query =mysql_query($sql);
$row = mysql_fetch_row($query);
if($row[0] < $action->get_value('tr_order'))
$sql = "UPDATE test SET sorting = sorting + 1 WHERE parent_id = {$action->get_value('parent_id')} AND sorting > {$action->get_value('tr_order')};";
else
$sql = "UPDATE test SET sorting = sorting + 1 WHERE parent_id = {$action->get_value('parent_id')} AND sorting >= {$action->get_value('tr_order')};";
mysql_query($sql);
if($row[0] < $action->get_value('tr_order'))
$sql = "UPDATE test SET sorting = {$action->get_value('tr_order')} + 1 WHERE id = {$action->get_id()};";
else
$sql = "UPDATE test SET sorting = {$action->get_value('tr_order')} WHERE id = {$action->get_id()};";
mysql_query($sql);
$sql = "SET @new_sorting := -1;";
mysql_query($sql);
$sql = "UPDATE test SET sorting = (@new_sorting := @new_sorting + 1) WHERE parent_id = {$action->get_value('parent_id')} ORDER BY sorting;";
mysql_query($sql);
$action->success();
}
$tree->event->attach("beforeSort", "custom_sort");
$tree->event->attach("beforeUpdate", "my_update");
$tree->render_sql("SELECT * FROM test", "id", "name", "", "parent_id");
?>
And of course (config.php) - Just to make it complete:
<?php
$mysql_server="localhost";
$mysql_user = "root";
$mysql_pass = "";
$mysql_db = "test";
?>
This is just and only the implematation for the root level and without any other things to handle. Furthermore reorganizing the ‘sorting’ field may could be done more elegant. Comments are highly welcome.
Thanks to Alexandra! Even I did a different implementation you brought me back to investigate my Problem once more.