0

File with JavaScript in Textbox

asked 2018-08-28 16:08:35 +0800

softteam gravatar image softteam
45 4

updated 2018-08-28 16:37:31 +0800

Hi all, I would like to move this code below to a file so I can reuse it in more Textboxes without copying the same code, just putting the source of the file in the Textbox tag. Is it possible? Thanks.

<textbox id="TBIdFunc" xmlns:w="client">                                            
  <attribute w:name="doKeyDown_">
    <![CDATA[
      function (evt) {
        var keyCode = event.keyCode;
          if( keyCode == 8 // back space
            || keyCode == 13 // enter
            || keyCode == 16 // enter
            || (keyCode >= 35 && keyCode <= 40) // home, end, arrows
            || keyCode == 46) { // Delete                                                    
            this.$doKeyDown_(evt);

          } else if ( !evt.shiftKey &&  // only do without shift key
            (keyCode >= 65 && keyCode <= 90)) {// teclas A-Z                                                 this.$doKeyDown_(evt);                                                                  
          } else {                                                       
             evt.stop();
             return;
          }
       }
   ]]>
 </attribute>
</textbox>
delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-08-31 12:22:17 +0800

cor3000 gravatar image cor3000
4263 1 7
ZK Team

for a single reusable method override I often use zk.override in combination with a feature flag

zul file

<!-- TODO: add script globally at root page or via lang-addon.xml -->
<?script src="customTextbox.js"?>
<zk xmlns:w="client">
  Default Textbox: <textbox/>
  <separator/>
  Customized Textbox: <textbox w:featureEnabled="true"/>
</zk>

customTextbox.js

zk.afterLoad('zul.inp', function() {
  var xTextbox = {};
  zk.override(zul.inp.Textbox.prototype, xTextbox, {
    doKeyDown_ : function(evt) {
      if(!this.featureEnabled) {
        xTextbox.doKeyDown_.apply(this, arguments);
        return; //exit if feature is not enabled
      }
      var keyCode = event.keyCode;
      if( keyCode == 8 // back space
        || keyCode == 13 // enter
        || keyCode == 16 // enter
        || (keyCode >= 35 && keyCode <= 40) // home, end, arrows
        || keyCode == 46) { // Delete

        xTextbox.doKeyDown_.apply(this, arguments);

      } else if (!evt.shiftKey &&  // only do without shift key
                 (keyCode >= 65 && keyCode <= 90)) {// teclas A-Z

        xTextbox.doKeyDown_.apply(this, arguments);

      } else {
        evt.stop();
        return;
      }
    }
  });//zk.override
});//zk.afterLoad

see this runnable example on zkfiddle

link publish delete flag offensive edit
0

answered 2018-08-31 17:12:02 +0800

softteam gravatar image softteam
45 4

Did it like this and didn't work. I can introduce numbers in my Textbox.

Start of my zul file:

<?script src="/Recursos/js/nonumeros.js"?>
<zk xmlns:n="native">

Textbox I want to edit:

<textbox xmlns:w="client" w:featureEnabled="true"/>

My JS file (nonumeros.js)

  zk.afterLoad('zul.inp', function() {
  var xTextbox = {};
  zk.override(zul.inp.Textbox.prototype, xTextbox, {
    doKeyDown_ : function(evt) {
      if(!this.featureEnabled) {
        xTextbox.doKeyDown_.apply(this, arguments);
        return; //exit if feature is not enabled
      }
      var keyCode = event.keyCode;
      if( keyCode == 8 // back space
        || keyCode == 13 // enter
        || keyCode == 16 // enter
        || (keyCode >= 35 && keyCode <= 40) // home, end, arrows
        || keyCode == 46) { // Delete

        xTextbox.doKeyDown_.apply(this, arguments);

      } else if (!evt.shiftKey &&  // only do without shift key
                 (keyCode >= 65 && keyCode <= 90)) {// teclas A-Z

        xTextbox.doKeyDown_.apply(this, arguments);

      } else {
        evt.stop();
        return;
      }
    }
  });//zk.override
});//zk.afterLoad
link publish delete flag offensive edit

Comments

please check in your browser console (network) that your JS file was actually loaded. if there is an error please adjust the path in the <?script> directive... make sure it is on your main page (not in an include!) or configured in a lang-addon.xml

cor3000 ( 2018-08-31 19:01:31 +0800 )edit
Your answer
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
1 follower

RSS

Stats

Asked: 2018-08-28 16:08:35 +0800

Seen: 9 times

Last updated: Aug 31

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