Most of my web application have some kind of hierarchical structure in their models. It gives my users the ability to categorize their data in a pleasant way. The only problem is that its hard to create a user interface for editing these hierarchies. I made a prototype which adds nested set support to the symfony (1.2) generator. This extension allows for full drag&drop nested set support! See for yourself by clicking the image:
The editor uses the
treeTable jQuery plugin with added D&D support.
Full source is available
Today I created the
redotheoffice github repository. Right there you can find the full source of the project.
If you want to join the development or extend it, please send me an email or leave a reply to this entry.
As I’m quite short on time right now, so I’ll only do a brief explanation and give you hints where to look in the source.
One first note is that the current implementation of NestedSet has a bug. Moving a deep branch to the root may corrupt the tree. I made a ticket and patch, but it’s not yet patched in the repositories. So manually update your Doctrine library, or only use this code for testing purposes…
The editor is based on a really simple model, using Doctrine’s actAs NestedSet (see
--- Tree: actAs: NestedSet: hasManyRoots: true rootColumnName: root_id columns: name: type: string(255)
Next step is creating a default admin generator for the ‘Tree’ model. How to do this is clearly explained in Jobeet day 12: the Admin Generator. I think I did something like:
./symfony generate:app backend ./symfony doctrine:generate-admin backend Tree --module=tree
jQuery treeTable plugin
First enhancement is to implement the
treeTable plugin. This turns the normal generated table into a collapsible tree.
Download the treeTable code, I copied the
.css to the
web/css and added them to the
apps/backend/config/view.yml. Also make sure to load
I happen to like the way jQuery can be loaded through the googleapi system. Symfony can handle the full URL’s by default, this style is easy to maintain and upgrade. Google also makes sure the headers of the js library are set correctly, which minimizes reloads of the files.
TreeTable is then enabled by overwriting the
apps/backend/modules/tree/templates/_list.php template with some code that writes the correct classes to the individual rows. Also the table needs an
id for easy finding the object:
Doing this should give you a collapsible tree right inside your Admin Generator table.
Adding Drag&Drop support
_list.php template, see github. After dropping the item on a new parent the
treeTable plugin takes care of updating the client visual interface. It’s up to us to send the data back to the server.
For this I added a hidden select on each row of the table:
<td> <input type="checkbox" name="ids" value="<?php echo $tree->getPrimaryKey() ?>" class="sf_admin_batch_checkbox" /> <input type="hidden" id="select_node-<?php echo $tree->getPrimaryKey() ?>" name="newparent[<?php echo $tree->getPrimaryKey() ?>]" /> </td>
The implementation of the TreeForm is also noteworthy. It removes all nested set columns (
level) and adds a fully functional dropdown for selecting the parent. This code can easily be dropped into your project and doesn’t depend on jQuery or any other external library. After submitting the form makes sure all columns for the nested set are properly updated to the new parent. See the image below, and the
TreeForm.class.php file at github.
Things can always become better:
- Add a
serializefunction to treeTable which enables the possibility to get rid of the hidden input element on each row.
- Make it possible to edit the order of children from the same parent (now it always inserts asFirstChild). Possible solution could be an interface like
- Move the
Update tree orderbatch action from the dropdown to the table footer (next to the
- Change this prototype into a Plugin
Thanks for reading!