Download the PHP package serj/sortable-tree without Composer
On this page you can find all versions of the php package serj/sortable-tree. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download serj/sortable-tree
More information about serj/sortable-tree
Files in serj/sortable-tree
Package sortable-tree
Short Description A set of classes for Yii2 to create and maintain a tree.
License MIT
Informations about the package sortable-tree
yii2-sortable-tree
A set of classes for Yii2 to create and maintain a tree-like structure.
Installation
To import the component to your project, put the following line to the require section of your composer.json file:
or run the command
To create database tables apply a migration.
Usage
Adding a root
Adding a nested item
Assume that root item has an id = 1. To add an item under the root:
Let's add another item, but insert it before previous one. We assume that last inserted item has id = 2.
For the moment, our tree looks like this:
Moving items
Let's swap items 2 and 3
Now it should be like this:
Let's nest the item 3 into the item 2
The result is:
Deletion
We deleted item 2 and its child 3. The root item left.
Getting the tree
There are many other methods to work with the tree. For more information you can explore public methods of the Tree class and unit tests.
I want to store more data
Lets suppose you want to store title, created_at, updated_at fields. And you no longer want to remove items from the tree, but mark them as deleted. To achieve this, we can extend the Tree calss. But first, let's modify a migration.
Do not forget to apply a new migration.
Extend the tree class. Add and overwrite some methods.
To skip deleted items we've added a filter in the class constructor. Let's implement it.
Now to add an item, pass an array of extra attributes that was added to migration. In or case it's title.
To edit a title of the existing item:
For deletion use the same method as above.
Tree class triggers a set of events, which might be useful.
For example to get ids of the items before they are deleted: