Sortable trees with Ext and PHP
To use Jack's work, I had to do two things: 1) pull existing data out of the CMS database and give it to the tree; and 2) take the reordered tree data and save back into the database.
Pulling out the existing data and giving it to the tree was pretty easy. Saving the result after the user did some reordering was trickier. I haven't seen any other explanations out there about how to do this, so here's a simplified example of how I accomplished both.
Assume you have a hierarchy of nodes (i.e., a tree) that looks like this:
lorem
ipsum
dolor
sit
amet
consectetur
adipisicing
elit
sed
do
eiusmod
Here is a MySQL database schema to store this hierarchy (you could also use something like modified preorder tree transveral (http://www.sitepoint.com/article/hierarchical-data-database/2) but this layout makes my example simpler):
CREATE TABLE `nodes` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) default NULL,
`parent_id` int(11) NOT NULL default '0',
`display_order` int(11) default NULL,
PRIMARY KEY (`id`)
);
And here's the data you'd insert:
+----+-------------+-----------+---------------+
id title parent_id display_order
+----+-------------+-----------+---------------+
1 lorem 0 1
2 ipsum 0 2
3 dolor 2 3
4 sit 2 4
5 amet 2 5
6 consectetur 0 6
7 adipisicing 0 7
8 elit 7 8
9 sed 8 9
10 do 8 10
11 eiusmod 0 11
+----+-------------+-----------+---------------+
Here is an HTML file with embedded JavaScript (same file for the sake of simplicity) which will load a TreePanel-compatible, JSON version of this hierarchy from backend.php, and then submit a JSON tree whenever a node gets drag-and-dropped:
#If you have any other info about this subject , Please add it free.# |