Java – how to allow users to drag and drop releases in vaadin tables?
I have a multi - row table with custom components I want to allow users to delay the release
I tried the following code, but I couldn't drag any lines:
tblStructure = new Table(); tblStructure.setSizeFull(); tblStructure.setSelectable(false); tblStructure.setSortEnabled(false); tblStructure.setDragMode(Table.TableDragMode.ROW); tblStructure.setNullSelectionAllowed(true); tblStructure.setDropHandler(new DropHandler() { /** * */ private static final long serialVersionUID = 1L; @Override public AcceptCriterion getAcceptCriterion() { return AcceptAll.get(); } @Override public void drop(DragAndDropEvent event) { } });
Solution
I know I'm responding to an old thread I find it very troublesome in vaadin I refuse to use treetable or other components to achieve this result I want to share my solution with anyone who searches this content because I can't find a simple and clear answer myself I also want to do something for the community because we use a lot of open source software
Create a class, such as sortabletable, which extends the regular vaadin table
public class SortableTable extends Table { private static final long serialVersionUID = 1L; public SortableTable() { setDragMode(TableDragMode.ROW); setSelectable(true); setDropHandler(new DropHandler() { private static final long serialVersionUID = 1L; @Override public AcceptCriterion getAcceptCriterion() { return AcceptAll.get(); } @Override public void drop(DragAndDropEvent event) { Transferable t = event.getTransferable(); Object sourceItemId = t.getData("itemId"); AbstractSelectTargetDetails dropData = (AbstractSelectTargetDetails)event.getTargetDetails(); Object targetItemId = dropData.getItemIdOver(); switch(dropData.getDropLocation()) { case BOTTOM: moveAfter(targetItemId,sourceItemId); break; case MIDDLE: case TOP: final Object prevItemId = prevItemId(targetItemId); moveAfter(prevItemId,sourceItemId); break; } } }); } ...
Next, create this custom function to move table items
@SuppressWarnings("unchecked") /** * * @param targetItemId * @param sourceItemId * @return ItemId of the object the item moved to */ public Object moveAfter(Object targetItemId,Object sourceItemId) { if(sourceItemId == null) return null; Item sourceItem = getItem(sourceItemId); Object[] propertyIds = getContainerPropertyIds().toArray(); int size = propertyIds.length; Object[][] properties = new Object[size][2]; // backup source item properties and values for(int i = 0; i < size; i++) { Object propertyId = propertyIds[i]; Object value = sourceItem.getItemProperty(propertyId).getValue(); properties[i][0] = propertyId; properties[i][1] = value; } removeItem(sourceItemId); Item item = addItemAfter(targetItemId,sourceItemId); // restore source item properties and values for(int i = 0; i < size; i++) { Object propertyId = properties[i][0]; Object value = properties[i][1]; item.getItemProperty(propertyId).setValue(value); } return sourceItemId; } }
Let's try to explain For some obvious reason, when removeitem is called, the table item is completely deleted and deleted from the table You cannot put the same object back into a table except to copy its properties and values to a new row This is exactly what the custom moveafter does Another part of the code is inspired by the "tree to table drag - 'n-drop" example in the vaadin sampler mentioned by Nebraska in his previous post
I hope I can help others with this article to greet!
The above is the Java collected by programming house for you - how to allow users to drag and drop in the vaadin table? I hope this article can help you solve Java - how to allow users to drag and drop in the vaadin table? Program development problems encountered.
If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.