Uploaded image for project: 'ZK'
  1. ZK
  2. ZK-1891

Combobox with Validator will trigger reload selectedItem when typing

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Normal Normal
    • 6.5.4
    • None
    • Databind 2
    • None

      http://screencast.com/t/vl153iYts5

      <zk>
      	<zscript><![CDATA[
      		class MyVM {
      			private ListModel model;
      			private Item selItem;
      			
      			public MyVM () {
      				List list = new ArrayList();
      				list.add(new Item("01"));
      				list.add(new Item("02"));
      				list.add(new Item("03"));
      				model = ListModels.toListSubModel(
      					new ListModelList(list), new ItemComparator(), 10);
      			}
      			
      			public ListModel getModel () {
      				return model;
      			}
      			
      			public Item getSelItem () {
      				return selItem;
      			}
      			
      			public void setSelItem (Item selItem) {
      				this.selItem = selItem;
      			}
      			
      			public Validator getValidator(){
      			    return new Validator();
      			}
      		}
      		
      		class Item {
      			private String name;
      			public Item (String name) {
      				this.name = name;
      			}
      			public String getName () {
      				return name;
      			}
      		}
      		
      		class ItemComparator implements Comparator {
      			public int compare(Object text, Object item) {
      			    String val = text.toString().toLowerCase();
      
      			    if (val.isEmpty())
      			    	return 0;
      			    String name = "";
      			    if (item instanceof Item) {
      			    	name = ((Item)item).getName();
      			    }
      			    return name.toLowerCase().startsWith(val)? 0: -1;
      			}
      		}
      		
      		class Validator extends org.zkoss.bind.validator.AbstractValidator {
      			 public void validate(ValidationContext ctx) {
      	            if (ctx.getProperty().getValue() == null)
      	            	addInvalidMessage(ctx, "Please select an item!!");
      	        }
      		}
      	]]></zscript>
      	<window apply="org.zkoss.bind.BindComposer" 
      		viewModel="@id('vm') @init('MyVM')"
      		validationMessages = "@id('vmsgs')">
      		<combobox id="cb" model="@load(vm.model)" 
      			selectedItem="@bind(vm.selItem)@validator(vm.validator)"
      			autodrop="true" autocomplete="false">
      			<template name="model">
      				<comboitem label="@load(each.name)"/>
      			</template>
      		</combobox>
      		<label value="@load(vmsgs[cb])" style="color:red;" />
      	</window>
      
      	
      </zk>
      

            Unassigned Unassigned
            jimmyshiau jimmyshiau
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: