3.7.2. Drag & Drop
Steps to implement the feature
Drag & Drop of an item in a list involves a dragable object (e.g. ‘Subtask’) and a reference to a parent object (e.g. ‘Task’).
In order to implement this feature take the following steps:
Add metadata to the draggable object to mark it orderable within a certain reference (e.g. reference ‘task’ )
1 2 3 4 5 6 7 8 9 10
<object name="Subtask"> <metadata> ... <orderable>task</orderable> </metadata> ... <reference name="task" type="Task"> ... </reference> </object>
In the <orderable> metadata you need to specify the name of a reference, not the name of an object.
Add an attribute sortOrder to the draggable object (here object Subtask)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<object name="Subtask"> <metadata> <orderable>task</orderable> </metadata> ... <attribute name="sortOrder" type="Integer" notnull="true"> <metadata> <entity.default.value>Integer.MAX_VALUE</entity.default.value> <list.relation.mgmt>[number]</list.relation.mgmt> </metadata> </attribute> <reference name="task" type="Task"> <metadata> ... </metadata> </reference> </object>
Add metadata to the reference to create a finder (e.g. findByTask)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<object name="Subtask"> <metadata> ... <orderable>task</orderable> </metadata> ... <attribute name="sortOrder" type="Integer" notnull="true"> <metadata> <entity.default.value>Integer.MAX_VALUE</entity.default.value> <list.relation.mgmt>[number]</list.relation.mgmt> </metadata> </attribute> <reference name="task" type="Task"> <metadata> ... <create.finder.method.list>true</create.finder.method.list> </metadata> </reference> </object>
Generate the application
Create a changeSet in the Liquibase alters changelog to add the column to the table of the draggable object.
Set a default value of 0 as the sort_order is required and must not be null. On the entity the default value becomes Integer.MAX_VALUE, however this is not supported by SQL. 0 is fine to start with.
Listing 3.14 [projectdir]\[backenddir]\src\main\resources\config\liquibase\liquibase-alters-changeLog.xml1 2 3 4 5 6 7 8 9 10
<databaseChangeLog ...> ... <changeSet id="[id-named-after-jira-issue]" author="[your-initials]"> <addColumn tableName="subtask"> <column name="sort_order" type="INT" defaultValueNumeric="0"> <constraints nullable="false" /> </column> </addColumn> </changeSet> </databaseChangeLog>
Read more about Liquibase change sets
Run the application.