简体   繁体   中英

Adding html and java script code in GWT

I have a requirement to have a spinner field in GwT, but as i am using old version of GWT and i am not able to get the exact functionality which i required.

Now i am making use of GWT HTML widget which is being provide by GWT.

I have added the below code in the html widget

HTML spinnerHours = new HTML("<p> Time in Hours : <INPUT ID=\"hours\" TYPE=\"NUMBER\" MIN=\"00\" MAX=\"23\" STEP=\"1\" VALUE=\"00\" SIZE=\"6\"  onkeypress=\"return validate(event)\"> <script type=\"text/javascript\">  function validate(key){ var keycode = (key.which) ? key.which : key.keyCode; var hours = document.getElementById('hours'); if ((keycode < 48 || keycode > 57)){return false;}else{if (hours.value.length <2){return true;}else{return false;}}}</script>");

But the code which is written in the script tag to do some validations is not working as expected when used part of widget

The java script works fine when tested in a browser as standalone ie it will not allow to type alphabets or special characters and it will have a max length of two digits

Can we implement it in this way? Can someone help on how can i make this work or any other suggestions?

Thanks

You can do this this way. Or create a html div, call a jsni method which creates your spinner which can call other jsni method in which you can write validation method.

The best way to this, create your own version of gwt widget, using flowpanel, selectbox/combobox/textbox(s) using composite class. You can define your click handlers, valuechange handlers or any other handlers you require in pure java and then perform validation in java.

Here is guide how to create a new widget using composite

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM