-
-
Save esfand-r/4492658 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<window apply="org.zkoss.bind.BindComposer" viewModel="@id('vm') @init('org.zkoss.mvvm.examples.duallistbox.DragVM')"> | |
<hlayout> | |
<vlayout> | |
List1 | |
<listbox model="@bind(vm.list1)" width="300px" height="300px" droppable="true" | |
onDrop="@command('dropToList1',item=event.dragged.attributes.item)"> | |
<template name="model"> | |
<listitem draggable="true" droppable="true" | |
label="@load(each.name)" attributes.item="@load(each)" | |
onDrop="@command('insertToList1',item=event.dragged.attributes.item, base=each)"/> | |
</template> | |
</listbox> | |
</vlayout> | |
<vbox vflex="1" pack="middle" width="100px"> | |
</vbox> | |
<vlayout> | |
List2 | |
<listbox model="@bind(vm.list2)" width="300px" height="300px" droppable="true" | |
onDrop="@command('dropToList2',item=event.dragged.attributes.item)"> | |
<template name="model"> | |
<listitem draggable="true" droppable="true" | |
label="@load(each.name)" attributes.item="@load(each)" | |
onDrop="@command('insertToList2',item=event.dragged.attributes.item, base=each)"/> | |
</template> | |
</listbox> | |
</vlayout> | |
</hlayout> | |
</window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.zkoss.mvvm.examples.duallistbox; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import org.zkoss.bind.annotation.BindingParam; | |
import org.zkoss.bind.annotation.Command; | |
import org.zkoss.bind.annotation.NotifyChange; | |
public class DragVM { | |
List<Item> list1; | |
List<Item> list2; | |
/** | |
* Initialize the view model with fetching the ATS configurations data for the first time. | |
*/ | |
@Init | |
public void init() | |
{ | |
list1 = new ArrayList<Item>(); | |
list2 = new ArrayList<Item>(); | |
for (int i=0;i<10;i++){ | |
list1.add(new Item("Item "+i)); | |
} | |
} | |
public List<Item> getList1() { | |
return list1; | |
} | |
public List<Item> getList2() { | |
return list2; | |
} | |
@Command | |
@NotifyChange({"list1","list2"}) | |
public void dropToList1(@BindingParam("item") Item item){ | |
if (item != null) | |
{ | |
if (this.list1.contains(item)) | |
{ | |
this.list1.remove(item); | |
} | |
this.list1.add(item); | |
this.list2.remove(item); | |
} | |
} | |
@Command | |
@NotifyChange({"list1","list2"}) | |
public void dropToList2(@BindingParam("item") Item item){ | |
if (item != null) | |
{ | |
if (this.list2.contains(item)) | |
{ | |
this.list2.remove(item); | |
} | |
this.list2.add(item); | |
this.list1.remove(item); | |
} | |
} | |
@Command | |
@NotifyChange({"list1","list2"}) | |
public void insertToList1(@BindingParam("base") Item base,@BindingParam("item") Item item){ | |
if (item != null && base != null && this.list1.contains(base) && this.list2.contains(item)) | |
{ | |
this.list1.add(this.list1.indexOf(base), item); | |
this.list2.remove(item); | |
} | |
if (item != null && base != null && this.list1.contains(base) && !this.list2.contains(item)) | |
{ | |
Collections.swap(this.list1, this.list1.indexOf(base), this.list1.indexOf(item)); | |
} | |
} | |
@Command | |
@NotifyChange({"list1","list2"}) | |
public void insertToList2(@BindingParam("base") Item base,@BindingParam("item") Item item){ | |
if (item != null && base != null && this.list2.contains(base) && this.list1.contains(item)) | |
{ | |
this.list2.add(this.list2.indexOf(base), item); | |
this.list1.remove(item); | |
} | |
if (item != null && base != null && this.list2.contains(base) && !this.list1.contains(item)) | |
{ | |
Collections.swap(this.list2, this.list2.indexOf(base), this.list2.indexOf(item)); | |
} | |
} | |
static public class Item{ | |
String name; | |
public Item(String name){ | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment