0

Automatically clear text after user input

asked 2011-12-20 08:52:25 +0800

Jezreel gravatar image Jezreel
12

Dear All:

I have been enjoying learning ZK framework and trying my best to figure out my questions as I get along.
Now I am having a question which could not really be solved by myself, can anyone offer me a hint?

The question is the following:

My Goal: (similar to database search function)
(done) implement a textbox for user input;
(done) apply a onChanging event listener to capture user input for further processing (database transactions) ;
(stuck) automatically clear the textbox after this;

My Question:
The program does not clear text, unless the user input is not found in the database then next time after the user input
automatic text clear will happen. What change can i make so that the automatic text clear will happen every time after
the user input?

The following is the code snippet:

public void onChanging$itemBox(InputEvent event){
String searched = event.getValue();
Item availiableItem = ItemDao.findById(searched);
if(availiableItem!= null){
TransactionClient(availiableItem);
transactionList.setModel(new ListModelList(transactionsNow));
}
itemBox.setText(null);
}

What went wrong, should I change the onChanging to something else?

Your response will be highly appreciated!!

Jezreel

}

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2011-12-20 16:43:46 +0800

twiegand gravatar image twiegand
1807 3

Jezreel,

Try clearing your textbox like this instead:

itemBox.setValue(null);

 
Also, you might consider listening to the onChanged event instead of onChanging. The onChanging event could give you misleading results if the user pauses while typing.  To see what I mean, try the following:
 
<textbox value="XXXXXX" onCreate="self.setFocus(true)" onChanging='alert("onChanging=" + self.getValue())' onChanged='alert("onChanged=" + self.getValue())'/> 

Once you run this, try typing one character and then stop.  You'll notice that your typed character doesn't come back to you.  If you tab out of the field, you'll get all the characters.  Just something to consider anyway.

Hope that helps,

Todd

link publish delete flag offensive edit

answered 2011-12-21 04:18:27 +0800

Jezreel gravatar image Jezreel
12

Thanks Todd for the prompt reply.

I tried

itemBox.setValue(null);

It does not make a difference.

I understand what you mean by the difference between onChanged and onChanging. However when I tried to use onChanged, it does not work at all.
The following is my textbox definition in zul file.

textbox id="itemBox" focus="true"/>

Simplest possible, since I am still very unlearned with a lot of configurations in UI, so that I just kept the basic configuration.

Here is the desired behaviour of the textbox:
> capture input from the user (implemented a onChanging event listener)(maybe another type of listener instead??? onChanged has been tried and not working)
> process on the server side (done)
>generate output to the user (done)
> clear text after user input (problematic, it only clears the input after the user input is not found and the user gives an input again )
> allow for user input (could be same as before but still capture the input) (problematic,
if the user gives the same input, event listener will not be invoked, reason is obvious)

Any more suggestions?

Jezreel

link publish delete flag offensive edit

answered 2011-12-21 08:08:56 +0800

Jezreel gravatar image Jezreel
12

Also I noticed with the original code:

public void onChanging$itemBox(InputEvent event){
String searched = event.getValue();
<b >Item availiableItem = ItemDao.findById(searched);</b>
if(availiableItem!= null){
TransactionClient(availiableItem);
transactionList.setModel(new ListModelList(transactionsNow));
}
itemBox.setValue(null);
} 

The DAO is implemented simply as following:

public Item findByBarcode(String barcode){
		Session sess = currentSession();
	    return (Item) sess.load(Item.class,barcode);
	}

It would not return anything but throws an error message if the object is not found, what can i do to fix the problem of clearing text only after an error message and then gives an input which is found in the database instead of clearing the text each time of a user input.

Thanks for your help

link publish delete flag offensive edit

answered 2011-12-21 09:34:57 +0800

gekkio gravatar image gekkio flag of Finland
899 1
http://gekkio.fi/blog

updated 2011-12-21 09:40:10 +0800

If you want to try the other event type, it's called "onChange", not "onChanged".

However, that event is not posted while the user is typing, only afterwards.
Also please note that you cannot reliably read or write the textbox value within "onChanging", because
1. The textbox value has not changed yet so getValue() will return the old value
2. The textbox value will change later (even if you set it to null, it will be overriden later once onChange event is posted)

link publish delete flag offensive edit

answered 2012-03-21 03:11:31 +0800

onsir gravatar image onsir
132 2

i try to clear Textbox based my code
http://www.zkoss.org/forum/listComment/18963-Integrate-Spring-MVC-Hibernate-with-ZK
so i modification like this to clear textbox but, i get error

public void onBtnClick(Event evt){
            try{
		//get form data from zul page
		String codeValue = code.getValue();
		String descriptionValue = description.getValue();
	      
                 department=new Department();
                department.setCode(codeValue);
                department.setDescription(descriptionValue);
                departmentService.save(department);

               
                //show message box
                Messagebox.show("Save success");
               
               //clear textbox
                 code = ((Textbox)evt.getTarget().getFellow("code"));
                 code.setValue("");


            }catch(Exception e){
                e.printStackTrace();
            }
	}

error

SEVERE: org.zkoss.zk.ui.WrongValueException: Enter proper text
        at org.zkoss.zul.SimpleConstraint.wrongValue(SimpleConstraint.java:365)
        at org.zkoss.zul.SimpleConstraint.validate(SimpleConstraint.java:336)
        at org.zkoss.zul.impl.InputElement.validate(InputElement.java:319)
        at org.zkoss.zul.impl.InputElement.setText(InputElement.java:270)
        at org.zkoss.zul.Textbox.setValue(Textbox.java:55)

how to clear textbox for my code?
thanks

link publish delete flag offensive edit

answered 2012-03-21 10:14:02 +0800

onsir gravatar image onsir
132 2

i had search in this forum, but i dont get solution for my code.

link publish delete flag offensive edit

answered 2012-03-22 02:28:38 +0800

onsir gravatar image onsir
132 2

problem solved after i remove this code

constraint="/.+/ :Enter proper text"

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-12-20 08:52:25 +0800

Seen: 467 times

Last updated: Mar 22 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More